Outer transaction in a nested transaction use case isn't seeing updates persisted in database (JPA, MySQL, Spring Framework, and Hibernate)
I have a case where a transaction is started and along the way (in the code) a method gets called that starts a new transaction. When the inner transaction completes, the data is persisted in the database, but the data is not visible from the outer transaction.
Here's the code snippet(s)..
@Transactional(readOnly = true)
public void doSomething() {
// Some stuff happens here
doMoreStuff();
// Some more stuff happens here.
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void doMoreStuff() {
...
}
The "doMoreStuff" method updates some data in the database, afterwards the "doSomething" method needs to see that updated data, but it's not. For example, "doMoreStuff" is setting a boolean from false to true and persis开发者_如何学Goting it. The "doSomething" method is still only seeing the value as being false.
Any suggestions?
I don't know how the transaction "nesting" is done in Hibernate (as I don't believe MySQL can actually nest transactions).
So I would assume that the second (nested) transaction must (?) be a new connection to the database - otherwise it wouldn't be possible to rollback the "nested" transaction without affecting the "outer" transaction.
If this is indeed the case, then you are probably hit by MySQL's default isolation level which is REPEATABLE READ
which won't let the outer transaction see any data that has been committed after that transaction started.
To test this theory, try changing the isolation level (of the outer transaction) to READ COMMITTED
and see if that solves the problem.
Nested Transactions - Please use - Propagation.PROPAGATION_NESTED
精彩评论