File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Reusing Connection and Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Reusing Connection and Statement" Watch "Reusing Connection and Statement" New topic
Author

Reusing Connection and Statement

Rajesh Pathak
Ranch Hand

Joined: Aug 15, 2002
Posts: 56
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.


Rajesh(Bangalore,India)<br />SCJP2, SCWCD, SCEA, IBM-XML, UML-OOAD, IBM-Enterprise Connectivity with J2EE.
Andy Bowes
Ranch Hand

Joined: Jan 14, 2003
Posts: 171
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.


Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
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 not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reusing Connection and Statement
 
Similar Threads
Performance with preparedStatement.
JDBC Program Design Question
Connection Pool & Prepared Statement
Does jboss support pool of preparedstatement?
consuquences of not closing ResultSet?