• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Database Connection Pool Not Releasing Connection to pool

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Class which performs JDBC calls on a DB2 Database

The Database connections are pooled via the datasource,
There is a method called getdbconnection() which obtains a pooled connection.

The issue is that the method to obtain a connection and the method to release a connection are synchronized methods on the same object (DBConnectionPool). Therefore, at any given time, only one of those methods can be called.

When the data source connection pool runs out of connections calls to DBConnectionPool.getPooledConnection() block waiting for a connection to be returned. Calls to DBConnectionPool.releasePooledConnections() then have to wait for the calls to DBConnectionPool.getPooledConnection to unblock.

Because the calls to return a connection are waiting for the calls to get a connection no connections will ever be returned and therefore no connections will ever be given causing the dead lock.

Your assistance is appreciated.

Here is example of the code:

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to look into wait() and notifyAll(). You should use wait() in the getting the connection if no connections are available. This will release the lock and block until another thread with the lock calls notifyAll().

Your synchronized method gets a lock on the instance of the DBConnectionPool that the thread is using to invoke the method. However inside of the method you are accessing a static array of DBConnectionPools - so the code could be trying to allocate a connection from a pool you do not have a lock on. Of course this doesn't seem to be the whole class so things could be different in the real code. Which is a reason to post the entire class.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you're implementing your own ConnectionPool and you're obtaining Connections from a DataSource, where alternately you could register a Connection Pool like DBCP as your DataSource and other problems get managed by mature code.

I'm annoyed by Connection Pools. We have one written in house and it has never worked properly. My local dev environment is running DBCP with the old pool front end acting a facade to the DBCP DataSource. I look forward to kicking the old one out entirely.
 
Joe Urbanek
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help your answers confirmed what I was thinking, I just needed a few more opinions, I will look into wait() and notifyAll() methods and into DBCP.
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic