其他分享
首页 > 其他分享> > 从Spring 4.1.6升级到4.2.4并突然获得TransactionRequiredException

从Spring 4.1.6升级到4.2.4并突然获得TransactionRequiredException

作者:互联网

我已经从4.1.6.Release升级到了4.2.4.Release,并突然之间所有之前运行良好的最新Spring版本,现在抛出了以下异常.

 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]

拦截器类

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);
    }

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");
                    }
                }

该方法标有@Transaction Annotation,当我调用switch回到较早的spring版本时,它的工作正常,没有任何问题.

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

解决方法:

尝试将您的版本升级到此处宣布的4.2.4: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.

我认为您遇到的问题可能与以下问题有关:Inconsistent JPA behavior using no transaction, propagation SUPPORTS and OpenEntityManager pattern和此错误报告https://jira.spring.io/browse/SPR-13243

而且无论如何,由于许多问题和回归问题已得到解决,因此新版本仍然可以解决您的问题.

当遇到可能归因于框架的意外问题时,始终最好检查任何修复版本并检查其错误管理系统,例如https://jira.spring.io/browse/SPR-13243?jql=project%20%3D%20SPR%20AND%20text%20~%20transactionrequiredexception

标签:spring-transactions,spring-ws,hibernate,spring
来源: https://codeday.me/bug/20191119/2034655.html