Java连接池最佳实践?

发布于 2021-01-29 19:44:07

在厌倦了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异常。谢谢您的帮助。

这是我在工作线程中用于db调用的代码,它不会在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)
关注者
0
被浏览
119
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    这些应用程序是否接受这些设置?我问是因为运行一两分钟后,尝试在池上调用getConnection时,我收到了boneCP异常。谢谢您的帮助。

    如果您有100个工作线程,为什么将池限制为50个连接(分区数x每个分区的最大连接数,即您的情况为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) {}
    }
    

    您可以将以上调用归为一个实用程序类的静态方法。

    或者,您可以DbUnit.closeQuietly(Connection, Statement, ResultSet)从Commons
    DbUtils使用已经完成的操作。



推荐阅读
知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看