Search code examples
mysqlspring-boottransactionsinnodbmybatis

Spring Boot test transaction is opened, but not in MySQL DB


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:

  • Spring Boot 2.7.18
  • Mybatis 2.1.3
  • Junit 5
  • Hikari 4.0.3
  • Embedded Tomcat 9.0.83
  • Use multiple datasource with LazyConnectionDataSourceProxy
  • All the table in DB is InnoDB

Many thanks!

I expected that the database must listed a transaction & the inserted data must be rolled back.

Also, I have tried:

  • Change config dialect
  • Make the class/method public
  • Turn off auto-commit
  • Try to add @Transactional to the test
  • Disable JTA

Solution

  • I have fixed the problem by grade-down/grade-up the version of MySQL connector. Many thanks for you help guys~

    AS-IS:

    • MySQL Connector 8.0.28

    TO-BE:

    • MySQL Connector 8.0.33

    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