This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JDBC and the fly likes how to properly handle connections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to properly handle connections" Watch "how to properly handle connections" New topic
Author

how to properly handle connections

Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
Hi, I have a utility class that has 2 methods - openConnection() and closeConnection(). These methods are used by a data access class that has 5 methods. Each of the 5 methods will open a connection and close a connection, and they run one after the other as part of an initialization routine for retrieving data.

My questions is: Should I reuse a connection by passing it's reference around to each of the 5 methods, or should I open and close a separate connection for each of the 5 methods?

Thanks!
Baiju Varugese
Ranch Hand

Joined: Dec 16, 2003
Posts: 74
it is better to pass the reference of the connection across methods, b'cos opening a connection is costly operations. but it all depends upon you need/requirement how you want the connection in different methods.


Measure twice cut once carpenter theory <br /> <br />Baiju <br />SCJCP, SCBCD, IBM-486, IBM-484, SCEA Part I
Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

Hi Andy Hahn

This is totally depend upon your requirements. If your requirement is using different connection at a single time then you have to create multiple connection whereas if you can handle one connection in different operation then you can do the same.

As far as my project is concern I am using single connection in my entire project where as in certain area I am using more then one connection according to my need.


Love is GOD and GOD is Love.
Anand Karia
IT Concretor.......
M/s. Anand Karia Concreting IT
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

It also depends on where your application gets its database connections from.

If it is running inside an application server, it most likely gets database connections from a connection pool managed by the application server (i.e., you're not opening connections yourself using DriverManager, but you're using a DataSource object provided by the appserver). In that case, you should release (close) each connection as soon as you're done with it, and not hold it for a longer time, otherwise you'll be harming the scalability of your application.

If it's a standalone application that directly opens database connections, it would be better to not close the connection immediately.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to properly handle connections
 
Similar Threads
Problem with preapred Statement
Connection object from EJB or DAO
Open/Close file
RandomAccessFile & Threading
Problem closing PreparedStatement