4 Replies Latest reply on May 16, 2014 10:52 AM by Andrea Fantappiè

    Java batch (JSR352), JPA and chunk transactions problem

    Andrea Fantappiè Newbie

      Hi,

      I've wrote a simple java batch job with a single chunk that read a csv file and for each row build and persist a simple JPA entity. I've configured the chunk with a commit interval of 7 items. As explained on JSR352 the transaction is bound to the entire chunk so I expected that if an exception occured during the chunk process (in my code on a failure item write) the transaction rollback all the entities persisted by the active chunk.

       

      The problem is that during ItemWriter processing, inside the loop, I do an EntityManager.persist(myitem) (the EntityManager is injected with @PersistenceContext) and synchronously hibernate send and commit the insert sql as there is no transaction active (or a kind of autocommit), before the method exit. If the loop throw and exception the only effect is the job end, but entities persisted in the chunk are still there on the database!

       

      Same code on Glassfish 4 work fine

       

      Using the same injected persistence unit outside the job context (like an ejb timer) transactions works, so i suppose that is not a datasource configuration issue.

       

      Any idea? Am I missing some extra configuration for java batch transactions?