title: Spring batch use JpaPagingItemReader JpaItemWriter no transaction is in progress
body: <p>My project uses java config, the <code>DataSource</code> entityManagerFactory <code>PlatformTransactionManager</code> created by other java config file.</p>
  
  <p>When <code>ItemReader</code> read data, <code>ItemProcessor</code> data transformed <code>ItemWriter</code> has some error</p>
  
  <p>How can I solve this problem?</p>
  
  <p><br>
  Error message</p>
  
  <pre><code>2015-01-17 16:26:30.390 ERROR AbstractStep:225 - Encountered an error executing step testStep in job validJob
  javax.persistence.TransactionRequiredException: no transaction is in progress
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171)
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)  
  </code></pre>
  
  <p>How can I enable transactions on the following Code:</p>
  
  <pre><code>  @Configuration
          @EnableBatchProcessing
          public class JobConfiguration {   
  
       @Inject
              private PlatformTransactionManager transactionManager;
  
           @Inject
              private EntityManagerFactory emf;    
  
           @Bean
              public Job validJob(JobBuilderFactory jobBuilderFactory,Step step) {
                  return jobBuilderFactory.get("validJob").flow(step).end().build();
              }
  
              @Bean
              public Step testStep(StepBuilderFactory stepBuilderFactory, ItemReader&lt;PurchaseOrder&gt; reader,
                      ItemWriter&lt;PurchaseOrder&gt; writer, ItemProcessor&lt;PurchaseOrder, PurchaseOrder&gt; processor) {
                  return stepBuilderFactory.get("testStep")//
                          .transactionManager(transactionManager)//
                          .&lt;PurchaseOrder, PurchaseOrder&gt;chunk(100)//
                          .reader(reader)//
                          .processor(processor)//
                          .writer(writer)//
                          .build();
              }
  
              @Bean
              public ItemReader&lt;PurchaseOrder&gt; reader() {
                  JpaPagingItemReader&lt;PurchaseOrder&gt; reader = new JpaPagingItemReader&lt;PurchaseOrder&gt;();
                  reader.setEntityManagerFactory(emf);
                  reader.setQueryString(" select p from PurchaseOrder p ");
                  reader.setTransacted(false);
                  return reader;
              }
  
              @Bean
              public ItemProcessor&lt;PurchaseOrder, PurchaseOrder&gt; processor() {
                  return new PurchaseOrderItemProcessor();
              }
  
              @Bean
                          public ItemWriter&lt;PurchaseOrder&gt; writer() {
                  JpaItemWriter&lt;PurchaseOrder&gt; writer = new JpaItemWriter&lt;PurchaseOrder&gt;();
                  writer.setEntityManagerFactory(emf);
                  return writer;
  
              }
  </code></pre>