Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 352708

Summary: EntityManagerFactoryBuilder to create more than one EntityManagerFactory
Product: [RT] Gemini.JPA Reporter: Missing name <ephikles>
Component: CoreAssignee: Michael Keith <michael.keith>
Status: RESOLVED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: cvgaviao, eduard.bartsch, edufrazao, gunnar, juergen.kissner, laeubi, nepomuk.seiler, shaun.smith, xu.xiang
Version: 1.0.0Flags: michael.keith: iplog+
Target Milestone: ---   
Hardware: All   
OS: All   
URL: http://www.eclipse.org/forums/index.php/m/699026/
Whiteboard:
Attachments:
Description Flags
Gemini JPA patch that uses EL feature for multiple EMFs per PU
none
Slightly updated patch to also check for EclipseLink session name property none

Description Missing name CLA 2011-07-21 04:32:29 EDT
According to the JPA-specs (5.3 Obtaining an Entity Manager Factory) "More than one entity manager factory instance may be available simultaneously in the JVM.", but also that "There is only one entity manager factory per persistence unit".

Unfortunately, Gemini JPA's EMFB design doesn't support multiple EMF creation.
Typically that's sufficient for simple scenarios where there's only one datasource, or even a set of datasources that never change. In a multi-datasource environment, where during an application's lifetime new datasources come into play and need to be accessed, and where you have to decide on each call which database is the target of your operation, you're at a loss.

However, the fact that there's only one EMF allowed per persistence unit remains.
The main identifier for an punit is it's name. So I wish for a way to use your EMFB to create a new EMF for a new datasource (via the 'properties' parameter) and, to be in line with the specs, a new punit (which would/could/should be, in fact, a renamed "clone" of the punit at hand).

The EMFs could still be cached and reused, identified by their punits or punit's names.

I hope that this is possible and fits into your agenda!?

See also: http://www.eclipse.org/forums/index.php/m/699026/

Kind regards, Florian
Comment 1 Michael Keith CLA 2011-07-21 12:26:54 EDT
This enhancement will be added when the EclipseLink bug #352506 is addressed.
Comment 2 Eduard Bartsch CLA 2011-07-22 03:19:08 EDT
(In reply to comment #1)
> This enhancement will be added when the EclipseLink bug #352506 is addressed.

Actually, EclipseLink is capable of providing/handling multiple EntityManagerFactory instances for the same persistence unit without creation of new PUs.

Subsequent calls to Persistence.createEntityManagerFactory(puName,props) with different JDBC URL and username/pwd will create multiple EMF instances that point to different databases or DB users. 

Internally, EclipseLink uses a concept of DB sessions that are differentiated by unique keys like <PU_Name>_nonJtaDataSource=<DS_ID>_url=<JDBC_URL>_user=<JDBC_USER> that include persistent unit name, datasource, JDBC URL and JDBC user.

I will attach a patch to Gemini JPA that demonstrates how this EclipseLink feature can be enabled with Gemini JPA for incomplete persistence units.
Comment 3 Eduard Bartsch CLA 2011-07-22 03:22:39 EDT
Created attachment 200150 [details]
Gemini JPA patch that uses EL feature for multiple EMFs per PU

Here is the promised patch that patches Gemini JPA and adds a test class that checks that two EMFs for the same PU really work with different DBs.
Comment 4 Missing name CLA 2011-07-22 03:52:39 EDT
wow..hey, i depended on you guys stalling me for a couple of days, but now it seems like i have to get back to do some actual work again ;)
no, just kiddin'.. works like a charm on first glance. now i gotta fit it into my whole datasource- and emf-creation logic.

thanks alot!! f.
Comment 5 Michael Keith CLA 2011-07-22 11:28:14 EDT
> Actually, EclipseLink is capable of providing/handling multiple
> EntityManagerFactory instances for the same persistence unit without creation
> of new PUs.

