0

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.

hoefling
  • 59,418
  • 12
  • 147
  • 194
  • If these methods belong to the same class, then I would suggest to introduce 2 separate classes so that each of them to use only one data source. – Serge Bogatyrev Jan 07 '18 at 22:40

1 Answers1

0

@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().

Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42