GeeCON Prague 2014*
The moose likes JDBC and the fly likes Opening the Closed Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Opening the Closed Connection" Watch "Opening the Closed Connection" New topic
Author

Opening the Closed Connection

Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
Is it possible to re-open the closed connection?. If no how does the datasource concept work? Does it create a new connection object when ever we close the connection?
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

No, you cannot reopen a closed connection. It's a dead object for the most part and one that represents a physically dead connection.

Data sources create a new connection on getConnection(), as described in the API. Database pools on the other hand maintain a set of connections and open/close them as needed. If the number of available connections is below the pool minimum, a new one is opened.

The key thing to note is that once you open a connection with a DataSource, it is your responsibility to make sure it gets closed, which is why you should always use a finally block for closing JDBC resources.


My Blog: Down Home Country Coding with Scott Selikoff
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
If the number of available connections is below the pool minimum, a new one is opened.


Scott, Does this mean a new connection object will be created every time the available connections is below the minimum limit or when the existing connection is closed?
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

Database pooling is complex and is generally vendor-specific. That said, most have a minimum and maximum number of connections and will auto-create connections if the number available falls below the minimum.
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
will auto-create connections if the number available falls below the minimum
If that is the case what advantage do i get from connection pooling Scott, i could create connection object when ever it is required in my code? What is the advantage of having predefined number of connections in the connection pooling?
 
GeeCON Prague 2014
 
subject: Opening the Closed Connection