Yes, thanks for bringing this up. I'm aware of what EclipseLink can do, but this is a misfeature and completely breaks the JPA persistence unit model. Having multiple logical persistence units of the same name but configured differently is incorrect and not something that I will want to support in Gemini JPA. The proper way is to do as I suggested and create a new persistence unit. I know it will be easy precisely because EclipseLink already can create a new ServerSession (EMF-level db connections and cache) on demand.
Comment 6 Missing name CLA 2011-07-22 12:21:53 EDT
Somehow I feared you'd say something like that. Well, I suppose I will take Eduard's patch as an interim solution until the clean and good one is out..
Comment 7 Eduard Bartsch CLA 2011-07-25 02:35:48 EDT
I expected it somehow as well. My fix is simple but it resides in the gray zone beyond the spec. 

So if we have a chance to get a JPA spec compliant solution soon it would be the best. Let's continue the discussion on bug #352506 since I have some questions about the solution.
Comment 8 Gunnar Wagenknecht CLA 2011-12-14 02:17:23 EST
*** Bug 365833 has been marked as a duplicate of this bug. ***
Comment 9 Gunnar Wagenknecht CLA 2011-12-14 02:22:52 EST
(In reply to comment #5)
> > Actually, EclipseLink is capable of providing/handling multiple
> > EntityManagerFactory instances for the same persistence unit without creation
> > of new PUs.
> 
> Yes, thanks for bringing this up. I'm aware of what EclipseLink can do, but
> this is a misfeature and completely breaks the JPA persistence unit model.

FWIW, I applied the patch to latest Gemini and modified it to check for the "eclipselink.session-name" property. This property was recommended to me as the proper way of enabling multiple schema based multi-tenancy in EclipseLink.

Given that this is a rare use-case I wonder why it's not ok to apply this patch which requires a property to be set. Users of this property likely know the consequences. Multi-tenancy is a nice feature of EclipseLink and allowing Gemini JPA to support this without a lot overhead (eg. creating dummy PUs) doesn't sound too bad.
Comment 10 Gunnar Wagenknecht CLA 2011-12-14 02:26:27 EST
Created attachment 208367 [details]
Slightly updated patch to also check for EclipseLink session name property
Comment 11 Gunnar Wagenknecht CLA 2012-01-04 06:27:38 EST
Mike, from your descriptions is looks like that that your preferred solution requires more time to implement and won't be available soon. 

Sorry if I may sound impatient. But we'd like to start shipping a Gemini JPA/EclipseLink integration together with our Eclipse project soon. The attached patch runs fine. The only option I see currently is delivering the patch by myself which  I really don't like to do. 

Is there any chance we can have a workable solution for that issue soon which won't require to ship a patched version?
Comment 12 Michael Keith CLA 2012-01-06 20:08:16 EST
I can look at supporting this solution temporarily, requiring a property, and with a warning that it is only for certain use cases.
Comment 13 Gunnar Wagenknecht CLA 2012-01-07 05:10:32 EST
Thanks a lot Mike!
Comment 14 Eduardo Frazão CLA 2012-03-26 15:46:54 EDT
Hi all! First of all, thanks a lot for the patches provided here.
My scenario is exactly as Gunnar has mentioned.

I'm using Eclipselink 2.3.x with Virgo, with a dynamic multitenant properties.

I need to use a Shared EntityManager Factory per application logged tentant, so, the second patch was my only solution. (And it works very well, thanks!!)

As Eclipselink multitenant documentation says:

--
Entity Manager Factory
At this level, users will be required to provide a unique session name through the "eclipselink.session-name" property to ensure a unique server session (and cache) is provided for each tenant. This allows for user defined properties (without any prefixing)
(http://wiki.eclipse.org/EclipseLink/Development/Indigo/Multi-Tenancy)
--

So, it will be great if Gemini JPA includes this change in the default destribuition, as it is designed to work with Eclipselink.

Thanks by all effort!

Att,
Eduardo Frazão
Comment 15 Michael Keith CLA 2012-03-27 11:22:21 EDT
Okay, by popular demand I will add/enable this feature now and document it accordingly. Thanks, everyone, for your input.
Comment 16 Eduardo Frazão CLA 2012-03-27 13:55:41 EDT
I want to thank you for the effort and agility.
Comment 17 Michael Keith CLA 2012-03-29 11:20:16 EDT
Pushed to repo.
Comment 18 Michael Keith CLA 2012-03-29 12:06:09 EDT
*** Bug 366622 has been marked as a duplicate of this bug. ***