使用keepStatement()时,JOOQ似乎不会关闭连接

时间:2014-04-15 19:27:39

标签: java jooq

当我使用keepStatement()方法重用预准备语句时,我在使用java.sql.DataSource和连接池时耗尽了连接...

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();
    }
}

结果(连接池中允许的最多10个开放连接):

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

如果我从上面的代码中删除.keepStatement(true),一切都运行没有问题(连接在执行后关闭)。

我调用了close()的{​​{1}}方法,但似乎没有关闭连接。 我还需要关闭其他东西吗?

(JOOQ版本3.3.1)

1 个答案:

答案 0 :(得分:1)

这是jOOQ 3.3.1和之前版本中的错误,感谢您的报告。我已创建issue #3191以尽快解决此问题(由于jOOQ 3.4.0,3.3.3,3.2.6)。在jOOQ中使用keepStatement()时似乎没有正确实现DataSource语义。

与此同时,我建议您不要使用此功能。