使用Hibernate实体管理器但连接未关闭且内存未释放

时间:2017-05-08 11:09:54

标签: java hibernate

我发现em.close()和emf.close()没有关闭连接,看到内存没有重新密封。

有两个关于会话的相关帖子,但不是实体经理。 How to force hibernate to release memory once the session is closed? Closing Hibernate Connection

是否有任何以正确方式关闭连接的示例?

public class EntityManagerHelper {

private static final EntityManagerFactory emf;

static {
    // pass those parameters via JVM properties or environment variables
    final Map<String, String> config = new HashMap<>();
    config.put("javax.persistence.jdbc.password",
            Optional.ofNullable(System.getenv("PG_PASSWORD"))
                    .orElseThrow(() -> new RuntimeException("PG_PASSWORD is not provided")));
    config.put("javax.persistence.jdbc.user",
            Optional.ofNullable(System.getenv("PG_USER"))
                    .orElseThrow(() -> new RuntimeException("PG_USER is not provided")));
    final String fullURL = Optional.ofNullable(System.getenv("PG_CONNECTION_STRING"))
                    .orElseThrow(() -> new RuntimeException("PG_CONNECTION_STRING is not provided"));
    config.put("javax.persistence.jdb.url", fullURL);
    config.put("hibernate.connection.url", fullURL);
    config.put("connection.provider_class", "org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider");
    config.put("hibernate.c3p0.acquire_increment", "1");
    config.put("hibernate.c3p0.idle_test_period", "3");

    config.put("hibernate.c3p0.min_size",
            Optional.ofNullable(System.getenv("POOL_MIN_SIZE")).orElse("1"));
    config.put("hibernate.c3p0.max_size",
            Optional.ofNullable(System.getenv("POOL_MAX_SIZE")).orElse("1"));
    config.put("hibernate.c3p0.timeout",
            Optional.ofNullable(System.getenv("TIMEOUT")).orElse("0"));

    config.put("hibernate.c3p0.max_statements", "50");
    config.put("hibernate.c3p0.acquireRetryAttempts", "1");
    config.put("hibernate.c3p0.acquireRetryDelay", "250");

    config.put("hibernate.show_sql", "true");
    config.put("hibernate.use_sql_comments", "true");
    config.put("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
    config.put("hibernate.current_session_context_class", "thread");

    config.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
    config.put("hibernate.cache.use_second_level_cache", "true");
    config.put("hibernate.cache.use_query_cache", "true");
    config.put("net.sf.ehcache.configurationResourceName", "/ehcacheAdmin.xml");
    try {
        emf = Persistence.createEntityManagerFactory("ConfigPersistence", config);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

public static EntityManager getEntityManager() {
    try {
        EntityManager em = emf.createEntityManager();
        return em;
    }catch(Exception e){
        throw new RuntimeException(e);
    }
}

public static void closeEntityManagerFactory() {
    emf.close();
}

public static void beginTransaction() {
    getEntityManager().getTransaction().begin();
}

public static void rollback() {
    getEntityManager().getTransaction().rollback();
}

public static void commit() {
    getEntityManager().getTransaction().commit();
}

}

修改: 目前它是只读的。

时会出现内存问题
entityManager = EntityManagerHelper.getEntityManager();
            fooConf = entityManager.find(Foo.class, fooId);

0 个答案:

没有答案