glassfish
  1. glassfish
  2. GLASSFISH-20304

BATCH RI: Can't inject an EntityManager in an ItemWriter implementation

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 4.0_b84_RC1
    • Fix Version/s: 4.0_b85
    • Component/s: batch
    • Labels:
      None
    • Environment:

      Glassfish promoted b84.

      Description

      Injecting an EntityManager using @PersistenceContext in an ItemWriter implementation results in a null pointer. For example:

      ...
      public class MyWriter implements ItemWriter {
      @PersistenceContext
      EntityManager em;

      @Override
      public void open(Serializable s) throws Exception

      { System.out.println(em.toString); // em is a null pointer }

      ...
      }

        Activity

        Hide
        rcervera added a comment -

        We will cover this use case in the documentation and examples, unless something changes.

        Show
        rcervera added a comment - We will cover this use case in the documentation and examples, unless something changes.
        Hide
        Mahesh Kannan added a comment -

        Based on Scott's comment, marking this as resolved

        Show
        Mahesh Kannan added a comment - Based on Scott's comment, marking this as resolved
        Hide
        jimnicolson added a comment -

        Although this is closed, I have just hit this problem for GF 4 Build 89 (Final release).

        I've tried an EJB and a WAR project, with and without a valid persistence.xml and injecting EntityManager using @PersistenceContext and custom CDI Producer annotation.

        The Batch processing actions are functional, EcliseLink/JPA initialization seems to be behaving normally, but no EntityManager.

        Injection returns null on all three Batch classes Reader, Processor, Writer.

        I'm happy to be wrong but at this point there still seems to be a problem.

        I have a test WAR (with source included) (17K) but first just making an inquiry sine I'm a newbee to this site.

        Show
        jimnicolson added a comment - Although this is closed, I have just hit this problem for GF 4 Build 89 (Final release). I've tried an EJB and a WAR project, with and without a valid persistence.xml and injecting EntityManager using @PersistenceContext and custom CDI Producer annotation. The Batch processing actions are functional, EcliseLink/JPA initialization seems to be behaving normally, but no EntityManager. Injection returns null on all three Batch classes Reader, Processor, Writer. I'm happy to be wrong but at this point there still seems to be a problem. I have a test WAR (with source included) (17K) but first just making an inquiry sine I'm a newbee to this site.
        Hide
        rcervera added a comment -

        jimnicolson, this is no longer an issue; injecting an EntityManager should work. Please see the phonebilling example in the EE 7 tutorial for an example of a batch job that injects an EntityManager:

        http://docs.oracle.com/javaee/7/tutorial/doc/batch-processing009.htm

        The code for the example is included with the EE 7 SDK.

        Show
        rcervera added a comment - jimnicolson, this is no longer an issue; injecting an EntityManager should work. Please see the phonebilling example in the EE 7 tutorial for an example of a batch job that injects an EntityManager: http://docs.oracle.com/javaee/7/tutorial/doc/batch-processing009.htm The code for the example is included with the EE 7 SDK.
        Hide
        jimnicolson added a comment -

        Thanks it's working but just for future reference:

        From @Named to @Named("SimpleBatchReader") resolved the problem.

        (I didn't need to add a no-arg constructor to have this work but will do so.)

        @Dependent
        @Named("SimpleBatchReader")
        public class SimpleBatchReader extends AbstractItemReader {

        @PersistenceContext
        EntityManager em;

        @Override
        public void open(Serializable checkpoint) throws Exception

        { System.out.println("SimpleBatchReader.open em=" + em); }

        ....

        Looking at the Java EE 7 Tutorial docs (http://docs.oracle.com/javaee/7/tutorial/doc/batch-processing005.htm#BCGIFJBB).

        I suggest that it might be a useful addition to add an explicit note/clarification of this 'side-effect'.

        Especially as the Batch Job 'works' and there are examples from credible sources (e.g. https://glassfish.java.net/hol/javaee7-hol.pdf See p40) whihc don't clarify this.

        i.e. it is not clear that @Named without the string and using a batch.xml in META-INF has this side-effect (when in fact they are alternatives ?)

        I had both - seems obvious now of course

        Show
        jimnicolson added a comment - Thanks it's working but just for future reference: From @Named to @Named("SimpleBatchReader") resolved the problem. (I didn't need to add a no-arg constructor to have this work but will do so.) @Dependent @Named("SimpleBatchReader") public class SimpleBatchReader extends AbstractItemReader { @PersistenceContext EntityManager em; @Override public void open(Serializable checkpoint) throws Exception { System.out.println("SimpleBatchReader.open em=" + em); } .... Looking at the Java EE 7 Tutorial docs ( http://docs.oracle.com/javaee/7/tutorial/doc/batch-processing005.htm#BCGIFJBB ). I suggest that it might be a useful addition to add an explicit note/clarification of this 'side-effect'. Especially as the Batch Job 'works' and there are examples from credible sources (e.g. https://glassfish.java.net/hol/javaee7-hol.pdf See p40) whihc don't clarify this. i.e. it is not clear that @Named without the string and using a batch.xml in META-INF has this side-effect (when in fact they are alternatives ?) I had both - seems obvious now of course

          People

          • Assignee:
            Mahesh Kannan
            Reporter:
            rcervera
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: