I can not make a transaction in the database

1

Good afternoon what happens is that my persistence.xml I have it configured with JTA and when trying to insert a data in the database it generates the following error.

Caused by: java.lang.IllegalStateException: Exception Description: Can not use an EntityTransaction while using JTA.

    
asked by Alexander Gil Tafur 14.06.2016 в 20:08
source

1 answer

0

When you use JTA you can not use the traditional way to access the EntityManager , i.e.:

EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistence_unit);

or

@PersistenceUnit(unitName="persistence_unit")
private EntityManagerFactory factory;

EntityManager entityManager = factory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();

The above only works if you have in persistence.xml the

transaction-type="RESOURCE_LOCAL";

When using JTA, it is being specified that the container is the one that will provide the EntityManager , so the following should be used:

@PersistenceContext(unitName="persistence_unit")
private EntityManager entityManager;

And the container is responsible for injecting it.

If you want to solve your error, and do not need multiple contexts, simply change the persistence.xml

de

transaction-type="JTA"

a

transaction-type="RESOURCE_LOCAL"

More info ..

    
answered by 13.01.2017 в 03:16