从Spring 4.1.6升级到4.2.4并突然收到TransactionRequiredException [英] Upgraded from Spring 4.1.6 to 4.2.4 and suddenly getting TransactionRequiredException

查看:503
本文介绍了从Spring 4.1.6升级到4.2.4并突然收到TransactionRequiredException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从4.1.6升级到最新的Spring版本。发布到4.2.4.Release,突然之前所有的功能都顺利运行,现在抛出以下异常。

  javax.persistence.TransactionRequiredException:没有实际事务可用于当前线程的EntityManager  - 无法可靠地处理'persist'调用
在org.springframework.orm.jpa.SharedEntityManagerCreator $ SharedEntityManagerInvocationHandler .invoke(SharedEntityManagerCreator.java:277)〜[na:4.2.4.RELEASE]
at com.sun.proxy。$ Proxy51.persist(Unknown Source)〜[na:na]

Interceptor.class

 公共类MessageInterceptor实现EndpointInterceptor {

@Resource
private SaveMessageDO saveMessageDO;
$ b $ @Override
public boolean handleRequest(MessageContext messageContext,Object endpoint){
saveMessageDO.addMessage(messageContext.getRequest()));
返回true;

$ b @Override
public boolean handleResponse(MessageContext messageContext,Object endpoint){
return false;
}

@Override
public boolean handleFault(MessageContext messageContext,Object endpoint){
return false;

$ b @Override
public void afterCompletion(MessageContext messageContext,Object endpoint,Exception ex){





SaveMessageDO.class
/ **
*实体经理。
* /
@PersistenceContext(unitName =xxxHibernatePersistenceUnit)
private EntityManager entityManager;


/ **
*添加消息。
*
* @param消息消息
* /
@Transactional(value =xyzTxManager,propagation = Propagation.REQUIRED)
public void addMessage(final Message消息){
entityManager.persist(message);
}



SharedEntityManagerCreator.java中发生异常

  else if(transactionRequiringMethods.contains(method.getName())){
//根据JPA规范,我们现在需要一个事务性目标。
//否则,该操作会被接受但仍然不会被刷新......
if(target == null ||!TransactionSynchronizationManager.isActualTransactionActive()){
throw new TransactionRequiredException(No EntityManager当前线程的实际事务可用+
- 不能可靠地处理'+ method.getName()+'call);






该方法用@Transaction注解标记,当我调用切换回老的春天版本其工作正常,没有任何问题。



位代码背景,从Spring Webservices拦截器调用以将SOAP消息存储在数据库中。

解决方案

尝试将您的版本升级到此处公布的4.2.4版本: https://spring.io/blog/2015/12/17/spring-framework-4-2-4-4-1-9-released a>


4.2.4解决了4.2.x行中的一些倒退问题,并包含许多
修正和增强功能,其中没有立即解决问题。 4.2.4是现在所有4.x用户推荐的
升级。


我认为您遇到的问题可能是与此问题相关:不使用事务,传播SUPPORTS和OpenEntityManager模式的JPA行为不一致以及此错误报告 https://jira.spring.io/browse/SPR-13243



无论如何,如果不是新版本,仍然可以解决您的问题,因为很多问题和回归都已修复。



可能归因于框架的问题,检查任何修补程序版本并检查其错误管理系统总是一个好主意,例如 https:/ /jira.spring.io/browse/SPR-13243?jql=project%20%3D%20SPR%20AND%20text%20~%20transactionrequiredexception


I have upgraded to latest Spring version from 4.1.6.Release to 4.2.4.Release and suddenly all what has functioned smoothly before, now throws the following exception.

 javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:277) ~[na:4.2.4.RELEASE]
at com.sun.proxy.$Proxy51.persist(Unknown Source) ~[na:na]

Interceptor.class

public class MessageInterceptor implements EndpointInterceptor {

    @Resource
    private SaveMessageDO saveMessageDO;

    @Override
    public boolean handleRequest(MessageContext messageContext, Object endpoint) {
     saveMessageDO.addMessage(messageContext.getRequest()));        
     return true;
    }

    @Override
    public boolean handleResponse(MessageContext messageContext, Object endpoint) {
        return false;
    }

    @Override
    public boolean handleFault(MessageContext messageContext, Object endpoint) {
        return false;
    }

    @Override
    public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) {

    }
}


SaveMessageDO.class
    /**
     * The entity manager.
     */
    @PersistenceContext(unitName = "xxxHibernatePersistenceUnit")
    private EntityManager entityManager;


    /**
     * Adds the message.
     *
     * @param message the message
     */
    @Transactional(value = "xyzTxManager", propagation = Propagation.REQUIRED)
    public void addMessage(final Message message) {
        entityManager.persist(message);
    }

Exception occurs in SharedEntityManagerCreator.java

else if (transactionRequiringMethods.contains(method.getName())) {
                    // We need a transactional target now, according to the JPA spec.
                    // Otherwise, the operation would get accepted but remain unflushed...
                    if (target == null || !TransactionSynchronizationManager.isActualTransactionActive()) {
                        throw new TransactionRequiredException("No EntityManager with actual transaction available " +
                                "for current thread - cannot reliably process '" + method.getName() + "' call");
                    }
                }

The method are marked with @Transaction Annotation, when i call switch back to older spring version its works fine without any issues.

Bit Background about the code, calling from Spring Webservices Interceptors to store SOAP Message in database.

解决方案

Try upgrading your version to 4.2.4 announced here: https://spring.io/blog/2015/12/17/spring-framework-4-2-4-4-1-9-released

4.2.4 addresses a few regressions in the 4.2.x line and includes many fixes and enhancements, with no immediate issues remaining. 4.2.4 is a recommended upgrade for all 4.x users now.

I think the problem your experiencing is possibly related to this question: Inconsistent JPA behavior using no transaction, propagation SUPPORTS and OpenEntityManager pattern and this bug report https://jira.spring.io/browse/SPR-13243

And in anyway, if it isn't the new version may still resolve your problem as many issues and regressions were fixed.

When getting unexpected issues that could be attributed to a framework it's always a good idea to check for any fix releases and to check their bug management system as well e.g. https://jira.spring.io/browse/SPR-13243?jql=project%20%3D%20SPR%20AND%20text%20~%20transactionrequiredexception

这篇关于从Spring 4.1.6升级到4.2.4并突然收到TransactionRequiredException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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