Announcement Announcement Module
Collapse
No announcement yet.
Problem with shared DB connection when running a job concurrently with Quartz Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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.item.support.AbstractIte mCountingItemStreamItemReader.open(AbstractItemCou ntingItemStreamItemReader.java:137)
    at org.springframework.batch.item.support.CompositeIt emStream.open(CompositeItemStream.java:93)
    at org.springframework.batch.core.step.tasklet.Taskle tStep.open(TaskletStep.java:301)
    at org.springframework.batch.core.step.AbstractStep.e xecute(AbstractStep.java:192)
    at org.springframework.batch.core.job.SimpleStepHandl er.handleStep(SimpleStepHandler.java:135)
    at org.springframework.batch.core.job.flow.JobFlowExe cutor.executeStep(JobFlowExecutor.java:61)
    at org.springframework.batch.core.job.flow.support.st ate.StepState.handle(StepState.java:60)
    at org.springframework.batch.core.job.flow.support.Si mpleFlow.resume(SimpleFlow.java:144)
    at org.springframework.batch.core.job.flow.support.Si mpleFlow.start(SimpleFlow.java:124)
    at org.springframework.batch.core.job.flow.FlowJob.do Execute(FlowJob.java:135)
    at org.springframework.batch.core.job.AbstractJob.exe cute(AbstractJob.java:281)
    at org.springframework.batch.core.launch.support.Simp leJobLauncher$1.run(SimpleJobLauncher.java:120)
    at org.springframework.core.task.SyncTaskExecutor.exe cute(SyncTaskExecutor.java:48)
    at org.springframework.batch.core.launch.support.Simp leJobLauncher.run(SimpleJobLauncher.java:114)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMetho dSite$PojoCachedMethodSiteNoUnwrapNoCoerce.invoke( PojoMetaMethodSite.java:229)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMetho dSite.call(PojoMetaMethodSite.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallS ite.call(AbstractCallSite.java:128)
    at com.dealer.epd.batch.DailyBatch.executeInternal(Da ilyBatch.groovy:23)
    at org.springframework.scheduling.quartz.QuartzJobBea n.execute(QuartzJobBean.java:86)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:2 02)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run (SimpleThreadPool.java:525)
    Caused by: java.lang.IllegalStateException: Stream is already initialized. Close before re-opening.
    at org.springframework.util.Assert.state(Assert.java: 384)
    at org.springframework.batch.item.database.AbstractCu rsorItemReader.doOpen(AbstractCursorItemReader.jav a:397)
    at org.springframework.batch.item.support.AbstractIte mCountingItemStreamItemReader.open(AbstractItemCou ntingItemStreamItemReader.java:134)
    ... 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

    Comment


    • #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!

      Comment

      Working...
      X