Java连接池最佳实践?
在厌倦了c3p0的持续锁定之后,我转向BoneCP为我的数据库提供一个替代连接池。我有一个每分钟处理7000个项目的服务器应用程序,需要将这些项目记录到我们的MySQL数据库中。我目前有100个工作线程,并已将我的池设置为:Java连接池最佳实践?,java,database,multithreading,connection-pooling,bonecp,Java,Database,Multithreading,Connection Pooling,Bonecp,在厌倦了c3p0的持续锁定之后,我转向BoneCP为我的数据库提供一个替代连接池。我有一个每分钟处理7000个项目的服务器应用程序,需要将这些项目记录到我们的MySQL数据库中。我目前有100个工作线程,并已将我的池设置为: BoneCPConfig config = new BoneCPConfig(); config.setJdbcUrl("jdbc:mysql://"+Settings.MYSQL_HOSTNAME+"/"+Settings.MYSQL_DATABASE+"?a
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)
这些设置是否适用于此类应用程序?我这样问是因为在运行一两分钟后,我在试图调用池上的getConnection时遇到了boneCP异常。谢谢你的帮助
如果您有100个工作线程,为什么将池限制为50个连接(分区数x每个分区的最大连接数,即在您的情况下为5 x 10)
我没有正确地关闭连接吗
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());
}
}
看起来正常(但可能会按提示启用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) {}
}
您可以在实用程序类的静态方法中对上述调用进行分组
或者您可以使用Commons DbUtils,它已经做到了这一点。我认为这样做的目的是减少池中的连接。池中有100个连接吗?@beagleguy连接比什么少?您的池的大小必须能够处理并发请求。在网站上,并发请求通常低于登录用户的数量。但在你的情况下,如果你的100名员工可以同时忙碌,你不需要为每个人建立连接吗?我不知道你的应用程序和代码在做什么,你知道。我明白你在说什么。。工作线程基本上进行出站http连接,记录结果并将其存储在DB中,因此DB插入只是工作线程所做的工作的一部分,因此在正常流中不会有100个并发插入。我想知道这是否是因为我没有关闭我的结果集,我注意到在bonecp faq页面上,它提到你需要关闭结果集,因为conn.close()不会为你这样做。我要试一试。thanks@beagleguy啊,我明白了。那么您可能不需要100个连接。事实上,试着关闭所有获得的资源,看看情况如何(无论如何,这是一个很好的做法)。如果你仍在耗尽游泳池的能量,可以激活建议的标志。不客气。关闭池中的连接有什么意义。这似乎违反直觉。