• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Reusing Connection and Statement

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a application I have to query the same database many times (for select ,update...etc).
Should I reuse the same Connection and the same Statement? Or is it suggestable to follow the approach of create-use-close and so on.
Thanks in advance.
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The act of creating a new Connection to the database can take a significant length of time and so you would not want to do this every time that you execute a SQL statement. Ideally you should be using a pool of database connections that is managed by a javax.sql.DataSource resource. If you close a connection that is managed by the DataSource you are just returning it to the pool of available connections (i.e. it is not actually disconnected from the DB).
If you use the DataSource then you should get into the habit of writing getConnection() - use - close() code withing try {} catch() finally {} blocks to ensure that all of the DB resources are released even if an error occurs.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might wanna check DBCOnnectionBroker, at www.javaexchange.com.


The Connection Broker completely avoids the overhead required in establishing a new database connection (typically around 1 to 2 seconds) by reusing a collection of pre-established connections


hope this helps
 
I'm full of tinier men! And a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic