Uploaded image for project: 'JBeret'
  1. JBeret
  2. JBERET-305

Open / close method is not the best place to create / close entity manager for JpaItemReader and JpaItemWriter

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • Major
    • 1.3.0.Beta5
    • 1.3.0.Beta4
    • jberet-support
    • None

    Description

      JpaItemReader and JpaItemWriter currently initialize the EntityManager (and sometimes EntityManagerFactory) in the open() method of the reader or writer, and close them in the close() method of the reader or writer. However, this is not the best place to perform these lifecycle operations.

      Instead, we shoudl initialize EntityManager and EntityManagerFactory in @PostConstruct method, and close them in @PreDestroy method, to make sure thes expensive lifecycle operations are only done once in the life span of the batch artifact.

      The open() and close() method of JpaItemReader and JpaItemWriter are invoked multiple times during the life of the component, e.g., when retrying after a retryable exception. In this case, we don't really need to re-initialize the EntityManager.

      EntityManager can also be optionally injected via @Inject field injection, which happens only once during the component initialization. So moving EntityManager initialization logic to @PostConstruct is also consistent with injection of EntityManager.

      Attachments

        Activity

          People

            cfang@redhat.com Cheng Fang
            cfang@redhat.com Cheng Fang
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: