Cannot use an EntityTransaction while using JTA

andreaxi picture andreaxi · Jun 6, 2012 · Viewed 37.2k times · Source

I'm receiving this error:

javax.servlet.ServletException: java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.

While trying to use JPA and JAVAEE, Glassfish.

My persistence.xml file is as follow:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="acmeauction">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/MySQLJDBCResource</jta-data-source>
        <class>it.uniroma3.acme.auction.model.User</class>
        <class>it.uniroma3.acme.auction.model.Auction</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/acmeauction"/>
            <property name="javax.persistence.jdbc.user" value="user"/>
            <property name="javax.persistence.jdbc.password" value="password"/>
        </properties>
    </persistence-unit>
</persistence>

What i'm trying to do is to persist an object (User), in this way:

@ManagedBean
public class UserRepository implements Serializable{

    @PersistenceUnit
    EntityManagerFactory emf;

    @PersistenceContext
    private EntityManager em; 

    private static UserRepository instance; 

    /**
     * Gives back the singleton UserRepository singleton. 
     */
    public static UserRepository getInstance() {
        if (instance==null) {
            instance = new UserRepository(); 
        }
        return instance; 
    }

    private UserRepository() {
        emf = Persistence.createEntityManagerFactory("acmeauction");
        em = emf.createEntityManager();             
    }

    /** 
     * Save and persist a new User. 
     */
    public void save(User user) {
        em.getTransaction().begin(); 
        em.persist(user);
        em.getTransaction().commit(); 
    }
}

While if it try to use UserRepository from a simple Java application, it works correctly.

Thanks in advance, AN

Answer

Joe Almore picture Joe Almore · Feb 12, 2014

You are not supposed to use em.getTransaction().begin(); nor em.getTransaction().commit();, these instructions are to be used with RESOURCE_LOCAL transaction type.

In your case the transaction is managed by the container, in the first use of the EntitiyManager in your method, the container checks whether there is an active transaction or not, if there is no transaction active then it creates one, and when the method call ends, the transaction is committed by the container. So, at the end your method should look like this:

public void save(User user) {
    em.persist(user);
}

The container takes care of the transaction, that is JTA.