google / google/guice

JpaLocalTxnInterceptor is inconsistent. It uses both UnitOfWork and JpaPersistService

Open
#753 1 comment 0 reactions 0 assignees View on GitHub
Component-Persist imported
Dominant language
Java
Stars
12.7k
Forks
1.7k
Avg merge
11m
Merged PRs (30d)
2

Description

_From [guillaume.polet](https://code.google.com/u/104043893669511382727/) 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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.