-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change in noRollbackFor transaction behavior with H2 database 2.x #28679
Comments
created a sample project to demonstrate the issue https://github.com/ksm-973/spring-transaction-rollback-issue/tree/master/demo Update springBootVersion in gradle.properties to 2.0.0.RELEASE (uses spring 5.0.4) and run UserServiceTest -> Test runs successfully |
Thanks for providing the demo project. We'll look into it. As a side note, test classes and test methods have to be |
FYI: if you set the Spring Boot version to ext['spring-framework.version'] = '5.3.21' Thus, it does not appear to be an issue with the core Spring Framework itself. However, I have confirmed that the test fails as soon as you upgrade Spring Boot from 2.6.9 to 2.7.0. |
After further analysis, I have determined that the upgrade to H2 2.x is causing the issue. Running the test with Spring Boot 2.7.0, Spring Framework 5.3.21 and H2 1.4.200 passes. ext['spring-framework.version'] = '5.3.21'
ext['h2.version'] = '1.4.200' Switching |
Thanks for looking into this. Original issue was identified in a stand alone spring app which uses SQL Server database. Spring: 5.3.20 Issue was possibly caused by the change for this #22420 |
The issue with the demo application is not an issue with Spring Framework or Spring Boot. Rather, the issue pertains to Hibernate's support for H2 2.0 and the fact that the test catches and swallows all exceptions and that the service catches all exceptions. The latter two issues mask the first issue. If you inspect the actual exceptions and log, you will see that there are numerous SQL exceptions. The following is the initial cause.
To fix the problem, you need to declare the @Entity
@Table(name = "`user`")
public class User {
// ...
} After doing that, your test should pass with Spring Boot 2.7.0, Spring Framework 5.3.21, and H2 2.1.214. In light of that, I am closing this issue as invalid. |
If you are able to provide us a sample application that reproduces the problem, feel free to share additional information in this issue and we will consider reopening it. Thanks |
Thank you! I overlooked few details but I have now updated the demo project with the code setup where the tests work with 5.0.x but fails with 5.3.x. https://github.com/kiranshm973/spring-transaction-rollback-issue/tree/master/demo Issue seems to be happening when a |
I think the change in behavior might be a result of commit 42d6d7e, due to the switch from I just wanted to leave that note here in case somebody else investigates in depth before I do. |
I've updated the sample to a supported version and I am not sure I understand it, nor how the referred commit would be related. The However when the exception in the DAO is thrown, the transaction source is the one from the DAO, not the one that started the transaction. At this stage, I'd need some help from @jhoeller as I am not sure whether that's legit and what to investigate next. And just to be clear @kiranshm973, this has nothing to do with an upgrade to H2, right? |
Closing due to the lack of requested feedback. |
Affects: 5.3.x
Issue identified while upgrading from Spring 5.0.x to 5.3.x
The above setup works as expected in Spring 5.0.x where the transaction is not rolled back when
CustomException
is thrown. But on upgrading to 5.3.x, an exception thrown inUserDAO
marks the transaction for rollback. This looks to be working in Spring 5.0.x because Spring does not track / parse classUserDAO
and methodupdateUser
for@Transactional
. So there is NOTransactionInfo
(withinTransactionAspectSupport
) created before invoking theupdateUser
method and hence no attempt to mark the transaction for rollback.In 5.3.x, Spring is able to source
TransactionAttribute
forUserDAO
becauseSpringTransactionAnnotationParser
identifies it as a candidate for@Transactional
and henceTransactionInfo
is created before the method is invoked, and the transaction is marked for rollback whenRuntimeException
is thrown.This would work if
updateUser
method inUserDAO
is annotated with@Transactional(noRollbackFor...)
, but I would like to start transaction in the service layer.The text was updated successfully, but these errors were encountered: