• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

close connection

 
Susilo Saja
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When I close an application, will it automatically close all db connection created by the application?
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34237
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Susilo Saja
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic