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.
org.springframework.batch.core.repository.dao.NoSuchObjectException: Invalid JobExecution, ID 8 not found.
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)