【问题标题】:javax.persistence.TransactionRequiredException: no transaction is in progressjavax.persistence.TransactionRequiredException:没有正在进行的事务
【发布时间】:2012-07-29 04:46:54
【问题描述】:

我有一项使用 HQL 将许多对象存储到数据库中的服务。但我有这个例外

javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:789)
at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
at $Proxy42.flush(Unknown Source)
at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy42.flush(Unknown Source)
at org.skyway.spring.util.dao.AbstractJpaDao.flush(AbstractJpaDao.java:90)
at sun.reflect.GeneratedMethodAccessor16.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy52.flush(Unknown Source).........

问题可能是 DB 上存在大量活动的并发操作。 有什么建议吗?

【问题讨论】:

    标签: hibernate persistence hql jpa-2.0 atomikos


    【解决方案1】:

    您需要在每个服务方法中使用会话工厂开始和提交 Hibernate 事务,或使用 AOP(面向方面​​编程)为您的方法自动启动、提交/回滚事务。请参考this

    【讨论】:

    • 我所有的方法都有'@transactional'注解
    • 好的,但是问题肯定是因为没有附加事务。你的 spring bean 上下文中有 吗?
    • 是的:<tx:annotation-driven transaction-manager="transactionManager"/>
    • 开始一切正常,但在几次成功后我得到了异常
    • @ClaudioPomo 虽然您已经接受了我的回答,但我很好奇确切的错误是什么,因为您说您已经遵循了我的所有建议。
    猜你喜欢
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 2021-09-21
    • 2017-07-24
    相关资源
    最近更新 更多