使用Spring Boot + My Batis Spring Boot创建测试时遇到问题
我为我的地图制作者创建了几个U. Test。对于插入,我使用@SelectKey
注释来获取序列的下一个值。当Spring回滚时,事务不会回滚序列,并在每个应用程序启动时递增它。
FE。
@RunWith(SpringRunner.class)
@ActiveProfiles(profiles = "local")
@SpringBootTest
@Slf4j
@Transactional
public class MultimediaMapperTest {
@Autowired
private MultimediaMapper multimediaMapper;
@Autowired
private IEmpresasService empresasService;
@Test
public void insertArchivo() {
Multimedia archivo = Multimedia.builder()
.tiposEntidad(TiposEntidad.EMP)
.idEntidad(1)
.awsKey("KEY_AMAZON_WEB_SERVICES")
.nomFichero("fichero.txt")
.mime("application/pdf")
.observaciones("prueba observaciones")
.build();
multimediaMapper.insertArchivos(archivo);
log.debug("Fin de la inserción de {}", archivo);
assertThat(archivo.getIdArchivo(), is(notNullValue()));
}
日志:
首先开始:
[INFO ] 2018-04-23 21:44:16,369 [] o.s.c.s.DefaultLifecycleProcessor - Starting beans in phase 2147483647 [INFO ] 2018-04-23 21:44:16,369 [] s.d.s.w.p.DocumentationPluginsBootstrapper - Context refreshed [INFO ] 2018-04-23 21:44:16,396 [] s.d.s.w.p.DocumentationPluginsBootstrapper - Found 1 custom documentation plugin(s) [INFO ] 2018-04-23 21:44:16,470 [] s.d.s.w.s.ApiListingReferenceScanner - Scanning for api listing references [INFO ] 2018-04-23 21:44:16,880 [] e.g.m.a.d.MultimediaMapperTest - Started MultimediaMapperTest in 7.035 seconds (JVM running for 8.255) [INFO ] 2018-04-23 21:44:18,600 [] o.s.t.c.t.TransactionContext - Began transaction (1) for test context [DefaultTestContext@40d10264 testClass = MultimediaMapperTest, testInstance = es.gogroup.module.archivos.dao.MultimediaMapperTest@6edd4fe2, testMethod = insertArchivo@MultimediaMapperTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@2773504f testClass = MultimediaMapperTest, locations = '{}', classes = '{class es.gogroup.PanchoApplication}', contextInitializerClasses = '[]', activeProfiles = '{local}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.SpringBootTestContextCustomizer@2a798d51, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@37afeb11, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@245b4bdc, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@1b7cc17c], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]]; transaction manager [org.springframework.jdbc.datasource.DataSourceTransactionManager@cfb94fd]; rollback [true] [DEBUG] 2018-04-23 21:44:18,662 [] e.g.m.m.d.M.insertArchivos!selectKey - ==> Preparing: SELECT pg_catalog.nextval('"SEQ_MULTIMEDIA"'); [DEBUG] 2018-04-23 21:44:18,689 [] e.g.m.m.d.M.insertArchivos!selectKey - ==> Parameters: [DEBUG] 2018-04-23 21:44:18,759 [] e.g.m.m.d.M.insertArchivos!selectKey - Preparing: INSERT INTO MULTIMEDIA(ID_ARCHIVO,TIPO_ENTIDAD, ID_ENTIDAD,AWS_KEY, NOM_FICHERO, OBSERVACIONES, MIME) VALUES (?,?,?,?, ?, ?, ?) [DEBUG] 2018-04-23 21:44:18,767 [] e.g.m.m.d.M.insertArchivos - ==> Parameters: 15(Integer), EMP(String), 1(Integer), KEY_AMAZON_WEB_SERVICES(String), fichero.txt(String), prueba observaciones(String), application/pdf(String) [DEBUG] 2018-04-23 21:44:18,803 [] e.g.m.m.d.M.insertArchivos -
下一步开始:
[INFO ] 2018-04-23 21:58:15,778 [] o.s.t.c.t.TransactionContext - Began transaction (1) for test context [DefaultTestContext@40d10264 testClass = MultimediaMapperTest, testInstance = es.gogroup.module.archivos.dao.MultimediaMapperTest@6edd4fe2, testMethod = insertArchivo@MultimediaMapperTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@2773504f testClass = MultimediaMapperTest, locations = '{}', classes = '{class es.gogroup.PanchoApplication}', contextInitializerClasses = '[]', activeProfiles = '{local}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.SpringBootTestContextCustomizer@2a798d51, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@37afeb11, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@245b4bdc, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@1b7cc17c], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]]; transaction manager [org.springframework.jdbc.datasource.DataSourceTransactionManager@cfb94fd]; rollback [true] [DEBUG] 2018-04-23 21:58:15,856 [] e.g.m.m.d.M.insertArchivos!selectKey - ==> Preparing: SELECT pg_catalog.nextval('"SEQ_MULTIMEDIA"'); [DEBUG] 2018-04-23 21:58:15,888 [] e.g.m.m.d.M.insertArchivos!selectKey - ==> Parameters: [DEBUG] 2018-04-23 21:58:15,954 [] e.g.m.m.d.M.insertArchivos!selectKey - Preparing: INSERT INTO MULTIMEDIA(ID_ARCHIVO,TIPO_ENTIDAD, ID_ENTIDAD,AWS_KEY, NOM_FICHERO, OBSERVACIONES, MIME) VALUES (?,?,?,?, ?, ?, ?) [DEBUG] 2018-04-23 21:58:15,964 [] e.g.m.m.d.M.insertArchivos - ==> Parameters: 16(Integer), EMP(String), 1(Integer), KEY_AMAZON_WEB_SERVICES(String), fichero.txt(String), prueba observaciones(String), application/pdf(String) [DEBUG] 2018-04-23 21:58:16,001 [] e.g.m.m.d.M.insertArchivos -
答案 0 :(得分:6)
无法回滚数据库序列。这是一个功能,而不是一个bug。 :)
重要说明:避免阻止获取的并发事务 来自相同序列的数字,从不滚动nextval操作 背部;也就是说,一旦获取了一个值,就会被认为是使用过的, 即使稍后执行nextval的事务也会中止。这意味着 中止的事务可能会在序列中留下未使用的“漏洞” 分配值。
生成序列号后,序列号可用 仅限于生成该数字的会话。独立于 提交或回滚的事务,其他用户引用 order_seq.NEXTVAL获取唯一值。如果两个用户正在访问 同时执行相同的序列,然后每个用户的序列号 接收可能有间隙因为序列号也在 由其他用户生成。
序列号是在当前范围之外生成的 交易。他们是否使用该交易消费 序列号已提交或回滚。