-
Bug
-
Resolution: Done
-
Major
-
1.3.0.Beta4
-
None
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.