我正在尝试在处理后关闭ResultSet
和CallableStatement
,但只有Connection
正在关闭,而其他两个则没有。
final String procedureCall = "{call GET_PAWS_PERSON_DETAILS(?, ?)}";
try {
//Get Connection instance from dataSource
connection = dataSource.getConnection();
callableStatement = connection.prepareCall(procedureCall);
callableStatement.setString(1, userName);
callableStatement.registerOutParameter(2, OracleTypes.CURSOR);
//Call Stored Procedure
callableStatement.executeUpdate();
// get cursor and cast it to ResultSet
resultSet = (ResultSet) callableStatement.getObject(2);
logger.debug("reslt" + resultSet.getFetchSize());
// loop it like normal
while (resultSet.next()) {
personDTO.setPersonID(resultSet.getString(1));
personDTO.setFirstName(resultSet.getString(2));
personDTO.setLastName(resultSet.getString(3));
personDTO.setFullName(resultSet.getString(4));
personDTO.setEmail(resultSet.getString(5));
}
}catch (SQLException e) {
e.printStackTrace();
} finally {
if(resultSet != null)
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(callableStatement != null)
try {
callableStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(connection != null)
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
我正在使用此代码。关闭后只需打印Connection
,Statment
和CallableStament
。结果是
Connection : null
Resultset : org.apache.tomcat.dbcp.dbcp.DelegatingResultSet@6f1f6b1e
Callable statement : oracle.jdbc.driver.OracleStatementWrapper@77b1b790.
由于此问题,数据库连接池会增加大小。
请给我解决方案。
在Tomcat server.xml中
<Resource driverClassName="oracle.jdbc.OracleDriver" maxActive="4" maxIdle="10" maxWait="5000" name="jdbc/epaws" global="jdbc/epaws"
password="polusneu" type="javax.sql.DataSource" url="jdbc:oracle:thin:@192.168.1.60:1521:coeusnew"
username="polusneu" validationQuery="select 1 from dual" testOnBorrow="true" removeAbandoned="true"
removeAbandonedTimeout="55"/>
的applicationContext.xml
<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/epaws"/>
<property name="resourceRef" value="true"/>
<property name="cache" value="false"/>
<property name="lookupOnStartup" value="false"/>
<property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>
答案 0 :(得分:0)
首先关闭:
关闭对象将不将该对象引用设置为null
。所以你可以在收盘后仍然有对象。但奇怪的是,你的联系应该是null
之后......但无论如何。
关闭finally
区块是有效的程序 - 但老派。 Java 7引入了一个非常棒的新功能,称为try-with-resource
,它可以处理关闭 - 即使出现错误也是如此。然后代码看起来像
try(Connection con = dataSource.getConnection();
CallableStatement callable = con.prepareCall(procedureCall)) {
callableStatement.setString(1, userName);
callableStatement.registerOutParameter(2, OracleTypes.CURSOR);
//Call Stored Procedure
callableStatement.executeUpdate();
// get cursor and cast it to ResultSet
try(ResultSet resultSet = (ResultSet) callableStatement.getObject(2)) {
logger.debug("reslt" + resultSet.getFetchSize());
// loop it like normal
while (resultSet.next()) {
personDTO.setPersonID(resultSet.getString(1));
personDTO.setFirstName(resultSet.getString(2));
personDTO.setLastName(resultSet.getString(3));
personDTO.setFullName(resultSet.getString(4));
personDTO.setEmail(resultSet.getString(5));
}
} //Don't need another catch
} catch(SQLException e) {
e.printStackTrace();
}