Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jpa - Cannot use an EntityTransaction while using JTA

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...