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

  • And the problem is? You see that a tx is created? It is rolled back (that is what the logging tells you) and what is the result you have?

    – 

  • Even that the log has shown transaction created & rolled back. But it is not created, as you can see, when I am running in the debug mode, the SQL DB does not show any transaction. And the inserted data is not rolled back.

    – 

  • You shouldn’t be using auto-commit in the first place (and normally that will be disabled by Spring tx manager). Why are you using JTA do you have multiple transactional resources? One thing that could be an issue here is the use of the LazyConnectionDataSourceProxy i’m not sure how you are determining which ds to use but that might be problematic.

    – 

  • As far as I know, the auto-commit does not affect here. Because in the transaction, it will be disabled (ref). For the LazyConnectionDataSourceProxy I am used it to route to Master/Slave database based on the transaction read-only properties. Particularly, if the transaction is read-only, it will use Slave node, otherwise use the Master node.

    – 




  • Shouldn’t that, ideally, be handled by your database driver in a proper master/slave configuration? As stated the auto-commit shouldn’t affect anything but it could depending on what is happening. Also is mybatis setup correctly for spring, if it is using a different connection it isn’t using a tx. You have posted too little of your configuration only a partial test and some code, please include more.

    – 

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

Leave a Comment