0

My project does batch processing and is trying to use 2 datasources (oracle) (one for spring metadata and second for application).

The sample configuration is in github project (github.com/jobas2007/spring_proj) , but because there are 2 transaction manager/datasource configured, there appears to be inconsistency in managing transaction across commits being done for 2 datasources. The Primary datasource (for batch) commits are happening correctly, but when finally doing "save" in ItemWriter,  very unpredictable behavior is observed during save/update to application tables.

Please advise a good way to manage transaction between multiple datasources?

As per link https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-1-the-basics/ , Tx starts for a chunk when entering step

Tried suggestion from one of the post to use 'ChainedTransactionManager' but facing major issues in wiring Use of multiple DataSources in Spring Batch

code is in github

expected result is to have predictable robust tx management

Arpit S
  • 137
  • 2
  • 10
  • 1
    Distributed transactions are really the only way to manage this. – Michael Minella Jun 10 '19 at 13:28
  • can you please give some code snippet or instruction, how to enable distributed tx in spring batch (where primary is for spring metadata) and secondary data source for application? For application level commits , can inject "Global/Chained Tx Manager" in the @Service class (which call repository.save(), within tx boundary), but how to pass Golbal/Chained Tx manager to inject at the beginning of the Spring Batch Step (where tx being as per this link https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-1-the-basics/) ? – Arpit S Jun 12 '19 at 22:29
  • Someone also tried to use distrubuted transaction using 'atomikos' but seems to be still having issue - https://stackoverflow.com/questions/38323207/issue-with-configuring-atomikos-on-a-spring-boot-spring-batch-application ...please assist? – Arpit S Jun 13 '19 at 06:03

1 Answers1

0

If you decide to use two datasources with Spring Batch, you need to use a JtaTransactionManager to synchronize the distributed transaction between the two datasources.

You can find a great article by Dr. David Syer on the matter here: Distributed transactions in Spring, with and without XA

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • Did follow the article and did something like this by using chained tx mgr, but again how to inject it at beginning of "Step"? ----------// single tx context across 2 resources / data sources @Bean(name = "globalTransactionManager") public ChainedTransactionManager build(@Qualifier("batchConfigurer") BatchConfigurer configurer, @Qualifier("transactionManager") PlatformTransactionManager appTransactionManager) throws Exception { ChainedTransactionManager ctm = new ChainedTransactionManager(configurer.getTransactionManager(), appTransactionManager); return ctm; } – Arpit S Jun 12 '19 at 22:03