4

I am getting always the same exception , i have tried my best by doing searches to get the solution but without benefit.

My exception is:

SEVERE: Exception : org.apache.tomcat.jdbc.pool.PoolExhaustedException: [pool-2-thread-1] Timeout: Pool empty. Unable to fetch a connection in 30 seconds, none available[size:100; busy:100; idle:0; lastwait:30000].
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:672)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.getConnection(ConnectionPool.java:187)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:128)
    at com.xxxxx.dbmanager.utils.DBManager.getConnection(DBManager.java:81)
    at com.xxxxx.dbmanager.utils.SQLManager.<init>(SQLManager.java:36)
    at ...
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

I have tried to increase the maximum number of connections in mysql DB.

I am just wondering if a servlet running in the background and creating threads could be the problem? And how can i kill my threads when tasks are done ?

Appreciating your help !!

user1597915
  • 41
  • 1
  • 1
  • 3

1 Answers1

4

From the error message it seems like your code is connecting to the database and never closing its connections.

The way that the pool recovers connections is that application code has to call close on the connection when it's done with it, which returns the connection back to the pool. Otherwise as far as the pool knows all those connections are still busy.

My first guess at the cause is that your code is not even trying to close the connections. Some people (especially those coming from PHP, where it's common for this to get taken care of automatically) are surprised to find out that this is something that they have to do themselves.

Alternatively, some error condition could be causing an exception to be thrown where your code is not handling the exception correctly, and ends up skipping the call to close, this can happen when more than one jdbc resource gets closed in a single finally block. For more details see this answer.

Or maybe, your application is just busy and those threads are all trying to get work done. Check if the threads are doing anything: add logging to tell what they're doing, and check if there is network traffic between your application and the database.

For how to kill threads see Servlet “has started a thread but failed to stop it” - memory leak in Tomcat. I don't think that's immediately relevant to the lost-connection issue, but it would be good to know. If you don't get this right it's possible to keep an old instance of the application alive in the background while your new instance is running.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276