My application works with multi datasources and 2 databases Oracle and PostgreSQL (I dont need global transaction) . I dont know which transaction manager to use. Both have some advantages and disadvantages.
- Atomikos suppport global transaction which I dont need and log some information about transaction to file system which I want to avoid:
public void setEnableLogging(boolean enableLogging)
Specifies if disk logging should be enabled or not. Defaults to true. It is useful for JUnit testing, or to profile code without seeing the transaction manager's activity as a hot spot but this should never be disabled on production or data integrity cannot be guaranteed.
advantages is that it use just one transaction manager
- When using DataSourceTransactionManager I need one per dataSource
@Bean @Primary DataSourceTransactionManager transactionManager1() { DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(); transactionManager.setDataSource(dataSource1()); return transactionManager; } @Bean DataSourceTransactionManager transactionManager2() { DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(); transactionManager.setDataSource(dataSource2()); return transactionManager; }
this is problem because I need to specify name of tm in annotation:
@Transactional("transactionManager1")
public void test() {
}
but I dont know it because in runtime I can switch in application which database to use.
is there some other options or I am missing something in this two transaction manager ?
You should solve this as option 2, using one DataSourceTransactionManager per data source. You will need to keep track of the transaction manager for each data source.
One thing additionally, if you need to be able to rollback transactions on both databases, you will have to set up a ChainedTransactionManager for both.