我有两个MySQL
架构,各自的MysqlXADataSource
配置如下 -
@Bean(name = "sourceDataSource")
@Primary
public DataSource dataSource() {
MysqlXADataSource dataSource = new MysqlXADataSource();
dataSource.setPinGlobalTxToPhysicalConnection(true);
dataSource.setUrl(DB_URL);
dataSource.setUser(DB_USERNAME);
dataSource.setPassword(DB_PASSWORD);
AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
atomikosDataSourceBean.setUniqueResourceName("SourceDB");
atomikosDataSourceBean.setXaDataSource(dataSource);
/*DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(DB_DRIVER);
dataSource.setUrl(DB_URL);
dataSource.setUsername(DB_USERNAME);
dataSource.setPassword(DB_PASSWORD);*/
System.out.println("Creatign Source data source.");
return atomikosDataSourceBean;
}
@Bean(name = "sourceSessionFactory")
public LocalSessionFactoryBean sessionFactory(@Qualifier("jtaTransactionManager") JtaTransactionManager jts) {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setPackagesToScan(ENTITYMANAGER_PACKAGES_TO_SCAN);
Properties hibernateProperties = new Properties();
hibernateProperties.put("hibernate.dialect", HIBERNATE_DIALECT);
hibernateProperties.put("hibernate.show_sql", HIBERNATE_SHOW_SQL);
hibernateProperties.put("hibernate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO);
hibernateProperties.put("hibernate.transaction.factory_class", "org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory");
hibernateProperties.put("hibernate.transaction.coordinator_class", "jta");
hibernateProperties.put("hibernate.transaction.jta.platform", "com.atomikos.icatch.jta.hibernate4.AtomikosPlatform");
//hibernateProperties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName());
sessionFactoryBean.setJtaTransactionManager(jts);
sessionFactoryBean.setHibernateProperties(hibernateProperties);
System.out.println("Creatign Source Session Factory.");
return sessionFactoryBean;
}
和
@Bean(name="destinationDataSource")
public DataSource dataSource() {
MysqlXADataSource dataSource = new MysqlXADataSource();
dataSource.setPinGlobalTxToPhysicalConnection(true);
dataSource.setUrl(DB_URL);
dataSource.setUser(DB_USERNAME);
dataSource.setPassword(DB_PASSWORD);
AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
atomikosDataSourceBean.setUniqueResourceName("DestinationDB");
atomikosDataSourceBean.setXaDataSource(dataSource);
/*DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(DB_DRIVER);
dataSource.setUrl(DB_URL);
dataSource.setUsername(DB_USERNAME);
dataSource.setPassword(DB_PASSWORD);
System.out.println("Creatign Destination Data Source.");*/
return atomikosDataSourceBean;
}
@Bean(name="destinationSessionFactory")
public LocalSessionFactoryBean sessionFactory(@Qualifier("jtaTransactionManager") JtaTransactionManager jts) {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setPackagesToScan(ENTITYMANAGER_PACKAGES_TO_SCAN);
Properties hibernateProperties = new Properties();
hibernateProperties.put("hibernate.dialect", HIBERNATE_DIALECT);
hibernateProperties.put("hibernate.show_sql", HIBERNATE_SHOW_SQL);
hibernateProperties.put("hibernate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO);
hibernateProperties.put("hibernate.transaction.factory_class", "org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory");
hibernateProperties.put("hibernate.transaction.coordinator_class", "jta");
hibernateProperties.put("hibernate.transaction.jta.platform", "com.atomikos.icatch.jta.hibernate4.AtomikosPlatform");
sessionFactoryBean.setJtaTransactionManager(jts);
sessionFactoryBean.setHibernateProperties(hibernateProperties);
System.out.println("Creatign Destination Session Factoy.");
return sessionFactoryBean;
}
交易配置
@Bean("transactionManager")
public UserTransactionManager transactionManager(){
UserTransactionManager userTransactionManager = new UserTransactionManager();
return userTransactionManager;
}
@Bean("userTransaction")
public UserTransaction userTransaction(){
J2eeUserTransaction userTransaction = new J2eeUserTransaction();
return userTransaction;
}
@Bean("jtaTransactionManager")
public JtaTransactionManager jtaTransactionManager(@Qualifier("transactionManager") UserTransactionManager userTransactionManager, @Qualifier("userTransaction") UserTransaction userTransaction) {
JtaTransactionManager transactionManager = new JtaTransactionManager();
transactionManager.setTransactionManager(userTransactionManager);
transactionManager.setUserTransaction(userTransaction);
//transactionManager.setSessionFactory(sessionFactory().getObject());
System.out.println("Creatign JTA Transaction Manager.");
return transactionManager;
}
服务......
@Transactional(transactionManager = "jtaTransactionManager", propagation=Propagation.REQUIRES_NEW, rollbackFor=Exception.class, timeout=500000)
public void transform() throws JobExecutionException{
System.out.println("START == Starting Trandformation ... ");
//Clean Up
transformProduct.cleanUp();
transformService.cleanUp();
transformResources.cleanUp();
transformResources.transform();
transformService.transform();
transformProduct.transform();
LOG.info("END == Starting Transformation ... ");
throw new JobExecutionException();
}
并且有各自的子服务类transformProduct
,transformService
& transformResources
这些类正在使用sessionFactory
实现的dao类。我没有提到提交或回滚。一切都应该是跨国的。但不知何故它不起作用
答案 0 :(得分:1)
好的..代码中有错误 -
AtomikosDataSourceBean
,并且应该返回AtomikosDataSourceBean
参考。AtomikosDataSourceBean
分配唯一名称。@Transactional(transactionManager = "jtaTransactionManager" ...)
,提供Spring JtaTransactionManager
。JtaTransactionManager
应引用javax.transaction.UserTransaction
com.atomikos.icatch.jta.J2eeUserTransaction
实施。JtaTransactionManager
应引用com.atomikos.icatch.jta.UserTransactionManager
实施。这是对Atomikos的直接开箱即用支持。