• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

When to close connection?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Always look on the bright side of life. At least this ad is really tiny:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic