1
0

More than 3 years have passed since last update.

意外と知られていない。SpringBoot のもう一つのPlatformTransactionManager

Posted at

意外と知られていない。SpringBoot のもう一つのPlatformTransactionManager
それは:org.springframework.data.transaction.ChainedTransactionManager です。

使用するイメージ

    @Bean(name = "chainedTransactionManager")
    public ChainedTransactionManager transactionManager(@Qualifier("primaryDs") PlatformTransactionManager ds1,
                                                    @Qualifier("secondaryDs") PlatformTransactionManager ds2) {
         return new ChainedTransactionManager(ds1, ds2);
    }
@Transactional(value="chainedTransactionManager")
public void updateDb01() {
    Entity01 entity01 = repository01.findOne(1234);
    entity01.setName("Name");
    repository01.save(entity01);

    //Calling method to update DB02
    updateDb02();
}

public void updateDb02() {
    Entity02 entity02 = repository02.findOne(1234);
    entity02.setName("Name");
    repository02.save(entity02);

    //Added this to force a roll back for testing
    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}

要するに、DS1とDS2を同時にコミット/ロールバックできることです。
DS1 -> insert -> OK
DS2 -> update -> OK
DS1,DS2 -> commit

DS1 -> insert -> OK
DS2 -> update -> NG
DS1,DS2 -> rollback

参考サイト
https://www.it-swarm-ja.tech/ja/spring/%E8%A4%87%E6%95%B0%E3%81%AE%E3%83%87%E3%83%BC%E3%82%BF%E3%82%BD%E3%83%BC%E3%82%B9%E3%81%AB%E3%81%BE%E3%81%9F%E3%81%8C%E3%82%8B%E3%83%88%E3%83%A9%E3%83%B3%E3%82%B6%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3%E3%82%92%E4%BC%B4%E3%81%86spring-transactional/837725347/

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0