2017-05-08 24 views
1

我發現em.close()和emf.close()沒有關閉連接,看到內存未被重新密封。使用Hibernate實體管理器,但連接未關閉,內存不釋放

有兩個關於會話的相關文章,但不是實體經理。 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

你總是調用上的entityManger一個新的實例中的所有方法的內存問題來了。這可能無法工作..

不要使用靜態方法來操作entityManager,而是直接在事務性方法中使用它。只需使用static util方法獲取管理器:

EntityManager em = EntityManagerHelper.getEntityManager(); 

try{ 
    em.getTransaction().begin(); 

    // operations 

    em.getTransaction().commit(); 
} catch(Exception e){ 
    em.getTransaction().rollback(); 
} finally{ 
    em.close(); 
} 
+0

謝謝!這是一個問題,但問題不在於事務方法,因爲它們沒有被調用。我將編輯我的問題。 –

+0

好的,即使你調用entityManager.close();在那之後找到方法..它的開放性? –

+0

我刪除所有的靜態,我無法找到連接打開,但內存仍未釋放。 –