具体来说,当我返回到池的连接时,dbcp(和其他连接池)是否会为我关闭语句和结果集?
或者我应该自己关闭这些?
memory-management connection-pooling jdbc apache-commons-dbcp
我在Tomcat中有一个DBCP连接池.问题是当连接短暂丢失时,应用程序被破坏,因为DBCP在连接时不会再尝试重新连接.我可以让DBCP自动重新连接吗?
我们使用org.apache.commons.dbcp.BasicDataSource作为Spring中数据源的父类.
是否可以在"url"或DBCP数据源中的另一个属性中指定要使用的模式?
我们看到我们的数据库连接因org.apache.commons.dbcp.BasicDataSource插槽写入错误而死的情况:
com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: socket write error
Run Code Online (Sandbox Code Playgroud)
当然,所有后续写入连接的尝试都会失败:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
Run Code Online (Sandbox Code Playgroud)
更新代码以捕获此类异常并在发生新连接时请求它,它再次失败.我是否正确怀疑DataSource#getConnection()每次呼叫时呼叫实际上并没有提供新的连接?是不是只是重用现有的连接,这是关闭的?
如果我是正确的,什么是丢弃旧连接并请求新连接的正确方法?
编辑:这是我想知道的更简洁的版本:
Connection c1, c2;
c1 = DatabaseManager.getConnection();
// c1.close() not called
c2 = DatabaseManager.getConnection();
Run Code Online (Sandbox Code Playgroud)
"c1 == c2"是真实的陈述吗?或者分配了两个连接?如果是后者,那么像这样的代码表示"连接池泄漏":
Connection c1;
c1 = DatabaseManager.getConnection();
// c1.close() not called
c1 = DatabaseManager.getConnection();
Run Code Online (Sandbox Code Playgroud) 我使用以下设置在JBoss EAP服务器中部署了Spring应用程序:
<bean:bean id="userDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<bean:property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<bean:property name="url" value="jdbc:oracle:thin:@10.8.1.5:1521:DB"/>
<bean:property name="username" value="WEBDB"/>
<bean:property name="password" value="WEBDB"/>
</bean:bean>
Run Code Online (Sandbox Code Playgroud)
如何配置连接池的最小和最大大小?
BasicDataSource的任何参考或任何最佳实践都将有很大帮助.
spring database-connection connection-pooling apache-commons-dbcp
我的项目设置是 -
这是我的应用程序的日志,它捕获与数据库的交互.
2013-01-29 15:52:21,549 DEBUG http-bio-8080-exec-3 org.springframework.jdbc.core.JdbcTemplate - Executing SQL query [SELECT id from emp]
2013-01-29 15:52:21,558 DEBUG http-bio-8080-exec-3 org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
2013-01-29 15:52:31,878 INFO http-bio-8080-exec-3 jdbc.connection - 1. Connection opened org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
2013-01-29 15:52:31,878 DEBUG http-bio-8080-exec-3 jdbc.connection - open connections: 1 (1)
2013-01-29 15:52:31,895 INFO http-bio-8080-exec-3 jdbc.connection - 1. Connection closed org.apache.commons.dbcp.DelegatingConnection.close(DelegatingConnection.java:247)
2013-01-29 15:52:31,895 DEBUG http-bio-8080-exec-3 jdbc.connection - open connections: none
2013-01-29 15:52:41,950 INFO http-bio-8080-exec-3 jdbc.connection - 2. …Run Code Online (Sandbox Code Playgroud) java database-connection connection-pooling spring-jdbc apache-commons-dbcp
MysqlConnectionPoolDataSource和C3p0,BoneCP或dbcp库之间的连接池有什么区别?我不明白为什么使用库,如果mysql连接器给出连接池.
I have read through various Stackover flow Questions and contents on the web on similar problem. However, I couldnt find useful hints that would allow me to narrow down on my problem. Here is my usecase which results in this error.
2 entities Campus and Programs --> One-to-many relation from Campus to Program and One-to-one from Program to Campus.
i am trying to create multiple programs associated with campuses. Each insert will create a new program with same details and …
java mysql hibernate spring-transactions apache-commons-dbcp
如果我们执行getConnection(),则使用DBCP中的BasicDataSource,在finally块中我们关闭连接,它确实会返回到池的连接,或者是否关闭连接.我正在检查的代码片段就是这个
try {
Connection conn1 = getJdbcTemplate().getDataSource()
.getConnection();
//Some code to call stored proc
} catch (SQLException sqlEx) {
throw sqlEx;
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex1) {
throw ex1;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在检查BasicDataSource的源代码,我到达了这个连接的包装类.
private class PoolGuardConnectionWrapper extends DelegatingConnection {
private Connection delegate;
PoolGuardConnectionWrapper(Connection delegate) {
super(delegate);
this.delegate = delegate;
}
public void close() throws SQLException {
if (delegate != null) {
this.delegate.close();
this.delegate = null;
super.setDelegate(null);
}
}
Run Code Online (Sandbox Code Playgroud)
委托对象,如果是java.sql.Connection类型.包装器代码调用委托的close方法,该方法将关闭集合而不是将连接返回到池.这是DBCP的已知问题,还是我错误地阅读了源代码请告诉我.
谢谢
java database-connection connection-pooling apache-commons-dbcp
我有一个简单的webapp,它从tomcat JDBC数据源获取连接.为了跟踪连接使用情况,我打算在打开和关闭连接时实现日志记录.记录应该打印这样的东西.
20151230143623.947[Thread-3] INFO [DataSourceManager:19] Opened connection identified by id : BlahBlahBlah1
20151230143623.947[Thread-3] INFO [DataSourceManager:19] Closed connection identified by id : BlahBlahBlah1
Run Code Online (Sandbox Code Playgroud)
我的开放和关闭方法是这样的.
Connection openConnection(String JNDILookupName) throws Exception {
Connection connection = DataSourceManager.getConnection(JNDILookupName);
logDBOperation("Opened", connection.toString());
return connection;
}
Connection closeConnection(String JNDILookupName) throws Exception {
connection.close();
logDBOperation("Closed", connection.toString());
}
void logDBOperation(String operation, String connecitonName){
logger.info(operation+" connection identified by id : "+connectionName);
}
Run Code Online (Sandbox Code Playgroud)
在这里,我使用Log connection.toString()作为Connection的唯一名称.但我想知道是否有更好的方法来做到这一点.