Results 1 to 5 of 5

Thread: Getting session closed error when using JpaPagingItemReader

  1. #1

    Default Getting session closed error when using JpaPagingItemReader

    Hi,

    I am getting the below error message when using the JpaPagingItemReader

    Code:
    03-10 15:41:16 ERROR [main] step.AbstractStep - Exception while closing step execution resources
    org.springframework.batch.item.ItemStreamException: Error while closing item reader
    	at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close(AbstractItemCountingItemStreamItemReader.java:101)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    	at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
    	at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    	at $Proxy32.close(Unknown Source)
    	at org.springframework.batch.item.support.CompositeItemStream.close(CompositeItemStream.java:86)
    	at org.springframework.batch.core.step.item.ChunkMonitor.close(ChunkMonitor.java:100)
    	at org.springframework.batch.item.support.CompositeItemStream.close(CompositeItemStream.java:86)
    	at org.springframework.batch.core.step.tasklet.TaskletStep.close(TaskletStep.java:360)
    	at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:255)
    	at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:348)
    	at org.springframework.batch.core.job.flow.FlowJob.access$100(FlowJob.java:43)
    	at org.springframework.batch.core.job.flow.FlowJob$JobFlowExecutor.executeStep(FlowJob.java:137)
    	at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60)
    	at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:144)
    	at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:124)
    	at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:105)
    	at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:250)
    	at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:110)
    	at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:48)
    	at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:105)
    	at org.nmdp.b2b.gateway.job.launcher.TestJobLauncher.main(TestJobLauncher.java:22)
    Caused by: org.hibernate.SessionException: Session was already closed
    	at org.hibernate.impl.SessionImpl.close(SessionImpl.java:275)
    	at org.hibernate.ejb.EntityManagerImpl.close(EntityManagerImpl.java:125)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
    	at $Proxy39.close(Unknown Source)
    	at org.springframework.batch.item.database.JpaPagingItemReader.doClose(JpaPagingItemReader.java:140)
    	at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close(AbstractItemCountingItemStreamItemReader.java:98)
    	... 29 more
    Below is the configuration for the JpaPagingItemReader

    Code:
    	<bean id="jpaItemReader" class="org.springframework.batch.item.database.JpaPagingItemReader" scope="step">
    	    <property name="entityManagerFactory" ref="txEntityManager"/>
    	    <property name="queryString" value="select tc from table tc where tc.column_name='value'"/>
    	</bean>
    
    	<bean id="txEntityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="persistence-unit-name" />
    	</bean>
    My first batch read was successful and this happens only in the second batch.

    Your help is much appreciated.

    Thanks.

  2. #2
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    Can we see the complete configuration for the Step? What do you mean by "second batch"? The second transaction (chunk)?

  3. #3

    Default

    Hi Dave,

    Below is the complete Job configuration. Yes, I meant the start of second transaction in the step

    Code:
    	<batch:job id="tJob" job-repository="jobRepository">		
    		<batch:step id="pFile">
    			<batch:tasklet>
    				<batch:chunk reader="tItemReader" processor="tProcessor" writer="tWriter" commit-interval="100" skip-limit="9999999">
    					<batch:skippable-exception-classes>
    						<batch:include class="com.company.validation.ValidationException"/>
    					</batch:skippable-exception-classes>
    				</batch:chunk>
    				<batch:listeners merge="true">
    					<batch:listener><bean class="com.listener.JobListener1"/></batch:listener>
    					<batch:listener><bean class="com.listener.JobListener2"/></batch:listener>
    				</batch:listeners>
    			</batch:tasklet>
    			<batch:end on="*" />
    		</batch:step>		
    		<batch:listeners>
    			<batch:listener><bean class="com.company.JobExecutionFailureListener"/></batch:listener>
    		</batch:listeners>	
    	</batch:job>

  4. #4

    Default

    Dave, can you please throw some light on this?

  5. #5
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    Not without more detail. But I can guess. You are using a JPA ItemReader which operates in a separate transaction to the processor and writer. It's fine on a sunny day, but if you load any lazy associations in your processor or writer and then there is a rollback it can fail. Frankly, there are so many failure scenarios for JPA ItemReader that I wouldn't bother with it at all if I were you - just retrieve the primary keys and load the entities in the processor. You can use 2nd level cache for performance if you have a lot of rollbacks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •