Atomikos Forum |
|
I am trying to perform a save and update (in two separate database) using atomikos JTA however when there is exception in second database update statement, atomikos doesnt rollback the whole transaction (it commits the record in first db).
Below is my code: config file: @Configuration @ComponentScan public class JTATransactionConfig { @Bean public JpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); hibernateJpaVendorAdapter.setShowSql(true); hibernateJpaVendorAdapter.setGenerateDdl(true); hibernateJpaVendorAdapter.setDatabase(Database.MYSQL); return hibernateJpaVendorAdapter; } @Bean(name = "userTransaction") public UserTransaction userTransaction() throws Throwable { UserTransactionImp userTransactionImp = new UserTransactionImp(); userTransactionImp.setTransactionTimeout(10000); return userTransactionImp; } @Bean(name = "atomikosTransactionManager", initMethod = "init", destroyMethod = "close") public TransactionManager atomikosTransactionManager() throws Throwable { UserTransactionManager userTransactionManager = new UserTransactionManager(); userTransactionManager.setForceShutdown(false); AtomikosJtaPlatform.transactionManager = userTransactionManager; return userTransactionManager; } @Bean(name = "transactionManager") @DependsOn({ "userTransaction", "atomikosTransactionManager" }) public PlatformTransactionManager transactionManager() throws Throwable { UserTransaction userTransaction = userTransaction(); AtomikosJtaPlatform.transaction = userTransaction; TransactionManager atomikosTransactionManager = atomikosTransactionManager(); return new JtaTransactionManager(userTransaction, atomikosTransactionManager); } } entity db configuration is: public class table1DataSourceConfiguration { @Autowired Environment env; @Autowired JpaVendorAdapter jpaVendorAdapter; public DataSource table1DS() { AtomikosDataSourceBean xaDS = new AtomikosDataSourceBean(); xaDS.setXaDataSourceClassName(env.getProperty(DRIVER_CLASS_NAME)); xaDS.setXaDataSource(getMysqlXADataSource()); xaDS.setUniqueResourceName("t1DS"); xaDS.setMaxPoolSize(3); return xaDS; } private MysqlXADataSource getMysqlXADataSource() { MysqlXADataSource ds = new MysqlXADataSource(); ds.setPinGlobalTxToPhysicalConnection(true); ds.setURL(env.getProperty(url)); ds.setUser(env.getProperty(username)); ds.setPassword(env.getProperty(password)); return ds; } @Bean(name="table1EntityMF") public LocalContainerEntityManagerFactoryBean table1EntityMF() throws Throwable{ Map<String, Object> properties = new HashMap<>(); properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName()); properties.put("javax.persistence.transactionType", "JTA"); LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean(); entityManager.setJtaDataSource(table1DS()); entityManager.setJpaVendorAdapter(jpaVendorAdapter); entityManager.setPackagesToScan("com.cbc.domain.table1"); entityManager.setPersistenceUnitName("Event1PersistenceUnit"); entityManager.setJpaPropertyMap(properties); return entityManager; } service class is : @Service public class table1ServiceImpl implements table1Service { @Autowired table1Repository repo; @Transactional public int saveTable1(table1 table1) { TransactionStatus status = TransactionAspectSupport.currentTransactionStatus(); LOG.debug("--------------- is new transaction in service2 call-------------" + status.isNewTransaction()); return repo.save(table1); } } and below is the method where i am making two calls to two separate databases: @Transactional public void testJTATransaction() { try { System.out.println(TransactionSynchronizationManager.isActualTransactionActive()); TransactionStatus status = TransactionAspectSupport.currentTransactionStatus(); table1Service.saveTable1(table1); table2Service.updateTable2(new Date()); }catch(Exception e){ LOG.debug(e.getMessage()); LOG.debug("transaction should be rolledback"); } } if there is an exception in updateTable2 method, then it the transaction in saveTable1 method should also be rolledback however saveTable1 method commits the record and doesnt roll back. Could anyone please help |