Multi-data source dynamic loading transaction control
Source code download address: https://github.com/qingqiangqiang/dynamic_datasource.git
According to the official documentation of spring boot, if spring boot detects the jta environment, it will automatically configure it, so no transaction configuration is required here.
Official address: http://docs.spring.io/spring-boot/docs/current/reference/ html/boot-features-jta.html
You can see that Atomikos will be managed on two data sources respectively.
However, this kind of transaction manages multiple data sources. If the operation of data source A is successful, but the operation of data source B fails, the data source A will not be rolled back. As follows:
Let's look at the code execution results and database results:
It can be seen that this kind of transaction management, for multiple data sources, the processing of abnormal rollback cannot be handled correctly, and a managed transaction manager is still required.
@Bean
public DataSourceTransactionManager transactionManager(@Qualifier("dynamicDS1") DynamicDataSource dynamicDS1) {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dynamicDS1);
return transactionManager;
}
It can be seen that the managed transaction will be managed on the bean of the dynamic data source, which will effectively control the transaction of multiple data sources. Execute test again, and no new data will be generated in the database.