gpt4 book ai didi

spring - 从 Spring 4.1.6 升级到 4.2.4 突然出现 TransactionRequiredException

转载 作者:行者123 更新时间:2023-12-02 05:03:34 25 4
gpt4 key购买 nike

我已经从 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]

拦截器类

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注释,当我调用切换回旧的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 - 从 Spring 4.1.6 升级到 4.2.4 突然出现 TransactionRequiredException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34494754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com