When the entity manager is injected, it is managed by the container, and you should not close it yourself.
So, for my scenario what do you think is the best ? Let the container manage it after injection or I create one and manage it myself ?
Let me know
In this scenario there is no advantage in using multiple entity managers, so you should let the container handle it.
If anything, in this scenario the use of multiple entity managers may slow down your app as it may mean updates / deletes are flushed more frequently than they need to be.
I suggest you code it the simplest way, ie container managed, then if performance is unacceptable look for bottlenecks to be optimised.
Thanks for your response.
Coming back to the problem, basically, I am supposed to update DB with contents of a file. If anything happens during data insertion, the whole transaction needs to be rolled back. So the reading of file and updating it into the DB should be in one transaction unit. Since there are multiple files to be read, I was wondering if I could open the entity manager before file-read/DB-update and close immediately after. Will CMP still yield better performance for this case too? Sorry if I sound repetitive.
I am just trying to understand how Container Manager Persistence (CMP) demarcates the transaction here and handles persistence.