aspose file tools*
The moose likes JDBC and the fly likes Close connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Close connection" Watch "Close connection" New topic
Author

Close connection

Tom Joiner
Ranch Hand

Joined: Sep 19, 2006
Posts: 47
Lets suppose I create a database connection...


Now I have an open connection. Do I need to close it explicitly?

If I do not close it explicitly, will the database run out of its pool of connections?

Thanks for any info


SCJP
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Moving to the JDBC forum...


GenRocket - A Test Data Generation Platform
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
It's good practice to close it if you no longer need it, since it consumes limited system resources (namely, a port). If the "connection" object goes out of scope and is garbage-collected, that will have the same effect -eventually- as closing the connection explicitly, but that's not good style at all.

In many cases you need the connection more than once, so you keep it around. That's especially the case with web applications, where many users may access it. Then the use of a connection pool would be indicated.

You'd only run out of DB connections if you open new connections all the time and keep the previous in scope somehow. But that would be a really bad implementation anyway.


Ping & DNS - updated with new look and Ping home screen widget
Tom Joiner
Ranch Hand

Joined: Sep 19, 2006
Posts: 47
Then the use of a connection pool would be indicated.

This I don't quit understand. Are you suggesting we create our own connection pool, independent of the connection pool the database runs for itself?

Is this not reinventing the wheel. Wouldn't it be better coding practice to open a connection when you want it, then close it, and let the database handle having a pool?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
The database does not keep a connection pool - the application server (or web server) that connects to it does. So, no, this would not be duplication of something.
Tom Joiner
Ranch Hand

Joined: Sep 19, 2006
Posts: 47
I am using Tomcat. Therefore, as I understand it, Tomcat keeps a connection pool. When I open a database connection, it is using this pool.

Now if I wanted to, I could create another connection pool- one that is owned by my application perhaps, in a servlet that keeps a set of connections. But this would be duplicating what Tomcat (I assume) already provides. Is this correct?
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
I'm creating connections to the database in which every session has it's own connection. Thereby I'm using the HttpSessionBindingListener for closing the connection if the session ends.

I do this, because I want to follow each user unique on the database en I've created a class for that, where I store his username for the different databases.

Here the code for closing the connection. The class has to implement the
HttpSessionBindingListener



Until now I'm not a fan of connection pooling within Tomcat, because I've to set this which every new installation.

Now I can use a simple textfile for reading the different connection parameters which I and my customers can change easily.

Are there good advantages for connection pooling which will outweight this disadvantage.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
I am using Tomcat. Therefore, as I understand it, Tomcat keeps a connection pool.

Not automatically - only if you tell it do so, and how to do it. If you have done so, then indeed you wouldn't want to create another one. But you also wouldn't close the connections - you would return them to the pool after you're done with them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Close connection
 
Similar Threads
Closing of connection object in Stateless Session Beans
Synchronization Problem whil accessing JDBC Code
DatasourceRealm connection pool exhausting
Closing/Clearing objects within a Session when browser closes
Problem in updating/deleting data from MS Access Database