0

I was just thinking that in a large web application where tens of connections are made to database for every client using the application, there is a possibility that very few of the connections are gone unclosed. Though it is strictly up to us that we must close all connections explicitly but, you know, there may be mistakes. If this happens then after some days we may find that our connection pool has reached its maximum size and the application is not running being unable to get a connection.

Actually I am facing this problem for one of my projects and whenever the connection pool reaches its maximum size I just restart the server which is not good.

I am curious to know how to handle this issue in a better manner and I want to know that is there such a thing that I can close all the unused active connections in my connection pool explicitly in a regular interval e.g every 72 hours using java code ?

I am talking about Java/J2ee technologies and my server is Glassfish Application Server2.1.

EDIT: I am using the connection pool that Glassfish Application server provides within, not any third party connection pool and using this pool using JNDI DataSource.

need your suggestions........

Thanks

mukund
  • 2,866
  • 5
  • 31
  • 41

2 Answers2

0

You can use MiniConnectionPoolManager. It is a lightweight standalone Java JDBC connection pool manager.

Take a look at this article http://www.h2database.com/javadoc/org/h2/jdbcx/JdbcConnectionPool.html. and the dispose() method.

There are a lot of jdbc drivers supported http://www.source-code.biz/miniconnectionpoolmanager/

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
0

If I were to address this issue in the absence of a good connection pooler that provides this out of box, I would do it as follows:

  1. One entry point for my application to get connections.
  2. Decorate the connection obtained from the self-maintained-pool, associate a timer set to a configured value (to close unused active connections) and return this decorated connection to the clients of the pool.
  3. If the close method is called before the timer fires, cancel the timer, delegate the call to the wrapped connection.
  4. If the close method is not called when the timer fires, close the wrapped connection and invalidate the state of this decorated connection object so that it will throw IllegalStateException if someone tries to work with it again.

My two cents!

Edit: C3P0 has unreturnedConnectionTimeout that can be configured to handle these kinds of connections taken out from the pool but not returned through a close() call.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • `in the absence of a good connection pooler` : Do you mean that if I use a connection pool like DBCP or C3P0, then I can do what I want. If yes, then how? And what does it mean decorating a connection...Could you please explain it & provide me some link. – mukund Sep 06 '12 at 06:38
  • In case of C3P0, the property `unreturnedConnectionTimeout` is configured to handle connections taken from the pool but returned it. More details can be found at http://www.mchange.com/projects/c3p0/index.html#maxIdleTime. – Vikdor Sep 06 '12 at 06:56