问题
测试事务传播行为的时候,在使用单位测试加了@Transactional,一开始是正常,后面出现了异常,即使没有报错的情况下,事务也会自动回滚
代码
@Test
@Transactional
// @Rollback(false)
public void test(){
KsA ksA = new KsA();
ksA.setName("林");
ksAService.insert(ksA);
KsB ksB = new KsB();
ksB.setAge(10);
ksBService.insert(ksB);
}
@Override
public KsA insert(KsA ksA) {
this.ksADao.insert(ksA);
return ksA;
}
@Override
@Transactional
public void insert(KsB ksB) {
ksBDao.insert(ksB);
}
报错
2022-10-20 11:27:31.818 [TID: N/A] [main] INFO o.s.t.c.t.TransactionContext -Began transaction (1) for test context [DefaultTestContext@c430e6c testClass = TranTest, testInstance = com.forlan.kaoshi.TranTest@ce0c2b3, testMethod = test@TranTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@312aa7c testClass = TranTest, locations = '{}', classes = '{class com.forlan.kaoshi.KaoshiApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@418e7838, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@3e2e18f2, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@48ae9b55, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@72f926e6, org.spockframework.spring.mock.SpockContextCustomizer@0], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]; transaction manager [org.springframework.jdbc.datasource.DataSourceTransactionManager@1e1b512e]; rollback [true]
2022-10-20 11:27:32.296 [TID: N/A] [main] INFO c.m.interceptor.TenancyInterceptor -TenancyInterceptor插入日志信息->KsAMapper.insert->【系统调用】sql : insert into ks_a(name) values ('林')
2022-10-20 11:27:32.298 [TID: N/A] [main] DEBUG c.m.k.forlan.mapper.KsAMapper.insert -==> Preparing: insert into ks_a(name) values (?)
2022-10-20 11:27:32.316 [TID: N/A] [main] DEBUG c.m.k.forlan.mapper.KsAMapper.insert -==> Parameters: 林(String)
2022-10-20 11:27:32.365 [TID: N/A] [main] DEBUG c.m.k.forlan.mapper.KsAMapper.insert -<== Updates: 1
2022-10-20 11:27:32.377 [TID: N/A] [main] INFO c.m.interceptor.TenancyInterceptor -TenancyInterceptor插入日志信息->KsBDao.insert->【系统调用】sql : insert into ks_b(age) values (10)
2022-10-20 11:27:32.377 [TID: N/A] [main] DEBUG c.m.k.forlan.mapper.KsBDao.insert -==> Preparing: insert into ks_b(age) values (?)
2022-10-20 11:27:32.377 [TID: N/A] [main] DEBUG c.m.k.forlan.mapper.KsBDao.insert -==> Parameters: 10(Integer)
2022-10-20 11:27:32.421 [TID: N/A] [main] DEBUG c.m.k.forlan.mapper.KsBDao.insert -<== Updates: 1
2022-10-20 11:27:32.470 [TID: N/A] [main] INFO o.s.t.c.t.TransactionContext -Rolled back transaction for test: [DefaultTestContext@c430e6c testClass = TranTest, testInstance = com.forlan.kaoshi.TranTest@ce0c2b3, testMethod = test@TranTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@312aa7c testClass = TranTest, locations = '{}', classes = '{class com.forlan.kaoshi.KaoshiApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@418e7838, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@3e2e18f2, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@48ae9b55, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@72f926e6, org.spockframework.spring.mock.SpockContextCustomizer@0], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]
数据库无数据
解决
加@Rollback(false),关闭自动回滚
@Test
@Transactional
@Rollback(false)
public void test(){
// do
}
2022-10-20 11:24:36.132 [TID: N/A] [main] INFO o.s.t.c.t.TransactionContext -Began transaction (1) for test context [DefaultTestContext@66982506 testClass = TranTest, testInstance = com.forlan.kaoshi.TranTest@5efa2b92, testMethod = test@TranTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@70cf32e3 testClass = TranTest, locations = '{}', classes = '{class com.forlan.kaoshi.KaoshiApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@3e2e18f2, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@3e92efc3, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@30ee2816, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@27ce24aa, org.spockframework.spring.mock.SpockContextCustomizer@0], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]; transaction manager [org.springframework.jdbc.datasource.DataSourceTransactionManager@1838e02]; rollback [false]
2022-10-20 11:24:38.073 [TID: N/A] [main] INFO c.m.interceptor.TenancyInterceptor -TenancyInterceptor插入日志信息->KsAMapper.insert->【系统调用】sql : insert into ks_a(name) values ('林')
2022-10-20 11:24:38.077 [TID: N/A] [main] DEBUG c.m.k.forlan.mapper.KsAMapper.insert -==> Preparing: insert into ks_a(name) values (?)
2022-10-20 11:24:38.113 [TID: N/A] [main] DEBUG c.m.k.forlan.mapper.KsAMapper.insert -==> Parameters: 林(String)
2022-10-20 11:24:38.134 [TID: N/A] [main] DEBUG c.m.k.forlan.mapper.KsAMapper.insert -<== Updates: 1
2022-10-20 11:24:38.249 [TID: N/A] [main] INFO c.m.interceptor.TenancyInterceptor -TenancyInterceptor插入日志信息->KsBDao.insert->【系统调用】sql : insert into ks_b(age) values (10)
2022-10-20 11:24:38.249 [TID: N/A] [main] DEBUG c.m.k.forlan.mapper.KsBDao.insert -==> Preparing: insert into ks_b(age) values (?)
2022-10-20 11:24:38.249 [TID: N/A] [main] DEBUG c.m.k.forlan.mapper.KsBDao.insert -==> Parameters: 10(Integer)
2022-10-20 11:24:38.267 [TID: N/A] [main] DEBUG c.m.k.forlan.mapper.KsBDao.insert -<== Updates: 1
2022-10-20 11:24:38.323 [TID: N/A] [main] INFO o.s.t.c.t.TransactionContext -Committed transaction for test: [DefaultTestContext@66982506 testClass = TranTest, testInstance = com.forlan.kaoshi.TranTest@5efa2b92, testMethod = test@TranTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@70cf32e3 testClass = TranTest, locations = '{}', classes = '{class com.forlan.kaoshi.KaoshiApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@3e2e18f2, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@3e92efc3, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@30ee2816, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@27ce24aa, org.spockframework.spring.mock.SpockContextCustomizer@0], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]
可以看到事务已经提交,数据库有数据了