Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Multiple Entity Manager Factories?
Multiple Entity Manager Factories? [message #1856324] Sat, 03 December 2022 01:04 Go to next message
Guy McArthur is currently offline Guy McArthurFriend
Messages: 2
Registered: July 2009
Junior Member
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 15:57 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 43
Registered: December 2021
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.
Previous Topic:L2 cache with OneToMany relationships
Next Topic:Unable to download EclipseLink 4.0 Documentation PDFs
Goto Forum:

Current Time: Fri Dec 01 14:17:39 GMT 2023

Powered by FUDForum. Page generated in 0.02011 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top