dbcp:打开准备好的语句的数量

发布于 2024-09-08 00:38:00 字数 174 浏览 11 评论 0原文

我的程序中出现 MaxOpenPreparedStatement 异常。我可以使用 getNumActive()/getNumIdle() 函数监视 GenericObjectPool 中的对象数量。我怎样才能获得连接&从 org.apache.commons.dbcp.BasicDataSource 对象准备语句池? 谢谢

I get MaxOpenPreparedStatement exception in my program. I can monitor number of objects in GenericObjectPool with getNumActive()/getNumIdle() functions. How can I get connection & prepared statement pools from org.apache.commons.dbcp.BasicDataSource object?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦晓ヶ微光ヅ倾城 2024-09-15 00:38:00

我不确定实际问题的答案,但打开的准备好的语句的最大允许数量通常相当高。因此,我强烈怀疑导致您提出这个问题的技术问题是 JDBC 代码没有按照以下 JDBC 习惯用法正确关闭 finally 块中所有打开的语句:

Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
// ...

try {
    connection = database.getConnection();
    preparedStatement = connection.prepareStatement(SQL_STRING);
    resultSet = preparedStatement.executeQuery();
    // ...
} finally {
    if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {}
    if (preparedStatement != null) try { preparedStatement.close(); } catch (SQLException ignore) {}
    if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}

I am not sure about the answer on the actual question, but the maximum allowable amount of opened preparedstatements is usually pretty high. So I strongly suspect that the technical problem causing you to ask this question is that the JDBC code is not properly closing all the opened statements in the finally block as per the following JDBC idiom:

Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
// ...

try {
    connection = database.getConnection();
    preparedStatement = connection.prepareStatement(SQL_STRING);
    resultSet = preparedStatement.executeQuery();
    // ...
} finally {
    if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {}
    if (preparedStatement != null) try { preparedStatement.close(); } catch (SQLException ignore) {}
    if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}
Spring初心 2024-09-15 00:38:00

DBCP 的 BasicDataSource 公开 maxOpenPreparedStatements 值数据源配置的。

此异常的存在似乎表明您打开了太多语句并且没有关闭它们:

由于连接通常一次只使用一两个语句,因此这主要用于帮助检测资源泄漏。

DBCP's BasicDataSource exposes the maxOpenPreparedStatements value that the datasource is configured with.

The presence of this exception seems to indicate that you are opening too many statements and not closing them however:

Since a connection usually only uses one or two statements at a time, this is mostly used to help detect resource leaks.

策马西风 2024-09-15 00:38:00

您也许能够通过继承 BasicDataSource 来掌握 DBCP 内部结构,然后重写 createPoolableConnectionFactory 并用您自己创建的工厂替换语句池工厂(因此能够跟踪)。

与此处的其他答案一样,这似乎表明准备好的语句处于打开状态 - 在这种情况下,即使您关闭准备好的语句池(或完全停止使用连接池),您也会遇到相同的问题,这可能使原来的问题更容易调试。

You might be able to get a hold of the DBCP internals by subclassing BasicDataSource, then overriding createPoolableConnectionFactory and replacing the statement pool factory with one you create yourself (and thus are able to track).

As with the other answers here, this would seem to indicate prepared statements are being left open - in which case you'll have the same problem even if you turn prepared statement pooling off (or stop using a connection pool at all), which might make the original problem a lot easier to debug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文