Java Connection Pooling Best Practices?
-
30-09-2019 - |
質問
C3P0の一定のロックにうんざりした後、私はデータベースの代替接続プールのためにBonECPに頼っています。 1分あたり約7,000個のアイテムを処理し、それらのアイテムをMySQLデータベースにログインする必要があるサーバーアプリがあります。現在、100のワーカースレッドがあり、そのようなプールをセットアップしています。
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
そのようなアプリのそれらの許容可能な設定はありますか?私は尋ねています。なぜなら、1、2分後に実行してから、電話をかけようとしたときにBoneCPの例外を取得していたからです getConnection
プールで。助けてくれてありがとう。
これが私がワーカースレッドでDB呼び出しに使用していたコードです、それは失敗することはできません dbConn = this.dbPool.getConnection()
ライン。接続を適切に閉じていませんか?
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());
}
}
これは私が見たエラーです:
[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)
解決
そのようなアプリのそれらの許容可能な設定はありますか?私は尋ねています。なぜなら、1、2分後にランニングに参加してから、プールでgetConnectionを呼び出そうとしたときにBoneCPの例外を取得していたからです。助けてくれてありがとう。
100人の労働者がいる場合、なぜプールを50の接続に制限するのですか(パーティションの数x最大の接続数、つまりあなたの場合は5 x 10)?
接続を適切に閉じていませんか?
大丈夫そうです(しかし、おそらく有効です connectionWatch
警告が正確に何であるかを確認するために示唆されたように)。個人的には、ステートメントや結果セットなど、使用するすべてのリソースを閉じます。念のため、私が使用するイディオムは次のとおりです。
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) {}
}
上記の呼び出しは、ユーティリティクラスの静的方法でグループ化できます。
または使用することができます DbUnit.closeQuietly(Connection, Statement, ResultSet)
すでにこれを行っているコモンズdbutilsから。