Spring Boot+Atomikos进行多数据源的分布式事务管理详解和实例
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.beans.factory.annotation.Qualifier;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.config.UserTransactionServiceImp;
import javax.transaction.UserTransaction;
import javax.sql.XADataSource;
import com.atomikos.icatch.config.Configuration;
import com.atomikos.icatch.config.ImplicitTransactionManager;
import com.atomikos.jdbc.AtomikosDataSourceBean;
@Configuration
@EnableTransactionManagement
public class TransactionConfig {
@Bean(initMethod = "init", destroyMethod = "close")
public UserTransactionImp userTransaction() {
UserTransactionImp userTransaction = new UserTransactionImp();
userTransaction.setTransactionTimeout(600000);
return userTransaction;
}
@Bean(initMethod = "init", destroyMethod = "close")
public UserTransactionServiceImp userTransactionService() {
UserTransactionServiceImp userTransactionService = new UserTransactionServiceImp();
userTransactionService.setMaxTransactions(100);
userTransactionService.setTransactionTimeout(600000);
return userTransactionService;
}
@Bean
public ImplicitTransactionManager implicitTransactionManager() {
ImplicitTransactionManager implicitTransactionManager = new ImplicitTransactionManager();
implicitTransactionManager.setAllowNestedTransactions(true);
return implicitTransactionManager;
}
@Bean(initMethod = "init", destroyMethod = "close")
public AtomikosDataSourceBean dataSource1(@Qualifier("xadsDataSource1") XADataSource xaDataSource) {
AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
ds.setXaDataSource(xaDataSource);
ds.setUniqueResourceName("dataSource1");
ds.setMinPoolSize(5);
ds.setMaxPoolSize(20);
ds.setMaxLifetime(18
评论已关闭