java - Closing JDBC-Connections only in finally-block? -


why database connections closed @ 2 positions, once directly after use, , secondly additionally in finally-block using check null in order prevent them closed twice. doesn't suffice use block? finally-block executed in every case.

here official example of apache-tomcat jndi datasource how-to. point out there connection must closed under every circumstance. wonder why not sufficient use finally-block close-commands within end of main try {}-block seem redundent.

    connection conn = null;     statement stmt = null; // or preparedstatement if needed     resultset rs = null;     try     {         conn = ... connection connection pool ...         stmt = conn.createstatement("select ...");         rs = stmt.executequery();         ... iterate through result set ...         rs.close ();         rs = null;         stmt.close ();         stmt = null;         conn.close (); // return connection pool         conn = null; // make sure don't close twice     }     catch (sqlexception e)     {         ... deal errors ...     }         {         // make sure result sets , statements closed,         // , connection returned pool         if (rs != null)         {             try             {                 rs.close ();             }             catch (sqlexception ignore)             {             }             rs = null;         }         if (stmt != null)         {             try             {                 stmt.close ();             }             catch (sqlexception ignore)             {             }             stmt = null;         }         if (conn != null)         {             try             {                 conn.close ();             }             catch (sqlexception ignore)             {             }             conn = null;         }     } 

i write shorter:

    connection conn = null;     statement stmt = null; // or preparedstatement if needed     resultset rs = null;     try     {         conn = ... connection connection pool ...         stmt = conn.createstatement ("select ...");         rs = stmt.executequery();         ... iterate through result set ...     }     catch (sqlexception e)     {         // ... deal errors ...     }         {         // make sure result sets , statements closed,         // , connection returned pool         try         {             if (rs != null)                 rs.close ();             if (stmt != null)                 stmt.close ();             if (conn != null)                 conn.close ();         }         catch (sqlexception ignore)         {         }     } 

you have question - don't understand "official example" either. block enough.

however, code has more serious faults, namely if rs.close() throw exception, you'd have stmt , conn leaking out , you'd ignore exception silently. should not do. since java 7, using try-with-resources construct preferred way, if cannot go there, @ least handle each possible exception (rs, stmt, conn) separately don't cause each other leak.

for example apache commons dbutils has closequietly() purpose, because used common scenario. go somewhere spring jdbctemplate sort of stuff behind scenes.

edit: try-with-resources explained oracle here. in short, you'd this:

try (connection conn = yourcodetogetconnection();     statement stmt = con.createstatement();     resultset rs = stmt.executequery(query)) {     while (rs.next()) {         string coffeename = rs.getstring("cof_name");         int supplierid = rs.getint("sup_id");         float price = rs.getfloat("price");         int sales = rs.getint("sales");         int total = rs.getint("total");          system.out.println(coffeename + ", " + supplierid + ", " +                                 price + ", " + sales + ", " + total);     } } catch (sqlexception ex) {     // log, report or raise } 

where try-statement automatically deals conn, stmt , rs closing in cases , in order (the reverse order in state them). possible exceptions still need handle yourself.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -