title: Possible resource leak when reusing PreparedStatement?
body: <p>Suppose you have the following code:</p>
  
  <pre><code>    Connection connection = null;
      PreparedStatement ps = null;
  
      try {
          Connection = connectionFactory.getConnection();
  
          ps = statement.prepareStamement(someQuery);
          // execute and read and stuff
  
          // now you want to use the ps again, since you don't want ps1, ps2, ps3, etc.
          ps = statement.prepareStatement(someOtherQuery); // DOES THIS FORM A POTENTIAL LEAK?
      } catch (a lot of exceptions) {
          // process exceptions
      } finally {
  
          // close the resources (using util class with null-checks and everything)
          SomeUtilClass.close(ps);
          SomeUtilClass.close(connection);
      }
  </code></pre>
  
  <p>Is reusing the ps variable a potential leak?</p>
  
  <p>And if so, I would hate to declare multiple of such prepared statements (ps1, ps2, ps3, etc). How should I refactor this?</p>
  
  <p>Thoughts anyone?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Receiving several answers stating that this doesn't matter. I would like to point out that I'm experiencing open cursors that stay open a little bit too long, and was wondering if this pattern might have something to do with it. </p>
  
  <p>My thoughts were:</p>
  
  <p>The first statement gets dereferenced and GC'ed, so how does the first PreparedStatement get closed (Database-wise) in this example?</p>