jdbc - Closing Oracle connection? -


we hear should close connection once done transaction. question here is: if don't close connection, garbage collector reclaim memory once out of method inside of creating connection. what's actual issue, if don't close connection?

no, gc doesn't here, because it's not memory.

a connection scarce resource on database server, not in app client. if clean your memory without telling server same you'll leak connection may not reclaimed. you'll run out.

same resultset - it's cursor.

you must always close sql resources - connection, statement, , resultset - in individual blocks, wrapped in try/catch blocks, in reverse order of acquisition.

public class jdbctemplate {      public object examplesqloperation(connection c) throws sqlexception {         object results = null;          statement st = null;         resultset rs = null;          try {            // sql stuff here            // load results object - data structure         } catch (sqlexception e) {            e.printstacktrace(); // better log it.         } {             databaseutils.close(rs);               databaseutils.close(st);         }          return results;     } }  public class databaseutils {     public static void close(connection c) {         try {             if (c != null) c.close();         } catch (exception e) {             e.printstacktrace();         }     }     // same statement, resultset } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -