【问题标题】:Spring JDBC Template with BoneCP Connection Pooling Configuration具有 BoneCP 连接池配置的 Spring JDBC 模板
【发布时间】:2017-01-09 08:08:28
【问题描述】:

我有一个使用 MySQL 作为数据库的 java spring 应用程序。 MySQL 连接限制约为 12k。但是我当前的应用程序仅包含基本配置,因此我的 MySQL 挂起,而客户端连接仅达到 1500 到 1600 连接。有谁知道如何为我当前的 MySQL 配置 BoneCP 连接池。

配置

<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value=“{URL}”/>    
<property name="username" value=“{USER}”/>
<property name="password" value=“{PASSWORD}”/>
<property name="idleConnectionTestPeriodInMinutes" value="60"/>
<property name="idleMaxAgeInMinutes" value="240"/>
<property name="maxConnectionsPerPartition" value="30"/>
<property name="minConnectionsPerPartition" value="10"/>
<property name="partitionCount" value="3"/>
<property name="acquireIncrement" value="5"/>
<property name="statementsCacheSize" value="100"/>
<property name="releaseHelperThreads" value="3"/>
<property name="connectionTestStatement" value="Select    1"/>
</bean>

【问题讨论】:

  • 使用您当前的配置,您的应用程序将使用最多 90 个连接,您确定您的代码正在关闭连接(这会将连接返回到池中)吗?
  • 谢谢。我正在使用 Spring JDBC 模板,所以我认为 Spring JDBC 模板在内部处理紧密连接。我的负载均衡器下有 10 台服务器,所以请告诉我配置的值是什么

标签: java mysql jdbc spring-jdbc bonecp


【解决方案1】:

请查看此更新的配置,谢谢。

  public static void main(String[] args) {

    BoneCP connectionPool = null;
    Connection connection = null;

    try {
        // load the database driver (make sure this is in your classpath!)
        Class.forName("org.hsqldb.jdbcDriver");
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }

    try {

        // setup the connection pool
        BoneCPConfig config = new BoneCPConfig();
        config.setJdbcUrl("jdbc:hsqldb:mem:test");
        // url
        // specific to
        // your database
        config.setUsername("sa");
        config.setPassword("");
        config.setMinConnectionsPerPartition(50);
        config.setMaxConnectionsPerPartition(200);
        config.setPartitionCount(50);
        config.setLazyInit(false);
        connectionPool = new BoneCP(config); // setup the connection pool
        for (int i = 0; i < 200000; i++) {
            connection = connectionPool.getConnection(); // fetch a //connection
            System.out.println("Connection" + connection);

            if (connection != null) {
                System.out.println("Number of Connection successful " + i);
                Statement stmt = connection.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS"); // do
                // something
                // with
                // the
                // connection.
                while (rs.next()) {
                    System.out.println(rs.getString(1)); // should print out
                    // "1"'
                }

            }
        }
        connectionPool.shutdown(); // shutdown connection pool.
    } catch (SQLException ex) {
        ex.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

}

【讨论】:

  • 谢谢。但我不知道以下字段的值idleConnectionTestPeriodInMinutes, idleMaxAgeInMinutes, maxConnectionsPerPartition, minConnectionsPerPartition, partitionCount, acquireIncrement, statementsCacheSize, releaseHelperThreads mysql 连接限制高达 12000。
  • 你能分享你当前的配置吗?
  • &lt;property name="idleConnectionTestPeriodInMinutes" value="60"/&gt; &lt;property name="idleMaxAgeInMinutes" value="240"/&gt; &lt;property name="maxConnectionsPerPartition" value="30"/&gt; &lt;property name="minConnectionsPerPartition" value="10"/&gt; &lt;property name="partitionCount" value="3"/&gt; &lt;property name="acquireIncrement" value="5"/&gt; &lt;property name="statementsCacheSize" value="100"/&gt; &lt;property name="releaseHelperThreads" value="3"/&gt;
  • 这根本不能回答问题;您只是在转储一些随机配置代码。
  • 我已将 MinConnectionsPerPartition 增加到 50,将 MaxConnectionsPerPartition 增加到 100,将 PartitionCount 增加到 50,这将使我从池中获得大约 12000 个连接,出于测试目的,我正在使用 hsql 数据库,并且由于 BoneCp 的文档很弱可以'没有找到其他方法,你可以用mysql数据库检查并测试这个,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 2017-01-26
  • 1970-01-01
  • 1970-01-01
  • 2014-11-18
  • 2013-12-13
相关资源
最近更新 更多