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

时间:2017-01-09 08:08:28

标签: java mysql jdbc spring-jdbc bonecp

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

1 个答案:

答案 0 :(得分:-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();
            }
        }
    }
}

}