I have to update two data sources as part of one transaction. That is -
If upd
I believe you have defined your txns like below.
@Bean(name="db01TransactionManager")
@Autowired
DataSourceTransactionManager tm1(@Qualifier ("datasource1") DataSource datasource) {
DataSourceTransactionManager txm = new DataSourceTransactionManager(datasource);
return txm;
}
@Bean(name="db02TransactionManager")
@Autowired
DataSourceTransactionManager tm2(@Qualifier ("datasource2") DataSource datasource) {
DataSourceTransactionManager txm = new DataSourceTransactionManager(datasource);
return txm;
}
Now simplest way is to try , catch and rollback for both of the transaction. But if you still want to delegate, there is an option as given below.
Create your own and override rollback methods and use it.
@Bean(name=“allTransactionManager")
@Autowired
DataSourceTransactionManager tm2(@Qualifier ("datasource1”) DataSource datasource1, @Qualifier ("datasource2") DataSource datasource2) {
DataSourceTransactionManager txm = new MyDataSourceTransactionManager(datasource1,datasouce2);
return txm;
}
And define your own transactionmanager as.
MyDataSourceTransactionManager extends DataSourceTransactionManager{
DataSourceTransactionManager tm1;
DataSourceTransactionManager tm2;
MyDataSourceTransactionManager(Datasource ds1,Datasource d2){
tm1 = new DataSourceTransactionManager(DataSource);
tm2 =new DataSourceTransactionManager(DataSource);
}
// override and for roll back, rollback for both of tm1 and tm2. Thus all actions are delegated in this class
}
Then use this for dao layers whereever you want to work synchronously.
@Transactional("allTransactionManager")
So now we have your own transaction managers, which is capable of rollbacking or commiting together for both type of transactions.