0

mysqlデータベースへの接続プールjdbc接続にBoneCPを使用しています。私はRESTアプリケーションでbonecpの例を使用しています。

すべてのRESTリクエストが接続プールを開いたり閉じたりしている場合、これはそもそも接続プールのポイントを打ち負かしませんか?

コードは次のとおりです。

 public class ExampleJDBC {

/** Start test
 * @param args none expected.
 */
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"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
        config.setUsername("sa"); 
        config.setPassword("");
        config.setMinConnectionsPerPartition(5);
        config.setMaxConnectionsPerPartition(10);
        config.setPartitionCount(1);
        connectionPool = new BoneCP(config); // setup the connection pool

        connection = connectionPool.getConnection(); // fetch a connection

        if (connection != null){
            System.out.println("Connection successful!");
            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 e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
} 
 }
4

1 に答える 1

2

はい。アプリケーションの(通常の)ライフサイクルで接続プールを複数回開いたり閉じたりするという目的は無効になります。代わりに、事前に確立されたプールから毎回接続をフェッチする必要があります。

于 2012-06-07T23:29:44.380 に答える