Java连接池最佳实践?

15

因为受够了c3p0的频繁锁定,我决定使用BoneCP作为数据库连接池的替代方案。我的服务器应用程序每分钟需要处理约7,000个项目并将这些项目记录到我们的MySQL数据库中。我目前有100个工作线程,并已设置我的连接池如下:

BoneCPConfig config = new BoneCPConfig();
      config.setJdbcUrl("jdbc:mysql://"+Settings.MYSQL_HOSTNAME+"/"+Settings.MYSQL_DATABASE+"?autoReconnectForPools=true" ); 
      config.setUsername(Settings.MYSQL_USERNAME); 
      config.setPassword(Settings.MYSQL_PASSWORD);
      config.setMinConnectionsPerPartition(5);
      config.setMaxConnectionsPerPartition(10);
      config.setPartitionCount(5);
      config.setAcquireIncrement(5);
      connectionPool = new BoneCP(config); // setup the connection pool

这些设置对于这样的应用程序是否可接受?我之所以问是因为在运行了一两分钟后,当尝试在池上调用 getConnection 时,我会遇到 BoneCP 异常。感谢您的帮助。

这是我在工作线程中用于数据库调用的代码,它不可能在 dbConn = this.dbPool.getConnection() 行失败。我没有正确关闭连接吗?

private void insertIntoDb() {
    try {  
        Connection dbConn = this.dbPool.getConnection();

        try {
            PreparedStatement ps3 = dbConn.prepareStatement("INSERT IGNORE INTO test_table1 SET test1=?, test2=?, test3=?");
            ps3.setString(1, "some string");
            ps3.setString(2, "some other string");
            ps3.setString(3, "more strings");
            ps3.execute();
            ps3.close();

            PreparedStatement ps4 = dbConn.prepareStatement("INSERT IGNORE INTO test_table2 SET test1=?, test2=?, test3=?");
            ps4.setString(1, "some string");
            ps4.setString(2, "some other string");
            ps4.setString(3, "more strings");
            ps4.execute();
            ps4.close();

        } catch(SQLException e) {
            logger.error(e.getMessage());
        } finally {
            try {
                dbConn.close();
            } catch (SQLException e) {
                logger.error(e.getMessage());
            }
        }
    } catch(SQLException e) {
        logger.error(e.getMessage());
    }
}

这是我看到的错误:

 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.
 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.
 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.
 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.
 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.
 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.
 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.

ERROR pool-2-thread-39 2010-09-04 13:36:19,798 com.test.testpackage.MyTask - null
java.sql.SQLException
    at com.jolbox.bonecp.BoneCP.getConnection(BoneCP.java:381)
1个回答

15

这些设置对于这样的应用程序是否可接受?我问这个问题是因为在运行一两分钟后,当尝试在池中调用getConnection时,我得到了boneCP异常。感谢您的帮助。

如果你有100个工作人员,为什么要将池限制为50个连接(分区数乘以每个分区的最大连接数,即您的情况下为5 x 10)?

我没有正确关闭连接吗?

看起来没问题(但也许启用connectionWatch以查看警告的确切内容)。就我个人而言,我关闭所有我使用的资源,包括语句和结果集。以防万一,这是我使用的习惯用法:

Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

try {
    conn = pool.getConnection();
    pstmt = conn.prepareStatement(SOME_SQL);
    pstmt.setFoo(1, foo);
    ...
    rs = pstmt.executeQuery();
    ...
} finally {
    if (rs != null) try { rs.close(); } catch (SQLException quiet) {}
    if (pstmt != null) try { pstmt.close(); } catch (SQLException quiet) {}
    if (conn != null) try { conn.close(); } catch (SQLException quiet) {}
}

你可以将上述调用分组放在实用类的静态方法中。
或者,你可以使用Commons DbUtils中的DbUnit.closeQuietly(Connection, Statement, ResultSet),它已经实现了这一功能。

我认为这个想法是要在池中减少连接数。你会在池中有100个连接吗? - James
@beagleguy 没有比什么更少的连接?您的池大小必须足以处理并发请求。在网站上,并发请求通常低于已登录用户的数量。但是在您的情况下,如果您的100个工作人员可以同时忙碌,难道您不需要每个人都有一个连接吗?我不知道您的应用程序和代码正在做什么,而您知道。 - Pascal Thivent
我懂你的意思...工作线程基本上是进行出站http连接、记录结果并将其存储在数据库中,因此数据库插入仅是工作线程所做的一部分,因此正常流程中不会有100个并发插入。我想知道是否因为我没有关闭我的结果集,我注意到在bonecp faq页面上它提到需要关闭结果集,因为conn.close()不会为您执行此操作。我会试一试。谢谢。 - James
@beagleguy 哦,我明白了。那么你可能不需要100个连接。确实,尝试关闭所有已获取的资源,看看情况如何(这是一个好习惯)。如果您仍然耗尽池,可以激活建议的标志。不用客气。 - Pascal Thivent
在池中关闭连接有什么意义呢?这似乎是违反直觉的。 - Hobbyist

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接