0

Is there any configuration for spring-batch, or in the datasources configurations, where I can disable/ignore all commits?

Basically, I want to execute my batch without updating the database just for testing. This would be perfect if I could disable and enable commits with a property

Edit: I don't want to use an in memory database because I want to process millions of lines that I have in the database. The batch reads lines from database and changes the state those lines after processing and I don't want to persist this changes.

rMonteiro
  • 1,371
  • 1
  • 14
  • 37
  • you can set ``autoCommit=false`` so nothing will be committed to database. Please check this thread for property details https://stackoverflow.com/questions/55343616/how-to-disable-autocommit-spring-boot – pratap Jun 01 '21 at 12:36
  • You can make use of Spring batch [In-Memory Repository](https://docs.spring.io/spring-batch/docs/current/reference/html/job.html#inMemoryRepository) – Rahul Dey Jun 01 '21 at 14:53
  • 1
    Does your job requires interacting with the database for data other than the meta-data of Spring Batch? – Mahmoud Ben Hassine Jun 02 '21 at 08:24
  • @MahmoudBenHassine yes my job reads and writes data from database, thats why I want to run the batch without changing values from database. I edited the post to explain why I don't use in Memory Database – rMonteiro Jun 02 '21 at 12:46
  • 1
    `The batch reads lines from database and changes the state those lines after processing and I don't want to persist this changes.`: What are you doing with those modified records? You can still use a `ResourcelessTransactionManager` on the step or disable the transaction of the step with Propagation.NOT_SUPPORTED, see https://stackoverflow.com/questions/55117663/disable-transactions-in-my-spring-batch-job. But in that case, what's the point of using Spring Batch here? – Mahmoud Ben Hassine Jun 02 '21 at 12:56
  • @MahmoudBenHassine thanks, I will analyse and try both options to see which one is best in my case. I will reply later with my solution – rMonteiro Jun 02 '21 at 12:59
  • The `Propagation.NOT_SUPPORTED` does not work because it occurs the following error on `ItemWriter`: `No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call` – rMonteiro Jun 02 '21 at 15:16
  • Which item writer are you using? You did not specify that in the question and that's key. If your item writer is expected to run within a transaction, then disabling the transaction all together with `Propagation.NOT_SUPPORTED` is obviously not the way to go. That's why there is the `ResourcelessTransactionManager` for the step so all components are still run within a transaction (precisely a `ResourcelessTransaction`) without really interacting with the db. This is what I said in my answer. – Mahmoud Ben Hassine Jun 03 '21 at 07:32

1 Answers1

2

You can use the ResourcelessTransactionManager for that. It is a No-Op implementation of PlatformTransactionManager which means there will be no real transaction ongoing against a transactional resource (hence the term Resourceless). You can use it when you don't really need (or care about) transactional behaviour, for example in tests/prototypes or when using a non-transactional job repository.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50