-
Notifications
You must be signed in to change notification settings - Fork 38.4k
Method invocation in same class when both methods are @Transactional, but method 2 never executes #27534
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
Comments
Use of In any case, a better way to achieve your goal is to make use of self injection or self reference by injecting a lazy proxy to yourself (which ends up accessing the transactional proxy). For example, the test class below prints:
This verifies that a new transaction was started for the invocation of You can also turn on package example;
// imports
@SpringJUnitConfig
class SelfReferenceTests {
@Test
void test(@Autowired TestService testService) {
testService.test1();
}
@Configuration
@EnableTransactionManagement
@Import(TestServiceImpl.class)
static class Config {
@Bean
TransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
DataSource dataSource() {
return new EmbeddedDatabaseBuilder().generateUniqueName(true).build();
}
}
}
interface TestService {
void test1();
void test2();
}
class TestServiceImpl implements TestService {
private final TestService self;
TestServiceImpl(@Lazy TestService self) {
this.self = self;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void test1() {
System.err.println("test 1 :: " + TransactionSynchronizationManager.getCurrentTransactionName());
self.test2();
}
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void test2() {
System.err.println("test 2 :: " + TransactionSynchronizationManager.getCurrentTransactionName());
}
} In light of that, I am closing this issue. |
@sbrannen |
@b19g3r, the answer to your question is in the class-level Javadoc for
In the future, please ensure that you have read all related documentation before asking questions, or alternatively ask on Stack Overflow. Thanks |
I want to use
Propagation.REQUIRES_NEW
to start a new transaction, but test2 never be executed and process has been hang up. Is there any thing wrong in my example?here is my code,
The text was updated successfully, but these errors were encountered: