2016-08-17 13 views
2

私は、Hibernate 5.1.0.Final、Guice、Jerseyを使用しています。Hibernate - ThreadLocal <EntityManager>と操作ごとのEntityManager

public class HibernateModule extends AbstractModule { 

    private static final ThreadLocal<EntityManager> ENTITY_MANAGER_CACHE = new ThreadLocal<EntityManager>(); 

    @Provides @Singleton 
    public EntityManagerFactory provideEntityManagerFactory(@Named("hibernate.connection.url") String url, 
     @Named("hibernate.connection.username") String userName, 
     @Named("hibernate.connection.password") String password, 
     @Named("hibernate.hbm2ddl.auto") String hbm2ddlAuto, 
     @Named("hibernate.show_sql") String showSql) { 

     Map<String, String> properties = new HashMap<String, String>(); 
     properties.put("hibernate.connection.driver_class", "org.postgresql.Driver"); 
     properties.put("hibernate.connection.url", url); 
     properties.put("hibernate.connection.username", userName); 
     properties.put("hibernate.connection.password", password); 
     properties.put("hibernate.connection.pool_size", "1"); 
     properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); 
     properties.put("hibernate.hbm2ddl.auto", hbm2ddlAuto); 
     properties.put("hibernate.show_sql", showSql); 
     properties.put("hibernate.cache.use.query_cache", "false"); 
     properties.put("hibernate.cache.use_second_level_cache", "false"); 

     return Persistence.createEntityManagerFactory("db-manager", properties); 
    } 

    @Provides 
    public EntityManager provideEntityManager(EntityManagerFactory entityManagerFactory) { 

     EntityManager entityManager = ENTITY_MANAGER_CACHE.get(); 

     if (entityManager == null || !entityManager.isOpen()) 
      ENTITY_MANAGER_CACHE.set(entityManager = entityManagerFactory.createEntityManager()); 

     entityManager.clear(); 

     return entityManager; 
    } 
} 

entityManager.clear()がdabataseから最新のデータを照会するために永続コンテキストと力をクリアするために使用されています。私はEntityManagerFactoryを作成し、EntityManagerインスタンスを管理HibernateModuleを持っています。 GenericDAOは、HibernateModuleから注入されたEntityManagerを受け取る。主な方法:

public class GenericDAOImpl<T> implements GenericDAO<T> { 

    @Inject 
    protected EntityManager entityManager; 

    private Class<T> type; 

    public GenericDAOImpl(){} 

    public GenericDAOImpl(Class<T> type) { 
     this.type = type; 
    } 

    @Override 
    public void save(T entity) { 
     entityManager.getTransaction().begin(); 
     entityManager.persist(entity); 
     entityManager.getTransaction().commit(); 
    } 

    @Override 
    public T find(Long entityId) { 
     return (T) entityManager.find(type, entityId); 
    } 
} 

は、以前私が実装するすべてのDB操作に新しいEntityManagerを提供するソリューションを試してみましたが、それは「永続化するために渡された一戸建てエンティティ」のようないくつかの副作用をもたらします。

EntityManagerThreadLocal<EntityManager>から再利用することをお勧めしますか?この実装の潜在的な欠点はありますか?

答えて

2

正しく動作するはずです。 Spring FrameworkはEntityManagerThreadLocalクラスを使用しています。参照から:

春が作成し、現在の スレッドにセッションをバインドすることが容易になり、透過

あなたはEntityManagerインスタンスを再使用する場合は、JPAのヒント、PersistenceContextTypeFlushModeTypeについて覚えておく必要があります。

関連する問題