@Transactional annotation, not starting transaction (Guice Persist)

I am trying to create a simple service class using the annotated @Transactional 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. The output of the magazine can be seen here: http://pastebin.com/Mh5BkqSC

Note the print on line 450:

Is the transaction active? Lying

As a result, 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 configure the application as follows:

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, TestServiceGuice is being created and I am using JpaPersistModulewhich should be what the wiring does @Transactional.

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

+4
2

@javax.transaction.Transactional @com.google.inject.persist.Transactional. -, Guice-Persist @Transactional API Java.

+6

@Transactional :

    entityManager.getTransaction().begin();
    entityManager.persist(entity);
    entityManager.getTransaction().commit();
+1

Source: https://habr.com/ru/post/1529962/


All Articles