I am working on the unit test of Spring Boot. As far as I know, it will auto open a transaction at the beginning & rollback at the end. However, I do not see the transaction is opened under the DB.
Here is some of the log from the Spring application:
[DEBUG] [AbstractPlatformTransactionManager.java]getTransaction(370) : Creating new transaction with name [...]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
[INFO ] [TransactionContext.java]startTransaction(107) : Began transaction (1) for test context [DefaultTestContext@25bc65ab testClass...]
[DEBUG] [AbstractPlatformTransactionManager.java]processRollback(833) : Initiating transaction rollback
[INFO ] [TransactionContext.java]endTransaction(139) : Rolled back transaction for test: [DefaultTestContext@25bc65ab testClass...]
While the test is running, I have run the following query on the DB:
SELECT * FROM information_schema.innodb_trx;
But no transaction has listed.
The test is just simple
@MyMapperTest
public class MapperTest {
@Autowired
private DbMapper mapper;
@Test
void test() {
mapper.insert(getDummyData());
}
}
And the annotation for @MyMapperTest
is:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@MybatisTest(excludeAutoConfiguration = DataSourceAutoConfiguration.class)
@ComponentScan(basePackages = { ... })
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("test")
The test is very simple, so it may not be because of the AOP proxy problem.
More information:
LazyConnectionDataSourceProxy
Many thanks!
I expected that the database must listed a transaction & the inserted data must be rolled back.
Also, I have tried:
@Transactional
to the testI have fixed the problem by grade-down/grade-up the version of MySQL connector. Many thanks for you help guys~
AS-IS:
TO-BE:
More detail: Looking at the change of 8.0.28, there is a change of autocommit override ("Some Java frameworks prevent changes of the autocommit value on the MySQL Server"). However, this change has been fixed again in 8.0.29