File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Regarding Many Connections in a service file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Regarding Many Connections in a service file" Watch "Regarding Many Connections in a service file" New topic

Regarding Many Connections in a service file

Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

I am writing a 'service file'. In this i am using more number of 'sql connections'. As of now i have declared Connection, PrepareStatement, ResultSet in Class level. So i am using these in my code again and again. ie. Every time i am creating new connection and closing it again.

Yesterday my friend said me to use a single connection through out the code. Like with out closing the connection.

Please help me.. Which better way(When i am using more SQL statements)


Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
I believe single connection is always better
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32793

I would use a connection pool. Even if it only has one connection, this lets the pool handle it for you. Keeping one connection open too long can give you a timeout error. Whereas getting a connection from the pool is much cheaper than getting a new one each time.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Regarding Many Connections in a service file
It's not a secret anymore!