When I use the keepStatement() method to reuse a prepared statement, I'm running out of connections when using java.sql.DataSource and connection pooling...

private void keepStatmentTest()
{
    Settings jooqSettings = = new Settings();
    jooqSettings.setRenderSchema(false);
    InitialContext ic = new InitialContext();
    DataSource ds = (DataSource) ic.lookup(dataSourceName);
    DSLContext create = DSL.using(dataSource, SQLDialect.MYSQL, jooqSettings)

    for (int i = 0; i < 100; i++)
    {
        ResultQuery<Record1<String>> q = create.select(ROLES.NAME)
            .from(ROLES)
            .where(ROLES.ID.equal(DSL.param("p", 0)))
            .keepStatement(true);
        q.bind("p", 1);
        q.execute();
        q.bind("p", 2);
        q.execute();
        q.close();
    }
}

Results into (max. 10 open connections allowed in the connection pool):

org.jooq.exception.DataAccessException: Error getting connection from data source org.apache.tomcat.dbcp.dbcp.BasicDataSource@22fc4de4
    at org.jooq.impl.DataSourceConnectionProvider.acquire(DataSourceConnectionProvider.java:89)
    at org.jooq.impl.DefaultExecuteContext.connection(DefaultExecuteContext.java:426)
    at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:278)
    ...
Caused by: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object
    at org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:114)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    at org.jooq.impl.DataSourceConnectionProvider.acquire(DataSourceConnectionProvider.java:86)
    ... 23 more
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
    at org.apache.tomcat.dbcp.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1167)
    at org.apache.tomcat.dbcp.dbcp.AbandonedObjectPool.borrowObject(AbandonedObjectPool.java:79)
    at org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)
    ... 25 more

If I remove the .keepStatement(true) from the code above, everything runs without problems (connections are close after execution).

I have called the close() method of the ResultQuery, but it seems not to close the connection. Do I need to close something other too?

(JOOQ version 3.3.1)

有帮助吗?

解决方案

This is a bug in jOOQ 3.3.1 and previous versions, thanks for reporting. I have created issue #3191 to fix this as soon as possible (due for jOOQ 3.4.0, 3.3.3, 3.2.6). It appears as though the keepStatement() semantics is not correctly implemented when using a DataSource with jOOQ.

In the mean time, I suggest you don't use this feature.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top