1 Reply Latest reply on Jul 6, 2016 4:31 PM by Cheng Fang

    JPA and EJB writter

    jballesteros Newbie

      Why I dont find any example for an ItemReader using JPA and EJB? It is bad practice read from one database and write in another one?

       

      I have tried to read data from one table of a database, process it and save the process data in another table. The problem is that the reading process becomes an infinite loop, althought the database have only 100 records. This is the code that i have been using:

      @Named
      public class EjemploDBReader extends AbstractItemReader {

      public static int COUNT = 0;

      @EJB
      PersonaDao personaDao;
      public List personas;
      public EjemploDBReader() {
      }

      @Override
      public void open(Serializable checkpoint) throws Exception {
      personas = personaDao.obtenerTodos();
      }

      @Override
      public Persona readItem() throws Exception {
      try {
      for (Persona persona : personas) {
      COUNT++;
      return persona;
      }
      } catch (Exception e) {
      throw new Exception(“Error al leer Lista Personas: ” + e);
      }
      return null;
      }

      }

       

      This is mi job.xml file:

       

      <?xml version="1.0" encoding="UTF-8"?>

      <job id="funcionalidad1Job" xmlns="http://xmlns.jcp.org/xml/ns/javaee"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd"

        version="1.0">

        <step id="inicializarBatch" next="ejemplo1Step">

        <batchlet ref="ejemploBatchlet"/>

        </step>

        <step id="ejemplo1Step">

        <chunk checkpoint-policy="custom">

        <reader ref="ejemploDBReader"/>

        <processor ref="ejemploProcessor" />

        <writer ref="ejemploJpaWriterDB"></writer>

        <checkpoint-algorithm ref="algoritmoCheckPoint"/>

        </chunk>

        </step>

      </job>

        • 1. Re: JPA and EJB writter
          Cheng Fang Master

          looks like your readItem method always returns the first item in personas.  Although your COUNT is incremented, it is not checked against the total number of items during each iteration.

           

          For this purpose, COUNT should not be static.

           

          something like:

           

          if(count < persons.size()) {

               return persons.get(count++);

          }

          return null;