在厭倦了c3p0的常量鎖定之後,我轉向BoneCP來爲我的數據庫提供備用連接池。我有一個服務器應用程序,每分鐘處理大約7000個項目,並且需要將這些項目記錄到我們的MySQL數據庫中。我現在有100個工作線程,並建立了我的游泳池就像這樣:Java Connection Pooling最佳實踐?
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
是這樣的應用程序可以接受的那些設置?我在問,因爲在運行一兩分鐘後,我在嘗試撥打getConnection
時遇到了BoneCP異常。謝謝您的幫助。
這是我在工作線程中用於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)
我認爲這個想法是在游泳池中減少連接。你會在游泳池有100個連接嗎? – James 2010-09-04 23:32:07
@beagleguy比什麼連接少?您的池必須具有處理*併發*請求的大小。在網站上,併發請求通常低於登錄用戶的數量。但就你而言,如果你的100名員工可以同時忙於*,你是不是每個人都需要一個連接?我不知道你的應用和你的代碼在做什麼,你做的。 – 2010-09-04 23:59:20
我看到你在說什麼..工作線程基本上做出站http連接,記錄結果並將它們存儲在數據庫中,因此數據庫插入只是工作線程的一部分,因此不會有100個併發插入正常流程。 我不知道是不是因爲我沒有關閉我的結果集,而我注意到在bonecp faq頁面上它提到你需要關閉結果集,因爲conn.close()不會爲你做。我會給它一個鏡頭。謝謝 – James 2010-09-05 03:07:20