Meaningless Drivel is fun!*
The moose likes JDBC and the fly likes When to close connection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "When to close connection?" Watch "When to close connection?" New topic
Author

When to close connection?

Allan Tan
Greenhorn

Joined: Oct 31, 2000
Posts: 7
When is it a good time to close connections? Can I keep connection open for later usage?
Can I use a static connection across objects so that I keep one connection object open all the time? Is this better?
Or should I assume that such tasks are performed by the connection pool, and I always have to open and close my connections whenever I am to access the database?
David Freels
Ranch Hand

Joined: Feb 01, 2001
Posts: 102
This all depends on the type of application. Server applications are better off using a connection pool and aging your connections (closing the connection after a pre-determined amount of time). Creating a connection is an expensive operation so pooling saves time. Single user applications with limited access to the database benefit from closing the connection after the data is retrieved.
Hope this helps.
David
hanumanth reddy
Ranch Hand

Joined: Jun 12, 2000
Posts: 118
u must free the connection as soon as u dont need it .
if u keep the connection open for long time it would tax the system and performance may go down .


<a href="http://www.jobklub.com" target="_blank" rel="nofollow">http://www.jobklub.com</a><br /> 'Add Job To Life'
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Or should I assume that such tasks are performed by the connection pool, and I always have to open and close my connections whenever I am to access the database?

I think, that if we are using a connection pool, we take a connection from the pool (eg. pool.getConnection()), and after using it we return the connection to the pool (eg. pool.returnConnection()). It is the Pool classes which take care of opening or closing connections- The classes using the connection pool do not open/close connection.

[This message has been edited by Rahul Rathore (edited March 12, 2001).]
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
I agree with David that the best approach depends on the type of application being made by you and your system resources etc.
Keeping a connection open is taxing, but on a server application where a frequent use of connection is made, repeatedly opening and closing a connection would be equally taxing and slow the server response. A pooling strategy is the best compromise. A pooling strategy allows us to keep an optimal number of connections open and to recycle the connections among the users. Sophesticated strategies like ageing of connection further optimizes the use of resources. But a pool may be overkill for single-user applications.

[This message has been edited by Rahul Rathore (edited March 12, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When to close connection?
 
Similar Threads
garbage collection and statement closing question
Doubt in closing connection, resultset, statemnet object???
Connection Pooling Help Needed
DB connection in a servlet question
How do the connections get closed in Connection Pool