在ejb 3.0 jboss 6环境中,我有一个名为DBInterface的bean,注入到许多dao类中以执行sql查询。 DBInterface bean将数据源作为字段变量注入。 DBInterface bean中的所有方法都从注入的数据源获取数据库连接,并在处理db-calls之后关闭连接。在运行应用程序时,经过一段时间后,我得到的sql异常说不能创建数据库连接。我正在关闭finally块中每个方法调用的连接。我在哪里犯错误?我在jboss中使用ejb 3.0。 问候 VEJB 3.0托管bean注入和db连接关闭
public class DBInterface {
@Resource(mappedName = "java:ora.ds", type = DataSource.class)
private static DataSource dataSource;
protected Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
e.printstacktrace();
}
}
public void method1() {
Connection connection = null;
try {
connection = getConnection();
...............
db codes
.................
} catch (SQLException e) {
logger.error(e);
throw new DBException("sql exception ", e);
} finally {
try {
closeResources(rs, statement, connection);
} catch (SQLException e) {
logger.error(e);
throw new DBException("sql exception ", e);
}
}
}
public void closeResources(ResultSet rs, Statement st, Connection con)
throws SQLException {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
}
}
不要注入一个数据源到静态字段... – 2013-04-26 15:46:34
也一定要关闭单独的try-catch块中的资源 – 2013-04-26 15:54:04
是的你是对的,我不再使用静态数据源。对不起,我在这里粘贴了旧的测试代码。 你是什么意思由单独的try-catch块?我有没有其他的错误,我没有看到这个代码? – 2013-04-26 18:54:44