This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes close connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "close connection" Watch "close connection" New topic
Author

close connection

Susilo Saja
Ranch Hand

Joined: May 27, 2003
Posts: 91
Hi,
When I close an application, will it automatically close all db connection created by the application?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

You mean exiting an application? If yes, the connection object will no longer exist as JVM is shut down. It is recommended to close the connection once done using it.


Groovy
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

While the connection object does not exist, it is not actually "closed" until the connection times out (could be 30 minutes) or you close it. It is better to close the connection explicitly to avoid a potential resource leak.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Susilo Saja
Ranch Hand

Joined: May 27, 2003
Posts: 91
Thanks guys, actually I don't really understand what is a db connection in a more low level term. Can someone help me with this? Is it an IO stream?
Beside of taking resource in the jvm, did it also take resource in the database?
If it is, and it was closed automatically (either by application exit or timeout), did it also clear the resource in the database?
One last question is about the timeout, does the timeout start to count when the connection is not used? or it start to count from the time the connection was created? and can we change the timeout?
Ram Kumar Subramaniam
Ranch Hand

Joined: Jan 17, 2003
Posts: 68
Originally posted by Susilo Saja:
Is it an IO stream?
Ultimate its an IO stream ....
Beside of taking resource in the jvm, did it also take resource in the database?
If it is, and it was closed automatically (either by application exit or timeout), did it also clear the resource in the database?
Here is what i think happens. When you create a connection, resouces are alloted by the database too.
So if you keep creating a connection without closing it, the database will run out of connections
and the application will no longer be able to connect to the db.
Exiting the application will not close the connection. You need to do it explictly.
What they mean by timeout, is that, when the db notices that the connection alloted to the applicaton
has not been used for quite sometime, it deallocates the connection or resouces it alloted to for that
application when it requested for a connection. A java programmer should not just let connections hang
around and let it timeout. Also the next time u use the same connection in the application, you will get
a not connected error.
One last question is about the timeout, does the timeout start to count when the connection is not used? or it start to count from the time the connection was created? and can we change the timeout?
I am not sure about the mechanism on the timeout count . I think its database dependent.
Change the timeout of a connection is a database related operation.

I've explained all this stuff in lame man terms .... I strongly suggest you do some reading up
for the exact details.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: close connection
 
Similar Threads
Reason for not keeping Connection instance as static
JTA, java.sql.Connection.close()
system.exit(0)
Java Database connection
How can I rollback when connection close?