Multiple Entity Manager Factories? [message #1856324] |
Fri, 02 December 2022 20:04  |
Eclipse User |
|
|
|
Basic question, I'm using EclipseLink 2.7 in a Tomcat (non-EE) server. My web-app has a base class that obtains an EMF with Persistence.createEntityManagerFactory. So each of 5 or 6 sub-classes are getting a distinct EMF instance. I should also point out that I'm using the deploy-on-startup property, which works great. Is this pattern a potential problem? Or, since behind the scenes, they all point to the same persistence unit (which is already deployed), does it matter? Should I create a singleton EMF? Could it be static instead?
|
|
|
Re: Multiple Entity Manager Factories? [message #1856347 is a reply to message #1856324] |
Mon, 05 December 2022 10:57  |
Eclipse User |
|
|
|
Generally it is better to reuse EntityManagerFactory instances yourself and handle the lifecycle rather than spin up new ones, but EclipseLink has some efficiencies to avoid re-loading and processing the persistence unit over and over if you don't. You will want to verify with a debugger, but EclipseLink uses EMFs to wrap ServerSessions and tie them to persistence units. Containers give different class loaders so that it can use entirely different persistence units, but outside of the container API, EclipseLink will be handing out EMF wrappers overtop of the same unit/serverSession. They should all be tied to the same ServerSession under the covers. This saves file loading, processing, and startup time, as most everything about them will be the same yet is very expensive to do on the fly. How this works in your Tomcat server will depend entirely on how you've deployed EclipseLink and its visibility to the application - statics depend on the class loader used to load them.
If you want/need truly independent EMFs, you can assign them different session names as a persistence property, which forces EclipseLink to create a new ServerSession for that name instead of reusing the one it might have on hand (which it names based on the persistence unit name and the class loader). Problems you might hit if you don't depend on what you do and what differences you expect on these EMFs. I don't remember the specifics, but the only time I needed distinct EMFs was in a project where I needed to handle multi tenancy myself on a per persistence unit basis - EclipseLink code could and can handle that though.
|
|
|
Powered by
FUDForum. Page generated in 0.05257 seconds