Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Transactional annotation not starting transaction (Guice Persist)

I am trying to create a simple service class with a @Transactional annotated method using Guice Persist:

public class TestServiceImpl implements TestService {

    @Inject
    private Provider<EntityManager> entityManager;

    @Override
    @Transactional
    public Long createEvent(String name) {
        Event event = new Event(name);

        System.out.println("Is transaction active? " + entityManager.get().getTransaction().isActive() );

        entityManager.get().persist(event);

        return event.getId();
    }

}

Unfortunately no transaction is created by Guice Persist. Log output can be seen here: http://pastebin.com/Mh5BkqSC

Note the print on line 450:

Is transaction active? false

The result is that nothing is stored in the database:

mydb=# \d
                 List of relations
 Schema |        Name        |   Type   |  Owner   
--------+--------------------+----------+----------
 public | events             | table    | postgres
 public | hibernate_sequence | sequence | postgres
(2 rows)

mydb=# select * from events;
 id | name 
----+------
(0 rows)

mydb=# 

I setup the application in the following way:

public static void main(String[] args) {
    Injector injector = Guice.createInjector(new TestModule(), new JpaPersistModule("postgresPersistenceUnit"));

    PersistService persistService = injector.getInstance(PersistService.class);
    persistService.start();

    TestService serviceToTest = injector.getInstance(TestService.class);

    Long id = serviceToTest.createEvent("test");

    System.out.println("Id: " + id);
}

As you can see TestService is created by Guice, and I am using the JpaPersistModule, which should be what does the @Transactional wiring.

I have created a small Maven project demonstrating the problem: https://github.com/uldall/guice-persist-error

like image 396
uldall Avatar asked Mar 04 '14 09:03

uldall


People also ask

What happens if one @transactional annotated method is calling another @transactional annotated method on the same object instance?

If you call a method with a @Transactional annotation from a method with @Transactional within the same instance, then the called methods transactional behavior will not have any impact on the transaction.

What does @transactional do in hibernate?

The @Transactional annotation is the metadata that specifies the semantics of the transactions on a method. We have two ways to rollback a transaction: declarative and programmatic. In the declarative approach, we annotate the methods with the @Transactional annotation.


2 Answers

The problem was resolved by switching from @javax.transaction.Transactional to @com.google.inject.persist.Transactional. Apparently Guice-Persist doesn't support the @Transactional annotation from the Java Transaction API.

like image 151
uldall Avatar answered Oct 05 '22 22:10

uldall


I have used without @Transactional annotation and it worked:

    entityManager.getTransaction().begin();
    entityManager.persist(entity);
    entityManager.getTransaction().commit();
like image 40
Justinas Jakavonis Avatar answered Oct 05 '22 23:10

Justinas Jakavonis