Skip to content

#7865 Spring Batch : Fix PostgreSQL with defaultAutoCommit=false #7866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -81,6 +85,8 @@ public class JobLauncherCommandLineRunner

private ApplicationEventPublisher publisher;

private PlatformTransactionManager transactionManager;

public JobLauncherCommandLineRunner(JobLauncher jobLauncher,
JobExplorer jobExplorer) {
this.jobLauncher = jobLauncher;
Expand Down Expand Up @@ -111,6 +117,11 @@ public void setJobs(Collection<Job> 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));
Expand Down Expand Up @@ -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<JobParameters>() {
@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) {
Expand Down