gpt4 book ai didi

java - Hibernate - 每个操作的 ThreadLocal 与 EntityManager

转载 作者:行者123 更新时间:2023-11-30 08:34:11 27 4
gpt4 key购买 nike

我使用 Hibernate 5.1.0.Final、Guice、Jersey。我有创建 EntityManagerFactory 的 HibernateModule并管理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()用于清除持久性上下文并强制从数据库查询最新数据。 GenericDAO接收注入(inject) EntityManager来自 hibernate 模块。主要方法:

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);
}
}

以前我曾尝试实现一个提供新 EntityManager 的解决方案在每个数据库操作上,但它会导致一些副作用,例如“将分离的实体传递给持久化”。

重用 EntityManager 是好的做法吗?来自 ThreadLocal<EntityManager> ?这种实现有什么潜在的缺点吗?

最佳答案

它应该可以正常工作。 Spring Framework 正在为 EntityManager 使用 ThreadLocal 类。来自引用:

Spring makes it easy to create and bind a Session to the current thread transparently

如果您想重新使用 EntityManager 实例,您应该记住 JPA 提示、PersistenceContextTypeFlushModeType

关于java - Hibernate - 每个操作的 ThreadLocal<EntityManager> 与 EntityManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38990608/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com