简体   繁体   中英

Transaction inside transaction support? (multi database) in Spring boot

I have DB1, DB2. I config two DataSource and two PlatformTransactionManager for two database (within the same physical machine). I have this code:

@Transaction("DB1")
public void A() {
    B();
}
@Transaction("DB2")
public void B() {
}

When B() has an SqlException , data in DB1 did not rollback. How to implement rollback DB1? Thank very much.

@Transaction annotation has an attribute rollbackFor ( @Transaction(value = "DB1", rollbackFor = SqlException.class) for example) that may cover your needs.

However, there is only one transaction will be actually used in your code, because B is invoked not via Spring proxy but via inner call ( this.B() ). To execute method inside a separate transaction method must be invoked via Spring proxy - someService.B() , not this.B() .

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