Announcement Announcement Module
No announcement yet.
Problem with shared DB connection when running a job concurrently with Quartz Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with shared DB connection when running a job concurrently with Quartz

    I am running a single batch job with Quartz set to run every 15 seconds. The job will connect to a database with a jdbc reader, select a subset of rows and write objects to a separate database. The first run will kick off fine. It will open a connection to the database and start reading and write rows. When the second job starts 15 seconds later, and the first job is still running, it will try to open a connection to the database. Rather than getting a new connection from the pool of connections, it tries to open the same connection, which results in the following error:

    2011-12-07 14:57:45,034 [org.springframework.scheduling.quartz.SchedulerFac toryBean#0_Worker-4] ERROR step.AbstractStep - Encountered an error executing the step
    org.springframework.batch.item.ItemStreamException : Failed to initialize the reader
    at org.springframework.batch.core.step.tasklet.Taskle
    at org.springframework.batch.core.step.AbstractStep.e xecute(
    at org.springframework.batch.core.job.SimpleStepHandl er.handleStep(
    at org.springframework.batch.core.job.flow.JobFlowExe cutor.executeStep(
    at ate.StepState.handle(
    at mpleFlow.resume(
    at mpleFlow.start(
    at Execute(
    at org.springframework.batch.core.job.AbstractJob.exe cute(
    at leJobLauncher$
    at org.springframework.core.task.SyncTaskExecutor.exe cute(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De
    at java.lang.reflect.Method.invoke(
    at org.codehaus.groovy.runtime.callsite.PojoMetaMetho dSite$PojoCachedMethodSiteNoUnwrapNoCoerce.invoke(
    at org.codehaus.groovy.runtime.callsite.PojoMetaMetho
    at org.codehaus.groovy.runtime.callsite.AbstractCallS
    at ilyBatch.groovy:23)
    at org.springframework.scheduling.quartz.QuartzJobBea n.execute(
    at 02)
    at org.quartz.simpl.SimpleThreadPool$ (
    Caused by: java.lang.IllegalStateException: Stream is already initialized. Close before re-opening.
    at org.springframework.util.Assert.state( 384)
    at org.springframework.batch.item.database.AbstractCu rsorItemReader.doOpen(AbstractCursorItemReader.jav a:397)
    ... 24 more

    I have tried defining scope="prototype" on my reader and writer, but it seems that the job itself is a singleton and therefore only ever requests one reader and one writer and therefore gets one connection and tries to reopen it. How can I run the same job concurrently without this issue? I believe a solution may be to set the scope on the job itself, but there is no scope parameter on job. Help is appreciated. Thanks!

  • #2
    I have tried defining scope="prototype" on my reader and writer
    afaik it does not work (yet), use scope="step" instead


    • #3
      It works!

      Thank you so much! Nothing I searched for pointed me in this direction, so I appreciate your help very much. Each of the solutions for using scope="step" worked, but I chose to use namespace 'batch' for clarity and my IDE likes it better . It would be nice if the documentation was a bit more explicit about this be necessary for concurrency. Thanks again!