Hello!! I want to know about closing the connection. How to create, use and close the connection object and what impact this open connectin object can have on the application??? I came across this piece of code. //DataSource ds PrepareStatement putStmt = ds.getConnection().prepareStatement("INSERT INTO TABLE_A( COL1, COL2, COL3) VALUES (?,?,?)"); I want to know what will happen to this connection object and if it will get closed automatically. If not how to close this connection? Please help, its urgent. Thanks, Mangala [ April 06, 2004: Message edited by: Bear Bibeault ]
eammon bannon
Ranch Hand
Joined: Mar 16, 2004
Posts: 140
posted
0
The Connection will remain open until you explicitly close it. And you must close open Connection objects once you are finished with them or eventually your applicaiton will run out of avaliable Connecitons. This is important - so do it in a finally block.
Pawan Ramchandani
Ranch Hand
Joined: Aug 15, 2003
Posts: 78
posted
0
define this code in your bean ________________________________ public void closeDBConnection() { try{ if(st!=null) st.close(); if(!connection.isClosed()) connection.close(); }catch(Exception e) { e.printStackTrace(); } } ________________________________
and called in your jsp as ds.closeDBConnection();
Pawan Ramchandani<br />*******************************<br />SCJP 1.4<br />SCWCD <br />*******************************<br />Everything is okay in the end. If it's not okay, then it's not the end.
eammon bannon
Ranch Hand
Joined: Mar 16, 2004
Posts: 140
posted
0
Pawan Ramchandani is half way right. You'd still need a finally block to make certain the Connection is closed.
Pawan Ramchandani
Ranch Hand
Joined: Aug 15, 2003
Posts: 78
posted
0
YA I FORGOT TO MENTION THAT CODE THANKS TOREMIND ME AND CODE WILL BE protected void finalize() { try{ if(st!=null) st.close(); if(!connection.isClosed()) connection.close(); }catch(Exception e) { System.out.println("not closing con"); e.printStackTrace(); }
eammon bannon
Ranch Hand
Joined: Mar 16, 2004
Posts: 140
posted
0
Ehm, nope Pawan. finalize() is a method you supply to the garbage collector which it will call before it cleans up the Object. And since there is never any guarantee that the GC will ever cleanup your Object relying on it to free up resources is flawed. So what you should do is:
[ April 08, 2004: Message edited by: eammon bannon ]