Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring batch - One transaction over whole Job

I am using Spring-Batch to execute a batch that creates some objects in the database, creates a file from these objects and then sends the file to a FTP server.

Thus, I have 2 steps : One that reads conf from DB, insert into the DB and creates the file ; the second sends the file to the FTP server.

The problem is when there is a problem with the FTP server, I can't rollback the transaction (to cancel the new inserts into the DB).

How can I configure my Job to use just one transaction over the different steps?

like image 520
Zava Avatar asked Sep 26 '13 14:09

Zava


People also ask

How does Spring Batch handle transactions?

Spring Batch handles transactions at the step level. This means that Spring Batch will never use only one transaction for a whole job (unless the job has a single step). Remember that you're likely to implement a Spring Batch job in one of two ways: using a tasklet or using a chunk-oriented step.

Can a Spring Batch have multiple jobs?

Multiple jobs can be run simultaneously. There are two main types of Spring Batch Parallel Processing: Single Process, Multi-threaded, or Multi-process. These are also divided into subcategories, as follows: Multi-threaded Step (Step with many threads, single process)

How do you run two jobs sequentially in Spring Batch?

Once context is initialized, either you can do - JobLauncher jobLauncher = (JobLauncher) ctx. getBean("jobLauncher"); or do Autowired for this JobLauncher bean in main class and launch specific jobs sequentially in specific sequential order by invoking , jobLauncher. run(job, jobParameters) .


2 Answers

A agree with bellabax: this is a bad idea.

But I wouldn't do a 3rd cleanup step because this step may also fail, letting the transaction not rollbacked.

You could mark the inserted entries with a flag that indicates the entries has not yet been sent to the FTP. The 3rd step would switch the flag to indicate that these entries has been sent to the FTP.

Then you just need a cron/batch/4th cleaning step/whatever that would remove all entries that haven't been sent to the FTP

like image 39
Sebastien Lorber Avatar answered Oct 16 '22 15:10

Sebastien Lorber


This is a bad idea due to transactional nature of spring-batch.
IMHO a simple solution should be to mark data saved in step 1 with a token generated when job starts and, if your FTP upload will fail, move to a cleanup step to delete all data with token.

like image 81
Luca Basso Ricci Avatar answered Oct 16 '22 16:10

Luca Basso Ricci