• Post Reply Bookmark Topic Watch Topic
  • New Topic

best way to return connection back to the connection pool  RSS feed

 
jay donald
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Could someone please tell me the correct way to return a connection to a connection pool. I have seen these two so far:
1. conn.close() // releases the connection object's database
2. conn.commit()// makes database changes permanent and releases the lock

The reason i'm asking is that i always use the first option but i saw second option used in some other application. Could someone tell me which is better and why? Thanks.

Jay
[ April 20, 2006: Message edited by: jay donald ]
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never seen that a commit() releases the connection in any pool I've ever used. It makes no sense - after a commit you could still want to do other things with the Connection. After a close() you should never do anything with the Connection.

Just my <small amount of your local currency> worth.
 
Jass Singh
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The practice we follow for foolproof connection releasing is:



Here UtilClass is our custom class and its releaseConnection(objConn) method essentially contains objCon.close() along with normal Exception handling.


Regards,
Jass
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using a "standard" connection pool (i.e., you get the connection via a javax.sql.DataSource), you should call close() on the connection to return it to the pool.

Note that commit() does not return the connection to the pool. You need to close() it after calling commit().

Note that if you use a connection pool in this way, close() doesn't really close the connection (like when you make a direct connection to the database), it just returns the connection to the pool.

(This is how it works with connection pool implementations like Apache Jakarta Commons DBCP).
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!