简体   繁体   中英

Spring4 Junit4 Test: @TransactionConfiguration VS @Transactional, use only one or together?

In My junit test case:

  1. I use only @transactional at class or one, each test method will roll back, because each method within running a transaction. And I add it at method, then the assigned method will roll back.

  2. I use only @TransactionConfiguration(defaultRollback = true) at class, but the transaction is not work.

  3. I use @transaction and @TransactionConfiguration(defaultRollback = true) at class together, also it will be roll back.

My issue:

  1. Why roll back is not work with only @TransactionConfiguration at class?

  2. I see @transactional default bean name is transaction manager and roll back is TRUE, so i only add it at class and not @TransactionConfiguration?

  3. @transactional must annotation, is @TransactionConfiguration only assign transaction manager in multi-transactionManager case?

  1. @transactional causes each method to be run within a transaction, and you can configuration, isolation etc by @transactional.
  2. You can configuration transaction manager with @TransactionManager in multi-TransactionManager. So transaction is not work if you only use this annotation.
  3. Because each method to be run within a transaction, then you can change it with @RollBack(false).

https://docs.spring.io/spring/docs/4.3.x/javadoc-api/org/springframework/test/context/transaction/TransactionConfiguration.html中 ,他们说@TransactionConfiguration已“弃用”,“自Spring开始”在框架4.2中,在类级别使用@Rollback或@Commit,在@Transactional中使用transactionManager限定符”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM