Spring Boot Druid multi-data source Atomikos distributed transaction defect

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.
write picture description here
Official address: http://docs.spring.io/spring-boot/docs/current/reference/ html/boot-features-jta.html
atomikos transaction management autoload
atomikos transaction management is loaded on a specific data source 1
atomikos transaction management is loaded on specific data sources 2
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:
write picture description here
Let's look at the code execution results and database results:
write picture description here
write picture description here

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;
    }

write picture description here

write picture description here
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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324379494&siteId=291194637