Description of the issue: JpaLocalTxnInterceptor is inconsistent as it uses the "JpaPersistService emProvider" field to begin a transaction but uses the "UnitOfWork unitOfWork" to finish it. When the "UnifOfWork" binding of the JpaPersistModule is overriden, this causes incoherent states and eventually crashes.
Here is a class (I used nested class to keep a single java file) that reproduce the issue:
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Persistence;
@Singleton
public static class EntityManagerProvider implements Provider<EntityManager>, UnitOfWork {
private final ThreadLocal<EntityManager> entityManager = new ThreadLocal<EntityManager>();
@Inject
private Provider<EntityManagerFactory> emf;
@Override
public EntityManager get() {
return entityManager.get();
}
From guillaume.polet on June 05, 2013 04:23:12
Description of the issue: JpaLocalTxnInterceptor is inconsistent as it uses the "JpaPersistService emProvider" field to begin a transaction but uses the "UnitOfWork unitOfWork" to finish it. When the "UnifOfWork" binding of the JpaPersistModule is overriden, this causes incoherent states and eventually crashes.
Here is a class (I used nested class to keep a single java file) that reproduce the issue: import javax.inject.Inject; import javax.inject.Provider; import javax.inject.Singleton; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Persistence;
import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.persist.PersistService; import com.google.inject.persist.Transactional; import com.google.inject.persist.UnitOfWork; import com.google.inject.persist.jpa.JpaPersistModule; import com.google.inject.util.Modules;
public class TestGuiceUnitOfWork {
@
Entity public static class MyEntity {@
Id@
GeneratedValue(strategy = GenerationType.AUTO) private Long id;@
Column(name = "name") private String name;public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
@
Singleton public static class EntityManagerFactoryProvider implements Provider<EntityManagerFactory>, PersistService { private EntityManagerFactory emFactory;@
Override public EntityManagerFactory get() { return emFactory; }@
Override public void start() { this.emFactory = Persistence.createEntityManagerFactory("my-pu"); }@
Override public void stop() { emFactory.close(); emFactory = null; } }@
Singleton public static class EntityManagerProvider implements Provider<EntityManager>, UnitOfWork { private final ThreadLocal<EntityManager> entityManager = new ThreadLocal<EntityManager>();@
Inject private Provider<EntityManagerFactory> emf;@
Override public EntityManager get() { return entityManager.get(); }@
Override &nbs...Original issue: http://code.google.com/p/google-guice/issues/detail?id=753