File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Query about  Connection Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Query about  Connection Object" Watch "Query about  Connection Object" New topic
Author

Query about Connection Object

Mangala Garg
Greenhorn

Joined: Nov 26, 2002
Posts: 1
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
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
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
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
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
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query about Connection Object
 
Similar Threads
What to do if client closed?
method with SQL parameter
Would this cause problems closing statements ?
How to execute two SQL stmts. simulteneously ?
Insert CLOB using JDBC