使用JTA时不能使用EntityTransaction [英] Cannot use an EntityTransaction while using JTA
问题描述
我收到此错误:
javax.servlet.ServletException:java.lang.IllegalStateException:
异常描述:在使用JTA时不能使用EntityTransaction。
尝试使用JPA和JAVAEE时,Glassfish。
我的persistence.xml文件如下:
< ?xml version =1.0encoding =UTF-8?>
< persistence version =2.0xmlns =http://java.sun.com/xml/ns/persistencexmlns:xsi =http://www.w3.org/2001/XMLSchema-实例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>
<属性>
< property name =javax.persistence.jdbc.drivervalue =com.mysql.jdbc.Driver/>
< property name =javax.persistence.jdbc.urlvalue =jdbc:mysql:// localhost:3306 / acmeauction/>
< property name =javax.persistence.jdbc.uservalue =user/>
< property name =javax.persistence.jdbc.passwordvalue =password/>
< / properties>
< / persistence-unit>
< /余辉>
我试图做的是以这种方式持久化一个对象(User):
@ManagedBean
public class UserRepository实现Serializable {
@PersistenceUnit
EntityManagerFactory EMF;
@PersistenceContext
私人EntityManager em;
私有静态UserRepository实例;
/ **
*返回单身UserRepository单身人士。
* /
public static UserRepository getInstance(){
if(instance == null){
instance = new UserRepository();
}
返回实例;
private UserRepository(){
emf = Persistence.createEntityManagerFactory(acmeauction);
em = emf.createEntityManager();
}
/ **
*保存并保留一个新用户。
* /
public void save(User user){
em.getTransaction()。begin();
em.persist(user);
em.getTransaction()。commit();
$ b 虽然如果它试图从简单的Java使用UserRepository应用程序,它正常工作。
在此先感谢,
AN
解决方案正如错误所述,如果您使用JTA进行事务处理,则需要使用JTA。
使用JTA UserTransaction来开始/提交事务,或使用RESOURCE_LOCAL持久性单元和非jta数据源。
请参阅
http://en.wikibooks.org/wiki/Java_Persistence/Transactions
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
解决方案 As the error states, if you are using JTA for transactions, you need to use JTA.
Either use JTA UserTransaction to begin/commit the transaction, or use a RESOURCE_LOCAL persistence unit and non-jta DataSource.
See,
http://en.wikibooks.org/wiki/Java_Persistence/Transactions
这篇关于使用JTA时不能使用EntityTransaction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文