Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Opening the Closed Connection

 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic