`
maozilee
  • 浏览: 252690 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

Java Connection Pooling最佳练习

阅读更多

After getting fed up with c3p0's constant locking I'm turning to boneCP for an alternative Connection Pool for my Database. I have a server app that processes around 7,000 items per minute and needs to log those items into our mysql database. I currently have 100 worker threads and have set up my Pool like such:

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
 

Are those acceptable settings for such an app? I'm asking because after a minute or two into running I was getting boneCP exceptions when trying to call getConnection on the pool. thanks for the help.

here is the code I was using for the db calls in my worker threads, it can't failing on the dbConn = this.dbPool.getConnection() line. Am I not closing connections properly?

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

This is the error I was seeing;
 [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)
 
最佳答案:

Are those acceptable settings for such an app? I'm asking because after a minute or two into running I was getting boneCP exceptions when trying to call getConnection on the pool. thanks for the help.

If you have 100 workers, why do you limit the pool to 50 connections (number of partitions x max number of connections per partition i.e. 5 x 10 in your case)?

Am I not closing connections properly?

Looks ok (but maybe enable connectionWatch as hinted to see what the warning is about exactly). Personally, I close all the resources I use, including statement and result sets. Just in case, here is the idiom I use:

You could group the above calls in a static method of a utility class.

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) {}
}
 

Or you could use DbUnit.closeQuietly(Connection, Statement, ResultSet) from Commons DbUtils that already does this.

分享到:
评论

相关推荐

    Using ODBC Connection Pooling with CDatabase (under MFC)使用

    本篇文章将深入探讨如何在MFC中利用ODBC连接池(Connection Pooling)功能,以提高应用程序的性能和资源效率。 首先,我们需要理解ODBC连接池的概念。连接池是一种管理数据库连接的技术,它预先创建并维护一定数量...

    DBConnectionManager.rar_Connection Pooling_DBConnectionManag_DBC

    2. **连接获取与释放**:当应用需要执行数据库操作时,调用`getConnection()`方法从连接池中获取一个已建立的连接。操作完成后,通过`returnConnection()`方法将连接返回到池中,以便后续使用,而不是直接关闭。 3....

    c3p0 - JDBC3 Connection and Statement Pooling

    关于jdbc连接池的一个第三方类 可以实现连接池的使用

    database_resident_connection_pooling.zip_oracle

    数据库驻留连接池(Database Resident Connection Pooling,简称DRCP)是Oracle 11g版本引入的一项重要特性,旨在优化数据库连接管理和资源利用率,从而提高系统性能和响应速度。DRCP通过在数据库内部维护一个连接池...

    UFLDL Exercise: Convolution and Pooling 卷积和池化

    UFLDL(Understanding Deep Learning via Linearized Models)是一个学习资源,提供了深入理解深度学习概念的练习,其中"Convolution and Pooling"这一部分主要介绍了卷积层和池化层在神经网络中的应用。 卷积层是...

    java+oracle+驱动

    PreparedStatement可以防止SQL注入攻击,而Connection Pooling(如C3P0、HikariCP等)则能有效地管理数据库连接,提高性能。 总的来说,"java的oracle数据库驱动jar包"是Java应用程序连接Oracle数据库的基础,它...

    javajdbc宠物商店-Mysql数据库_Java项目jdbc_java宠物项目_数据库代码_

    9. **性能优化**: 通过批处理(Batch Processing)、连接池(Connection Pooling)和使用预编译的SQL(PreparedStatement)等方式,可以显著提升JDBC应用的性能。 10. **数据库连接关闭**: 在完成数据库操作后,应...

    max pooling with dropout

    在matlab上实现了具备max pooling和dropout功能的convolutional neural network,并附带MNIST数据集,可以直接运行测试。 时间有限,仅有1层convolution和1层pooling。

    Java连接各种数据库的实例.rar

    - Connection Pooling:连接池管理数据库连接,提高效率,避免资源浪费。 - Transaction Management:利用Connection的事务管理功能,确保数据一致性。 通过这个压缩包中的"Java连接各种数据库的实例.txt",我们...

    oracle 数据库java驱动 ojdbc

    - Connection Pooling:使用连接池提高性能和资源利用率,如C3P0、HikariCP等。 - PreparedStatement缓存:提高预编译SQL的执行效率。 - 分页查询:通过ROWNUM伪列和子查询实现分页。 - 并行执行:通过多线程...

    mysql-connector-java-5.1.6-bin.jar

    7. **性能优化**:通过配置参数,如使用连接池(Connection Pooling),可以显著提升性能。 8. **兼容性**:此版本5.1.6针对MySQL 5.1.x版本进行了优化,确保了良好的兼容性。 在使用`mysql-connector-java-5.1.6-...

    java包(spring.jar等)

    `commons-dbcp.jar`是Apache Commons Database Connection Pooling的JAR文件,它是Java数据库连接池的一个实现。数据库连接池在多用户环境中提高了性能和效率,通过复用已建立的数据库连接,减少了创建和销毁连接的...

    Java操作Access数据库示例

    - 考虑使用连接池(Connection Pooling)管理数据库连接,避免频繁创建和销毁连接,提高应用程序效率。 - Access数据库适用于小型项目或个人使用,对于大型企业级应用,更推荐使用支持JDBC的其他数据库,如MySQL、...

    Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition

    work, we equip the networks with another pooling strategy, “spatial pyramid pooling”, to eliminate the above requirement. The new network structure, called SPP-net, can generate a fixed-length ...

    java与不同数据库的连接

    1. Connection Pooling(连接池):在大型应用中,为了提高性能和效率,通常使用连接池来管理数据库连接。例如,C3P0、HikariCP、Apache DBCP等。 2. JPA(Java Persistence API)和Hibernate:JPA是Java EE提供的...

    40_解释 ROI Pooling 和 ROI Align 的区别1

    ROI Pooling 和 ROI Align 的区别 在目标检测网络 Faster R-CNN 和实例分割网络 Mask R-CNN 中,对候选框 ROI 中的固定数目特征的提取非常重要。 ROI Pooling 和 ROI Align 都是用于提取 ROI 中的特征,但是它们有...

    mysql-connector-java-8.0.20.zip

    2. **Connection Pooling**:支持数据库连接池,如C3P0、HikariCP等,这有助于提高性能和资源利用率,特别是在高并发应用中。 3. **SSL/TLS Support**:提供安全的加密连接,保护数据在传输过程中的安全。 4. **...

    java版dataset datatable源码

    4. **Connection Pooling**:例如C3P0、HikariCP、DBCP等连接池实现,提供高效的数据库连接管理。 5. **DataBinding**:将数据对象绑定到UI组件,如Swing或JavaFX中的表格控件。 6. **Constraints**:使用Java的校验...

    tutorial-connection-pooling:一个演示 DBCP(数据库连接池)的 Maven 项目

    3. **Maven项目结构**:在“tutorial-connection-pooling-master”这个压缩包中,我们可以预见到一个标准的Maven项目结构,包括`pom.xml`配置文件、源代码目录`src/main/java`以及测试代码目录`src/test/java`。...

Global site tag (gtag.js) - Google Analytics