I have an issue when executing a new job asynchronously, through a web request. My web controller is wrapped by a TransactionInterceptor, and calls a SimpleJobLauncher configured with a SimpleAsyncTaskExecutor. The Launcher calls jobRepository.createJobExecution(), then the Job is executed in a new thread, and therefore outside my transaction. The transaction in which the jobExecution is created has not committed until the web controller completes.
Because the job execution begins before the transaction on the web request thread has committed, the job's thread does not see the newly created job and throws
With a breakpoint on the Job's execute() method, the problem can be avoided by giving the web request time to return.Code:org.springframework.batch.core.repository.dao.NoSuchObjectException: Invalid JobExecution, ID 8 not found. at org.springframework.batch.core.repository.dao.JdbcJobExecutionDao.updateJobExecution(JdbcJobExecutionDao.java:154) at org.springframework.batch.core.repository.support.SimpleJobRepository.saveOrUpdate(SimpleJobRepository.java:214) at org.springframework.batch.core.job.SimpleJob.execute(SimpleJob.java:159) at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:90) at java.lang.Thread.run(Thread.java:613)
It seems that the transaction isolation on the thread calling the Executor must be ISOLATION_READ_UNCOMMITTED for this use case to work. Does that seem true? It feels dirty. (haha "dirty read" pun)
Thanks!


Reply With Quote
