JPA当前没有交易活动 [英] JPA No transaction is currently active

查看:106
本文介绍了JPA当前没有交易活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将JPA与EclipseLink实施结合使用。

Using JPA with EclipseLink implementation.

代码:

try{
    if(!em.getTransaction().isActive())
        em.getTransaction().begin(); 
    System.out.println(2);
    em.persist(currentUser);
    System.out.println(3);
    if (em.getTransaction().isActive()){
        System.out.println("IS ACTIVE");
    } else {
        System.out.println("NO ACTIVE");
    }
    em.getTransaction().commit();
    System.out.println(4);
    } catch (Exception e){
    completed = false;
    em.getTransaction().rollback();
    System.out.println("ERROR: " + e.getMessage());
}

错误:

INFO: persistOne - Enter
INFO: 2
INFO: 3
INFO: IS ACTIVE
INFO: [EL Warning]: 2012-01-06 14:45:59.221--UnitOfWork(12492659)--java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: com.maze.model.UserDetail@d52ea.

WARNING: #{accountController.performRegister}: java.lang.IllegalStateException: 
Exception Description: No transaction is currently active
javax.faces.FacesException: #{accountController.performRegister}: java.lang.IllegalStateException: 
Exception Description: No transaction is currently active
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:662)
Caused by: javax.faces.el.EvaluationException: java.lang.IllegalStateException: 
Exception Description: No transaction is currently active
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 32 more
Caused by: java.lang.IllegalStateException: 
Exception Description: No transaction is currently active
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.rollback(EntityTransactionImpl.java:122)
    at com.maze.service.UserService.persistOne(UserService.java:63)
    at com.maze.controller.request.AccountController.performRegister(AccountController.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 33 more

奇怪的是,交易是活跃的,但接下来发生的事情是不活动的错误。

The weird thing is that the Transaction is active but the next thing happening is an error of inactivity.

编辑
EntityManager Singleton:
public class EntityManagerSingleton {

EDIT EntityManager Singleton: public class EntityManagerSingleton {

private EntityManagerSingleton(){
}

private static class EMSingletonHolder{
    private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("Maze");
    private static final EntityManager em = emf.createEntityManager();
}

public static EntityManager getInstance(){
    return EMSingletonHolder.em;
}

*获取em实例:*

*Getting the em Instance: *

public abstract class AbstractService {
protected EntityManager em;

public AbstractService(){
    em = EntityManagerSingleton.getInstance();
}
}

所有其他服务扩展了AbstractService

All other services extends the AbstractService

推荐答案

根据您的日志判断,您正在使用GlassFish。我假设你也在使用JTA。也许您还可以提供获取EntityManager实例的代码?此外,使用System.out语句进行调试是过去的事情,使用调试器,否则您的代码很难阅读。另外,你的缩进是错误的。

Judging by your log, you are using GlassFish. I assume you are also using JTA. Maybe you can provide also the code that obtains the EntityManager instance? Also, debugging with System.out statements is something from the past, use a debugger, otherwise your code is hard to read. In addition, your indentation is wrong.

首先,如果你使用的是JTA,请看看这个问题:

First of all, if you are using JTA, take a look at this question:

调用entityManager.getTransaction()时的EJBException

从接受的答案中引用:


获取对此的引用是非法的与Java EE托管上下文中的EntityManager关联的EntityTransaction实例。从
EntityManager.getTransaction()的Java EE API文档:

It is illegal to obtain a reference to the EntityTransaction instance associated with the EntityManager in a Java EE managed context. From the Java EE API documentation of EntityManager.getTransaction():

返回资源级EntityTransaction对象。可以串行使用
EntityTransaction实例来开始和提交
多个交易。

Return the resource-level EntityTransaction object. The EntityTransaction instance may be used serially to begin and commit multiple transactions.



Returns:
    EntityTransaction instance  Throws:
    IllegalStateException - if invoked on a JTA entity manager

这就是为什么如果你期望任何帮助,提供有关获得实体经理的代码/注入的信息是很重要的。

That is why if you expect any help it is important to provide information about the code/injection through which you obtain the entity manager.

此外,为了代码可读性,你不应该多次调用em.getTransaction();
您应该首先调用Transaction对象并重复使用它,如下所示:

Also, for the sake of code readability, you should not have multiple calls to em.getTransaction(); You should assign your first call to a Transaction object and reuse it, kinda like this:

//GOOD cause readable
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(someObject);
tx.commit();

而不是

 //BAD cause hard to read
 em.getTransaction().begin();
 em.persist(someObject);
 em.getTransaction().commit(); //NO!

这篇关于JPA当前没有交易活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