繁体   English   中英

具有BoneCP连接池配置的Spring JDBC模板

[英]Spring JDBC Template with BoneCP Connection Pooling Configuration

我有一个将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>

请查看此更新的配置。

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM