Traditional solutions for distributed transactions

Traditional solutions for distributed transaction jta + atomikos registered with a global transaction, the transaction will be registered to Atomikos to go to achieve a global transaction, properties file:

Custom attribute class, and read register to Atomikos form global transaction class

@Configuration

// basePackages best configured separately if placed in the same folder may be incorrect report

@MapperScan(basePackages = "com.itmayiedu.test01", sqlSessionTemplateRef = "testSqlSessionTemplate")

public class MyBatisConfig1 {

 

// configuration data source

@Bean(name = "testDataSource")

public DataSource testDataSource(DBConfig1 testConfig) throws SQLException {

MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();

mysqlXaDataSource.setUrl(testConfig.getUrl());

mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

mysqlXaDataSource.setPassword(testConfig.getPassword());

mysqlXaDataSource.setUser(testConfig.getUsername());

mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

 

AtomikosDataSourceBean xaDataSource  = new  AtomikosDataSourceBean ()?

xaDataSource.setXaDataSource(mysqlXaDataSource);

xaDataSource.setUniqueResourceName("testDataSource");

 

xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());

xaDataSource.setMaxPoolSize(testConfig.getMaxPoolSize());

xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());

xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());

xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());

xaDataSource.setMaintenanceInterval(testConfig.getMaintenanceInterval());

xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());

xaDataSource .setTestQuery ( testConfig .getTestQuery ());

return xaDataSource;

}

 

@Bean(name = "testSqlSessionFactory")

public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource)

throws Exception {

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

return bean.getObject();

}

 

@Bean(name = "testSqlSessionTemplate")

public SqlSessionTemplate testSqlSessionTemplate(

@Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

 

@Configuration

@MapperScan(basePackages = "com.itmayiedu.test02", sqlSessionTemplateRef = "test2SqlSessionTemplate")

public class MyBatisConfig2 {

 

// configuration data source

@Bean(name = "test2DataSource")

public DataSource testDataSource(DBConfig2 testConfig) throws SQLException {

MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();

mysqlXaDataSource.setUrl(testConfig.getUrl());

mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

mysqlXaDataSource.setPassword(testConfig.getPassword());

mysqlXaDataSource.setUser(testConfig.getUsername());

mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

 

AtomikosDataSourceBean xaDataSource  = new  AtomikosDataSourceBean ()?

xaDataSource.setXaDataSource(mysqlXaDataSource);

xaDataSource.setUniqueResourceName("test2DataSource");

 

xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());

xaDataSource.setMaxPoolSize(testConfig.getMaxPoolSize());

xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());

xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());

xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());

xaDataSource.setMaintenanceInterval(testConfig.getMaintenanceInterval());

xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());

xaDataSource .setTestQuery ( testConfig .getTestQuery ());

return xaDataSource;

}

 

@Bean(name = "test2SqlSessionFactory")

public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)

throws Exception {

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

return bean.getObject();

}

 

@Bean(name = "test2SqlSessionTemplate")

public SqlSessionTemplate testSqlSessionTemplate(

@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

After registering to Atomikos, the obligation would not specify the annotation layer to enable transaction manager of the database transaction points like that, only with the @Transactional to comment, as it has been registered as a global transaction, and

Remember to add on startup class

@EnableConfigurationProperties(value = { DBConfig1.class, DBConfig2.class }) 

It opens and reads the configuration file.

So that we can solve problems between different transaction management services.

This program is not recommended for micro-services, because there is no compensation mechanism, the performance is not very good (electronic business systems generally do not use this method, generally used for conventional projects). Small items can be used in this way, and you can get to the data source.

Published 18 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_32285039/article/details/83860200