• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

best way to return connection back to the connection pool

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
crispy bacon. crispy tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic