diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java index ec6c78e55117..03d7b5fd78bf 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java @@ -50,6 +50,10 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.TransactionCallback; +import org.springframework.transaction.support.TransactionTemplate; import org.springframework.util.PatternMatchUtils; import org.springframework.util.StringUtils; @@ -81,6 +85,8 @@ public class JobLauncherCommandLineRunner private ApplicationEventPublisher publisher; + private PlatformTransactionManager transactionManager; + public JobLauncherCommandLineRunner(JobLauncher jobLauncher, JobExplorer jobExplorer) { this.jobLauncher = jobLauncher; @@ -111,6 +117,11 @@ public void setJobs(Collection jobs) { this.jobs = jobs; } + @Autowired(required = false) + public void setTransactionManager(PlatformTransactionManager transactionManager) { + this.transactionManager = transactionManager; + } + @Override public void run(String... args) throws JobExecutionException { logger.info("Running default command line with: " + Arrays.asList(args)); @@ -205,11 +216,25 @@ private void executeRegisteredJobs(JobParameters jobParameters) } } - protected void execute(Job job, JobParameters jobParameters) + protected void execute(final Job job, final JobParameters jobParameters) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException, JobParametersNotFoundException { - JobParameters nextParameters = getNextJobParameters(job, jobParameters); + + JobParameters nextParameters; + if (this.transactionManager != null) { + TransactionTemplate transactionTemplate = new TransactionTemplate(this.transactionManager); + nextParameters = transactionTemplate.execute(new TransactionCallback() { + @Override + public JobParameters doInTransaction(TransactionStatus transactionStatus) { + return getNextJobParameters(job, jobParameters); + } + }); + } + else { + nextParameters = getNextJobParameters(job, jobParameters); + } + if (nextParameters != null) { JobExecution execution = this.jobLauncher.run(job, nextParameters); if (this.publisher != null) {