-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORE-385 - Enable transaction management on package protected methods.
The default AnnotationTransactionAttributeSource registered by Spring Framework is configured to *only* consider public methods for transaction attributes. This probably stems from the time of JDK proxies in which all invocable methods on the proxy are public by definition. In the age of CGLib proxies however, package protected methods can be invoked on the proxy and pass it properly. Due to the default setting within AnnotationTransactionAttributeSource, the interceptor chain ends up empty as it doesn't even bother to inspect methods for transaction annotations. We introduce a workaround in the form of a PriorityOrdered BeanPostProcessor that flips the flag within AnnotationTransactionAttributeSource to false, so that the lookup of the transaction attribute also inspects package protected methods. Aside: tweaked JpaAutoFlushTestExceutionCallback to only flush if a transactions is actually running, as that is the scenario it is supposed to handle: to make sure changes can be written to the database even in the light of a following rollback, in which the persistence provider would skip the writing entirely. We need this additional tweak as we need to run the test for the changes described above in a non-transactional integration test and trying to flush the EntityManager without a running transaction fails.
- Loading branch information
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package quarano; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
@Component | ||
public class SampleComponent { | ||
|
||
@Transactional | ||
void someMethod() { | ||
assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isTrue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package quarano; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
/** | ||
* @author Oliver Drotbohm | ||
*/ | ||
@RequiredArgsConstructor | ||
@QuaranoIntegrationTest | ||
@Transactional(propagation = Propagation.NOT_SUPPORTED) | ||
public class TransactionSetupTests { | ||
|
||
private final SampleComponent foo; | ||
|
||
@Test | ||
void createsTransactionForPackageProtectedMethod() { | ||
foo.someMethod(); | ||
} | ||
} |