2

I work with java 8 and apache tomcat.

Executing a project, I get the following error

java.sql.SQLException: Cannot get a connection, pool error Timeout waiting for idle object
    at org.apache.tomcat.dbcp.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:142)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:809)

Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
    at org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:438)
    at org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:348)
    at org.apache.tomcat.dbcp.dbcp2.PoolingDataSource.getConnection(PoolingD

I read in another question that

The DB is down or unreachable. The connection pool (which is set to 100 max active) is out of connections.

Now, I would like to know how to recover the database to initialize the connections.

EDITED

The follow code is repeated a lot of times

final InitialContext contextoInicial = new InitialContext();
 final Context contexto = (Context)contextoInicial.lookup("");
 final DataSource ds = (DataSource)contexto.lookup("");
  Connection  conn = ds.getConnection();

How can I reuse the connection and verify if there is

EDITED

java.sql.SQLException: Cannot get a connection, pool error Timeout waiting for idle object
    at org.apache.tomcat.dbcp.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:142)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:753)
    at es.magrama.ayudaapi.servlets.JSON.validaRega(JSON.java:249)
    at es.magrama.ayudaapi.servlets.JSON.doPost(JSON.java:125)
    at es.magrama.ayudaapi.servlets.JSON.doGet(JSON.java:89)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
    at org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:438)
    at org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:348)
    at org.apache.tomcat.dbcp.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:134)
    ... 28 more
3
  • can you please show the code. Commented Jan 16, 2020 at 7:17
  • Please post the full exception stacktrace. Either you aren't closing connections (which exhausts the pool, as closing connections is what returns them to the pool) or the connection pool cannot create connections. The fact your aren't getting your connection in a try-with-resources block is an indication you might be forgetting to close it when you're done. Commented Jan 16, 2020 at 10:24
  • I edited the exception Commented Jan 16, 2020 at 10:27

1 Answer 1

3

Exception shows "Connection pool exhausted" problem causing because of connection are not getting closed and returned to pool or It is because of max active connections defined in data source are small enough to handle current HIGH load of the system. I suspect that the max active count parameter (here maxActive=50) may causing the less number of connection to handle the application load at peak time.

Also in your code (as per your comments )I see you have initialized data source lots of time. I would suggest you initialize it once and then use it for connection, like singleton object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.