Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to close connection?

 
Allan Tan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .

 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic