我正在使用从Connection
获得的oracle.ucp.jdbc.PoolDataSource
执行查询。当关闭数据库资源时,只有当池重用连接或JVM退出时,似乎这些查询打开的数据库游标才会关闭。
我正在通过运行以下查询来检查打开的游标。
select SQL_TEXT, v$open_cursor .* from v$open_cursor
where
user_name = 'ME'
AND
CURSOR_TYPE = 'OPEN'
当代码到达第一次休眠时,查询在第二次休息时返回select 1 from dual a
,select 1 from dual b
等等......一旦JVM退出我的查询,就不会返回任何结果。
因此,在执行另一个查询或JVM退出之前,游标似乎保持打开状态。关闭statement
/ connection
/ resultSet
时,我如何确保游标可靠关闭?
pooledDataSource.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
pooledDataSource.setURL("jdbc:oracle:thin:@//" + host + ":1521/" + service);
pooledDataSource.setUser(user);
pooledDataSource.setPassword(pw);
pooledDataSource.setInitialPoolSize(1);
pooledDataSource.setMinPoolSize(1);
pooledDataSource.setMaxPoolSize(1);
pooledDataSource.setAbandonedConnectionTimeout(60);
pooledDataSource.setConnectionWaitTimeout(60);
pooledDataSource.setInactiveConnectionTimeout(60);
try (Connection con = pooledDataSource.getConnection();
PreparedStatement statement = con.prepareStatement("select 1 from dual a");
ResultSet rs = statement.executeQuery()) {
System.out.println();
}
System.out.println("opened a");
Thread.sleep(5000);
try (Connection con = pooledDataSource.getConnection();
PreparedStatement statement = con.prepareStatement("select 1 from dual b");
ResultSet rs = statement.executeQuery()) {
System.out.println();
}
System.out.println("opened b");
Thread.sleep(5000);
try (Connection con = pooledDataSource.getConnection();
PreparedStatement statement = con.prepareStatement("select 1 from dual c");
ResultSet rs = statement.executeQuery()) {
System.out.println();
}
System.out.println("opened c");
Thread.sleep(5000);