根据这个问题,使用时guice-persist
,EntityManager
是事务范围的。如果我理解正确,这意味着EntityManager
将为每笔交易创建一个新的。使用时guice-persist
,建议使用JpaPersistModule
,它提供了所有的绑定,并简单地注入Provider<EntityManager>
到某个类中,如下所示:
public class ProjectDAO {
private final Provider<EntityManager> entityManagerProvider;
@Inject
public ProjectDAO(Provider<EntityManager> entityManagerProvider) {
this.entityManagerProvider = entityManagerProvider;
}
}
注意:在这个答案中,它说不EntityManager
应该直接注入,而是使用Provider<EntityManager>
,以避免这个问题,因此注入Provider<EntityManager>
. 此外,通过查看JpaPersistService的代码,EntityManager
实例存储在ThreadLocal
. 同时,@Transactional
注解及其JpaLocalTxnInterceptor
对应物应确保在每次事务后在字段.set()
上.remove()
调用。ThreadLocal<EntityManager>
现在,我已经尝试过了,每个线程都有自己的EntityManager
. 但是,它似乎并没有被删除并重新设置,而是被重复用于后续事务,即没有清除 Hibernate 的一级缓存。
这是一个完整的示例,它从两个不同的线程(按顺序,而不是并行)插入和删除一些实体,这导致一个线程具有陈旧信息:
项目(一个简单的实体)
@NamedQueries({
@NamedQuery(name = "project.findAll", query = "from project"),
@NamedQuery(name = "project.deleteByProjectName", query = "delete from project p where p.name = :project_name")
}
)
@Entity(name = "project")
public class Project {
@Id
@GeneratedValue
private Long id;
@Column(name="name")
private String name;
// ... getters/setters
}
项目DAO
public class ProjectDAO {
private final Provider<EntityManager> entityManagerProvider;
@Inject
public ProjectDAO(Provider<EntityManager> entityManagerProvider) {
this.entityManagerProvider = entityManagerProvider;
}
public void insert(Project project) {
entityManagerProvider.get().persist(project);
}
public List<Project> findAll() {
return entityManagerProvider.get()
.createNamedQuery("project.findAll", Project.class)
.getResultList();
}
public void delete(String projectName) {
entityManagerProvider.get()
.createNamedQuery("project.deleteByProjectName")
.setParameter("project_name", projectName)
.executeUpdate();
}
public Project findById(Long id) {
return entityManagerProvider.get().find(Project.class, id);
}
}
项目服务
public class ProjectService {
private final ProjectDAO projectDAO;
@Inject
public ProjectService(ProjectDAO projectDAO) {
this.projectDAO = projectDAO;
}
@Transactional
public void addProject(Project project) {
projectDAO.insert(project);
}
@Transactional
public List<Project> findAll() {
return projectDAO.findAll();
}
@Transactional
public void delete(String projectName) {
projectDAO.delete(projectName);
}
@Transactional
public Project findById(Long id) {
return projectDAO.findById(id);
}
public EntityManager getEntityManager() {
return projectDAO.getEntityManager();
}
}
主班
public class Start {
public static void main(String[] args) throws InterruptedException {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new JpaPersistModule("hibernatetesting"));
bind(ProjectService.class).in(Scopes.SINGLETON);
}
});
ProjectService projectService = injector.getInstance(ProjectService.class);
PersistService persistService = injector.getInstance(PersistService.class);
persistService.start();
// For the purpose of making transactions from different threads, we
// create two single threaded executors
ExecutorService executorService1 = Executors.newSingleThreadExecutor();
ExecutorService executorService2 = Executors.newSingleThreadExecutor();
// Execute a few queries from Thread 1
CountDownLatch countDownLatch1 = new CountDownLatch(1);
executorService1.execute(() -> {
System.out.println("TEST: " + Thread.currentThread().getName());
System.out.println("TEST: " +"EntityManager: " + projectService.getEntityManager().hashCode());
projectService.addProject(new Project("project1"));
projectService.addProject(new Project("project2"));
countDownLatch1.countDown();
});
countDownLatch1.await();
// Execute a few queries from Thread 2
CountDownLatch countDownLatch2 = new CountDownLatch(1);
executorService2.execute(() -> {
System.out.println("TEST: " + Thread.currentThread().getName());
System.out.println("TEST: " +"EntityManager: " + projectService.getEntityManager().hashCode());
projectService.addProject(new Project("project3"));
projectService.addProject(new Project("project4"));
//----
projectService.delete("project1");
//----
// project3 is not shown in this list
projectService.findAll().forEach(System.out::println);
countDownLatch2.countDown();
});
countDownLatch2.await();
// Execute a few more queries from Thread 1
CountDownLatch countDownLatch3 = new CountDownLatch(1);
executorService1.execute(() -> {
System.out.println("TEST: " + Thread.currentThread().getName());
System.out.println("TEST: " +"EntityManager: " + projectService.getEntityManager().hashCode());
projectService.addProject(new Project("project5"));
projectService.addProject(new Project("project6"));
// project3, which was deleted in Thread 2 is still visible in
// this EntityManager
// ----
Project project = projectService.findById(3L);
System.out.println("Project still exists " + project);
// ----
projectService.findAll().forEach(System.out::println);
countDownLatch3.countDown();
});
countDownLatch3.await();
}
}
pom.xml
...
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-persist</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.11.Final</version>
</dependency>
</dependencies>
...
持久性.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="hibernatetesting" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:testDB"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
1) 这是将 EntityManager 与 guice-persist 一起使用并解决不同线程可能具有不同状态的事实的常用方法吗?
2)如果没有,如何确保在每次事务后在 ThreadLocal 上重新设置 EntityManager?