Glassfish上的JDBC缓存

Glassfish上的JDBC缓存,jdbc,glassfish,Jdbc,Glassfish,我在Glassfish上的一个长时间运行的后台进程上遇到了OutOfMemoryError。内存分析表明,抛出错误时,堆的50%专用于com.mysql.JDBC4ResultSet(28.1%)和com.mysql.jdbc.StatementImpl(22.1%)的实例 我的代码是基于这样的假设构建的,即一旦JDBC对象失去作用域,它们就会被垃圾收集,但情况显然并非如此。我最初是用JPA做的,但是内存负载爆炸了,所以我恢复到JDBC,但是我仍然得到了巨大的内存泄漏,包括对JDBC语句和结果集

我在Glassfish上的一个长时间运行的后台进程上遇到了
OutOfMemoryError
。内存分析表明,抛出错误时,堆的50%专用于
com.mysql.JDBC4ResultSet
(28.1%)和
com.mysql.jdbc.StatementImpl
(22.1%)的实例

我的代码是基于这样的假设构建的,即一旦JDBC对象失去作用域,它们就会被垃圾收集,但情况显然并非如此。我最初是用JPA做的,但是内存负载爆炸了,所以我恢复到JDBC,但是我仍然得到了巨大的内存泄漏,包括对JDBC语句和结果集的引用

所以我想知道Glassfish是否正在缓存这些查询,以及如何禁用它们,因为数据对象非常大,我真的不需要缓存它们

下面是我的代码结构,包括一个类和两个方法(为brievity修改)


由于您已经从实体管理器中打开了连接,从而完全控制了您自己手中的资源,因此您应该在同一个
try
块的
finally
块中明确地关闭它们,因为它们是根据以下标准JDBC习惯用法获得的

// Declare resources.
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;

try {
    // Acquire resources.
    connection = getConnectionSomehow();
    statement = connection.createStatement();
    resultSet = statement.executeQuery(sql);

    // ...
} finally {
    // Close resources in reversed order.
    if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
    if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
    if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
}

否则,它们将保持打开状态,并确实在服务器和数据库中泄漏。

您将一如既往地依赖BalusC。我不知道结果集和语句需要关闭。谢谢
// Declare resources.
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;

try {
    // Acquire resources.
    connection = getConnectionSomehow();
    statement = connection.createStatement();
    resultSet = statement.executeQuery(sql);

    // ...
} finally {
    // Close resources in reversed order.
    if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
    if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
    if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
}