【问题标题】:Upgraded from Spring 4.1.6 to 4.2.4 and suddenly getting TransactionRequiredException从 Spring 4.1.6 升级到 4.2.4 突然得到 TransactionRequiredException
【发布时间】:2015-12-28 13:49:33
【问题描述】:

我已经从 4.1.6.Release 升级到最新的 Spring 版本到 4.2.4.Release 并且突然所有之前运行顺利的东西,现在抛出以下异常。

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

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 Interceptors 调用以将 SOAP 消息存储在数据库中。

【问题讨论】:

  • 我认为我们需要更多背景知识。 :)
  • @Basco,我们希望在将消息发布到队列之前将消息保存在数据库中,并在我们编写的拦截器中拦截消息并存储在数据库中。添加了更多代码以供理解
  • 您需要在applicationContext.xml 中包含您的交易配置以及您使用的服务器(即 JBoss(又名 WildFly)

标签: spring hibernate spring-ws spring-transactions


【解决方案1】:

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

4.2.4 解决了 4.2.x 行中的一些回归问题,包括许多 修复和增强,不存在直接问题。 4.2.4 是一个 现在建议所有 4.x 用户升级。

我认为您遇到的问题可能与以下问题有关: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 版本 4.2.4.Release。它没有帮助。我想有人已经发布了:jira.spring.io/browse/SPR-13838。我认为我的问题也与此有关。 Spring 承诺在 4.2.5 中修复此问题
  • @SathishMurugesan 很高兴听到它正在进行中。如果问题在 4.2.5 中得到解决,请告诉我们并自行回答? :)
猜你喜欢
  • 2016-07-25
  • 2015-07-05
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 2018-12-30
  • 1970-01-01
  • 1970-01-01
  • 2016-05-23
相关资源
最近更新 更多