title: Do I get a memory leak by not closing my JDBC PreparedStatements?
body: <p>I'm working with java.sql PreparedStatements, and I was wondering about the following:</p>
  
  <p>In <a href="http://javadude.com/articles/passbyvalue.htm" rel="nofollow">Java is Pass-by-Value, Dammit!</a>, the following is given as an example of Java's Pass-By conventions:</p>
  
  <pre><code>public void foo(Dog d) {
      d = new Dog("Fifi"); // creating the "Fifi" dog
  }
  
  Dog aDog = new Dog("Max"); // creating the "Max" dog
  // at this point, aDog points to the "Max" dog
  foo(aDog);
  // aDog still points to the "Max" dog 
  </code></pre>
  
  <p>In my code, this comes up as the following (semi-Java pseudocode): </p>
  
  <pre><code>public void method() {
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      rs = executeStatement(sql-string, pstmt, conn, vars...);
    } catch (....) { /* error-handling */ }
    /// do stuff with the data
    rs.close();
  }
  </code></pre>
  
  <p>where <code>executeStatement</code> is (something similar to) the following:</p>
  
  <pre><code>ResultSet executeStatement(String sql, PreparedStatement pstmt, Connection conn, Object[] vars...) {
    pstmt = conn.prepareStatement(sql);
    /// set pstmt variables...
    ResultSet rs = pstmt.execute();
    return rs;
  }
  </code></pre>
  
  <p>From what I understand of Java's pass-by conventions, it's useless for me to do anything with pstmt in the main code, as it will still be null even after calling <code>executeStatement</code>. However, because closing a <code>PreparedStatement</code> also closes the ResultSet, I know that the <code>PreparedStatement</code> that is created in <code>executeStatement</code> is not closed when I'm processing the ResultSet.</p>
  
  <p>Does this imply that there is a memory leak here? (My understanding of memory leaks and how they can be diagnosed/fixed is spotty at best).  Is there any way I could structure this differently to avoid a leak, but continue having a method that can execute an SQL string and return the ResultSet in an abstract manner?</p>