- 论坛徽章:
- 0
|
概要
使用jdbc的数据源,同时有2个线程得到数据库连接,在各自执行DB操作时,发生异常 java.sql.SQLException: Closed Statement
相关代码
public class Connection {
/**
* The name used to identify the cache uniquely
*/
private static final String CACHE_NAME = "Cache";
/**
* Data Source Variable
*/
private static OracleDataSource ods = null;
/**
* Connection cache manager
*/
private static OracleConnectionCacheManager connMgr = null;
/**
* Connection*/
protected Connection conn = null;
static ResourceBundle res = ResourceBundle.getBundle("XXXXXXconn";
/**
* JDBC
*/
private static String url = res.getString("url";
/**
* userid
*/
private static String userid = res.getString("userid";
/**
* passwd
*/
private static String passwd = res.getString("passwd";
public Connection() {
try{
if(ods == null){
ods = new OracleDataSource();
ods.setURL(url);
ods.setUser(userid);
ods.setPassword(passwd);
//Enable cahcing
ods.setConnectionCachingEnabled(true);
//Set the cache name
ods.setConnectionCacheName(CACHE_NAME);
Properties properties = new Properties();
properties.setProperty("MinLimit", res.getString("poolmin");
properties.setProperty("MaxLimit", res.getString("poolmax");
connMgr = OracleConnectionCacheManager
.getConnectionCacheManagerInstance();
connMgr.createCache(CACHE_NAME, ods, properties);
System.out.println("ooledConnection Initialized:" + ods);
System.out.println("MaxPoolSize:" +
ods.getConnectionCacheProperties()
.getProperty("MinLimit");
System.out.println("MinPoolSize:" +
ods.getConnectionCacheProperties()
.getProperty("MaxLimit");
}
}catch(SQLException eq){
System.err.println(eq.getMessage());
};
}
public Connection getConn() throws SQLException {
try{
System.out.println("Active connection number:" +
connMgr.getNumberOfActiveConnections(CACHE_NAME));
System.out.println("Available connection number:" +
connMgr.getNumberOfAvailableConnections(CACHE_NAME));
connMgr.refreshCache(CACHE_NAME,
OracleConnectionCacheManager.REFRESH_INVALID_CONNECTIONS);
conn = ods.getConnection();
System.out.println("Got Pooled Connection:" + conn);
DefaultContext.setDefaultContext(new DefaultContext(conn));
}catch(SQLException eq){
System.err.println(eq.getMessage());
if(!conn.isClosed()) conn.close();
}
return(conn);
}
}
Exception message
[ 日期时间 JST] 000001dc ERROR:Closed Statement
[ 日期时间 JST] 000001c8 java.sql.SQLException: Closed Statement
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:20
at oracle.jdbc.driver.OracleStatement.ensureOpen(OracleStatement.java:3523)
at oracle.jdbc.driver.OracleCallableStatement.executeUpdate(OracleCallableStatement.java:4239)
at sqlj.runtime.ExecutionContext$OracleContext.oracleExecuteUpdate(ExecutionContext.java:1556)
经过:
有2个线程,它们之间没有任何关系
1 线程1开始执行getConn
1-1 执行refreshCache
1-2 执行ods.getConnection()
2 线程2开始执行getConn
2-1 执行refreshCache
2-2 执行ods.getConnection()
3 线程1和线程2同时取得数据库连接
4 Exception发生
log内容:
000001c8 SystemOut O Active connection number?0
000001c8 SystemOut O Available connection number?3
000001dc SystemOut O Active connection number?0
000001dc SystemOut O Available connection number?2
000001c8 SystemOut O Got Pooled Connection?oracle.jdbc.driver.LogicalConnection@60f4c701
000001dc SystemOut O Got Pooled Connection?oracle.jdbc.driver.LogicalConnection@66354461
※ 最后2个log输出时间完全相同。
补充 这个系统已经运行快2年了,是第一次出现这种bug。小弟才疏学浅,还请各位大侠不吝赐教。谢谢了 |
|