我正在尝试更新新事务中的Company对象,并期望使用更新的参数检索相同的对象。但它们不是:(名称不会改变。'之后'和'之前'是相同的。数据库已更新,但外部事务不知道这一点。你知道这种情况的解决办法吗?
@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
public Status newTransactionTest() {
logger.info("newTransactionTest() INNER");
Company company = companyDAO.findOne(10000013);
company.setName(company.getName() + "X");
return Status.OK;
}
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Status test() {
logger.info("test() BEFORE");
Company company1 = companyDAO.findOne(10000013);
String before = company1.getName();
// run in a new transaction
applicationContext.getBean(beanName, AdminService.class).newTransactionTest();
logger.info("test() AFTER");
Company company2 = companyDAO.findOne(10000013);
String after = company2.getName();
logger.info("COMPANY NAME BEFORE: " + before);
logger.info("COMPANY NAME AFTER: " + after);
return Status.OK;
}
并且日志是:
test() BEFORE
connection: 122 select company0_.name as name7_4_0_ ... where company0_.id=5000062
newTransactionTest() INNER
connection: 123 select company0_.name as name7_4_0_ ... where company0_.id=5000062
connection: 123 update Company set name='TestorexX' where id=5000062
connection: 123 commit
test() AFTER
connection: 122 select company0_.name as name7_4_0_ ... where company0_.id=5000062
COMPANY NAME BEFORE: Testorex
COMPANY NAME AFTER: Testorex
答案 0 :(得分:0)
好的,最后我修复了这个bug。问题在于我的本地mySQL实例上的默认隔离级别和设置为REPEATABLE-READ的Cloud SQL。要检查我使用的这些设置:
SHOW VARIABLES WHERE Variable_name ='tx_isolation'
所以我的重复查询返回了相同的结果,因为在db级别上完成的兑现不像我在hibernate / spring上预期的那样
要将REPEATABLE-READ更改为READ COMMITTED,我将其添加到我的persistence.xml
<property name="hibernate.connection.isolation">2</property>
其中
1: READ UNCOMMITTED
2: READ COMMITTED
4: REPEATABLE READ
8: SERIALIZABLE
现在一切正常,正如预期的那样。在每个新事务开始的时候,hibernate都会这样做
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED
外部交易看到内部的变化!