spring - Spring - @Transactional - 什么时候刷新数据?
问题描述
我正在使用Spring 4.3.4.RELEASE和Hibernate
5.1.15
作为从 3 到 5 的休眠迁移的一部分,我面临一些与事务注释相关的问题。
考虑一种方法包含 2 种不同方法(带/不带事务注释)的情况。
在休眠 3 中,它曾经正常工作。但是在升级到休眠 5 方法 setEMSStatus 后由于 subscribedEms 为空而失败。
createAndSaveSubscribedEms 没有将数据刷新到数据库。因为 setEMSStatus 没有从数据库中获取数据,因此为空。
显式添加刷新会起作用。但是可能有100个地方有类似的情况。所以添加显式刷新看起来不是一个可行的解决方案。
//Class1
@Transactional(rollbackFor = Exception.class)
public void validateAndSave(List<EmsDto> gridList) {
//Removing unnecessary code
emsEntityManager.createAndSaveSubscribedEms(EmsDto.getMessageName(), EmsDto.getDescription(), SubscribeEmsStatus.UNKNOWN.toString());
//Removing unnecessary code
emsEntityManager.setEMSStatus(entry.getKey(), SubscribeEmsStatus.VALID.toString());
}
//Class2
@Transactional
public void createAndSaveSubscribedEms(final String messageName, final String description, final String status) {
session.save(SubscribedEms.create(messageName, description, status));
}
// Note: No transactional and it was working earlier in hibernate 3
public void setEMSStatus(String emsName, String newStatus) {
SubscribedEms subscribedEms = (SubscribedEms) session.createCriteria(SubscribedEms.class).
add(Restrictions.eq(SubscribedEms_.messageName, emsName)).
uniqueResult();
subscribedEms.setStatus(newStatus);
}
我遇到了类似的问题
https://www.javacodegeeks.com/2013/03/migrating-from-hibernate-3-to-4-with-spring-integration.html
但是在休眠 5.1.17 我没有看到类本身。
我应该进行哪些更改才能使其正常工作?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"
>
<context:annotation-config />
<context:component-scan base-package="com.company.product.da">
<context:exclude-filter type="regex" expression="com\.company\.product\..*Test\$.*"/>
<context:exclude-filter type="regex" expression="com\.company\.product\..*TestSupport\$.*"/>
</context:component-scan>
<context:component-scan base-package="com.company.product.entity.platform.event">
<context:exclude-filter type="regex" expression="com\.companthis partct\..*Test\$.*"/>
</context:component-scan>
<jee:jndi-lookup id="jtaTransactionManager" jndi-name="java:/TransactionManager"/>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" depends-on="jtaTransactionManager"/>
<import resource="classpath:spring/database-common.xml"/>
<bean id="eventListenerIntegrator" class="com.company.product.persist.hibernate.EventListenerIntegrator"/>
<bean id="hibernateSessionFactory" class="com.company.product.persist.hibernate.CustomHibernateSessionFactoryBean"
depends-on="migrationVersionVerifier">
<constructor-arg ref = "customInfinispanRegionFactory"/>
<constructor-arg ref = "eventListenerIntegrator"/>
<property name="hibernateProperties" ref="optionValueStoreProperties" />
<property name="packagesToScan" value="com.company.product.entity" />
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
我启用了日志以查看使用的事务管理器。
DEBUG [o.s.t.jta.JtaTransactionManager] JTA TransactionManager found at fallback JNDI location [java:/TransactionManager]
INFO [o.s.t.jta.JtaTransactionManager] Using JTA UserTransaction: org.wildfly.transaction.client.LocalUserTransaction@701458cf
INFO [o.s.t.jta.JtaTransactionManager] Using JTA TransactionManager: org.wildfly.transaction.client.ContextTransactionManager@2bba8287
DEBUG [o.s.t.jta.JtaTransactionManager] JTA TransactionSynchronizationRegistry found at default JNDI location [java:comp/TransactionSynchronizationRegistry]
INFO [o.s.t.jta.JtaTransactionManager] Using JTA TransactionSynchronizationRegistry: org.jboss.as.txn.service.internal.tsr.TransactionSynchronizationRegistryWrapper@5437a6d1
session.getTransaction().getStatus() 是NOT_ACTIVE
session.getFlushMode() 是AUTO
*****jtaTransactionManager 有什么问题吗?****
解决方案
推荐阅读
- java - TreeSet 如何在内部工作并存储自定义对象?
- flutter - 无法使用 socket_io_client 颤振插件连接到服务器
- java - @EnableConfigurationProperties Javadocs 对“标准 Spring Beans”意味着什么?
- c# - Fluent Validation C# 集合中的唯一 ID
- python - python中的pymagnitude导入问题
- swift - Swift - 活动视图控制器。共享“zip”文件时,如何禁用“获取缩略图失败”消息?
- assembly - MIPS中的求和数组有条件地跳过某些数字
- azure - 创建 Blob 时的逻辑应用触发器显示错误
- python - 在 bash 中运行命令并在 python GUI 中更新 tkinter.Text
- sql - SQL 我只想显示相同的