title: Why does destroy method 'close' fail for JPAPagingItemReader configured with Java config?
body: <p>We are trying to convert our Spring-Batch jobs from XML configuration to Java configuration. We are using Spring 4.0.1.RELEASE and Spring Batch 2.2.1.RELEASE.</p>
  
  <p>After converting one job, the following warning started to appear in the log file:</p>
  
  <blockquote>
    <p>15-Apr-2014 09:59:26.335 [Thread-2] WARN  o.s.b.f.s.DisposableBeanAdapter - Invocation of destroy method 'close' failed on bean with name 'fileReader': org.springframework.batch.item.ItemStreamException: Error while closing item reader</p>
  </blockquote>
  
  <p>The full stacktrace is:</p>
  
  <pre><code>org.springframework.batch.item.ItemStreamException: Error while closing item reader
      at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close(AbstractItemCountingItemStreamItemReader.java:131) ~[spring-batch-infrastructure-2.2.1.RELEASE.jar:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_25]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_25]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_25]
      at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_25]
      at org.springframework.beans.factory.support.DisposableBeanAdapter.invokeCustomDestroyMethod(DisposableBeanAdapter.java:349) [spring-beans-4.0.1.RELEASE.jar:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:272) [spring-beans-4.0.1.RELEASE.jar:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:540) [spring-beans-4.0.1.RELEASE.jar:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:516) [spring-beans-4.0.1.RELEASE.jar:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:824) [spring-beans-4.0.1.RELEASE.jar:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:485) [spring-beans-4.0.1.RELEASE.jar:4.0.1.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:921) [spring-context-4.0.1.RELEASE.jar:4.0.1.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:895) [spring-context-4.0.1.RELEASE.jar:4.0.1.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:809) [spring-context-4.0.1.RELEASE.jar:4.0.1.RELEASE]
  Caused by: java.lang.IllegalStateException: EntityManager is closed
      at org.hibernate.ejb.EntityManagerImpl.close(EntityManagerImpl.java:132) ~[hibernate-entitymanager-4.2.5.Final.jar:4.2.5.Final]
      at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source) ~[na:na]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_25]
      at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_25]
      at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:334) ~[spring-orm-4.0.1.RELEASE.jar:4.0.1.RELEASE]
      at $Proxy67.close(Unknown Source) ~[na:na]
      at org.springframework.batch.item.database.JpaPagingItemReader.doClose(JpaPagingItemReader.java:236) ~[spring-batch-infrastructure-2.2.1.RELEASE.jar:na]
      at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close(AbstractItemCountingItemStreamItemReader.java:128) ~[spring-batch-infrastructure-2.2.1.RELEASE.jar:na]
      ... 13 common frames omitted
  </code></pre>
  
  <p>This error only appears when using the Java configuration for the job but not the XML configuration. The step configured using XML looks like this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;batch:step id="createFile" next="insertFile"&gt;
      &lt;batch:tasklet&gt;
          &lt;batch:chunk reader="fileReader" writer="fileWriter"
              commit-interval="#{jobProperties[commit_interval]}" /&gt;
      &lt;/batch:tasklet&gt;
  &lt;/batch:step&gt;
  
  &lt;bean id="fileReader"
      class="org.springframework.batch.item.database.JpaPagingItemReader"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;property name="queryString"
          value="select mt from MyTable mt where status in ('1','2','3')" /&gt;
      &lt;property name="pageSize" value="1000" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The Java configuration is:</p>
  
  <pre><code>@Bean
  public Job fileProcessJob(JobBuilderFactory jobBuilders,
          Step loadConfig,
          Step createFile,
          Step insertFile
          ) {
      return jobBuilders.get(moduleName)
              .start(loadConfig)
              .next(createFile)
              .next(insertFile)
              .build()
              .build();
  }
  
  @Bean
  public ItemReader&lt;MyTable&gt; cetFileReader(EntityManagerFactory entityManagerFactory) {
      JpaPagingItemReader&lt;MyTable&gt; itemReader = new JpaPagingItemReader&lt;MyTable&gt;();
      itemReader.setEntityManagerFactory(entityManagerFactory);
      itemReader.setQueryString("select mt from MyTable mt where status in ('1','2','3')");
      itemReader.setPageSize(1000);
      return itemReader;
  }
  </code></pre>
  
  <p>Why does this warning appear in the logs when using Java configuration but not XML configuration?</p>