jdbc - difference between connection.close() and connection= null -
if explicitly close connection calling close() on connection object, have set connection object null. difference in close() , null on connection object? if close connection ,still connection object maintained in connection pool? e.g.
connection dbconnection=null; preparedstatement preparedstatement = null; resultset rs; try { connection dbconnection= drivermanager.getconnection("jdbc:hsqldb:file:test5","sa", ""); ........... ........... dbconnection.close(); dbconnection=null; } catch (exception e) { logger.error("exception occured while fetching record:item details start method " + e.getmessage()); } { try { if (rs!=null) { rs.close(); rs=null; } } catch(sqlexception e) { logger.error(resultsetcloseexception + e.getmessage()); } try { if (preparedstatement != null) { preparedstatement.close(); preparedstatement=null; } } catch (sqlexception e) { logger.error(statementcloseexception + e.getmessage()); } try { if (dbconnection != null) { dbconnection.close(); dbconnection=null; } } catch (sqlexception e) { logger.error(connectioncloseexception + e.getmessage()); } }
is above code correct way close connection, prepared statement , resultset?
one closes connection, 1 sets connection reference null.
if don't close connection, can have connection leak. important close connection in block.
the close() operation closes connection--it doesn't anything connection reference. might not able do connection, it's not null. once it's closed can released collection pool, that's different yet again.
conclusion:: *connection.close()* close conection database , release resources. ***con = null***
- reference connection object deleted in case if connection open still open i.e. resources not free.
let me correct if getting wrong.