Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Gemini » REALLY dynamic database connection settings
REALLY dynamic database connection settings [message #698420] Tue, 19 July 2011 12:56 Go to next message
ephikles is currently offline ephiklesFriend
Messages: 3
Registered: July 2011
Junior Member
Hi,

I'm trying to figure out how to set my database properties at runtime. What makes my case special is that I don't want to set them only once, but over and over again.


So I tried the following:
1. take whatever EntityManagerFactoryBuilder-Service (EMFB) is registered for my "osgi.unit.name" by gemini jpa
2. define my own "DynamicEntityManagerFactory" (implements EntityManagerFactory) and inject the EMFB
3. in the overridden "EntityManagerFactory"'s methods: get a new EntityManagerFactory by calling the EMFB's "createEntityManagerFactory", e.g.:
@Override
public EntityManager createEntityManager() {

	//..somehow change the properties..

	return emfb.createEntityManagerFactory(properties).createEntityManager();
}


In my blueprint-xml file i have
<reference id="emfb" interface="org.osgi.service.jpa.EntityManagerFactoryBuilder" filter="(osgi.unit.name=myPUnit)" />

<bean id="dynemf" class="package.of.my.DynamicEntityManagerFactory">
	<property name="emfb" ref="emfb" />
</bean>

<bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
	<property name="entityManagerFactory" ref="dynemf" />
</bean>


So whenever the transactionmanager needs an entitymanager, my DynamicEntityManagerFactory somehow changes the properties and tries to get a new EntityManagerFactory from its EMFB.

Unfortunately that doesn't work, instead throwing an IllegalArgumentException. Checking the sourcecode I stumbled over a method called "verifyJDBCProperties" in "EMFBuilderServiceProxyHandler" causing the exception.

For reasons I don't understand, subsequent calls to "createEntityManagerFactory" must have the same driver and the same connection-url..!?

Can someone tell my the reason for that restriction? Or a way to circumvent it? Or some other solution for what I'm trying to do?
Re: REALLY dynamic database connection settings [message #698613 is a reply to message #698420] Tue, 19 July 2011 20:02 Go to previous messageGo to next message
Michael Keith is currently offline Michael KeithFriend
Messages: 243
Registered: July 2009
Senior Member
A persistence unit is meant to be a static configuration for persisting a set of entities. The provider needs to know the necessary bits in order to configure an EMF to connect to a datasource, create mapping descriptors, weave the entities, and so forth. This has always been an assumption of JPA.

It turns out that since the provider does not actually need to do this until the EMF is created then some of the properties, such as the connection info, can be passed in at EMF creation time instead of being defined in the persistence.xml file. However, this does not change the fact that a single named p-unit is bound to a specific datasource. If it were able to be associated with multiple data sources then how would you know which datasource you were getting when you referenced a named p-unit? An EMFB is just a way to dynamically specify the connection info at runtime without actually putting it in the persistence.xml file. It is still associated with a single p-unit.

It sounds like you are actually wanting to create a new p-unit on each call (complete with new p-unit name, etc), and that is a separate feature, one that is close to existing but does not currently exist. Please enter a bug and perhaps I can work with the EclipseLink team to see if it could be supported. There would be caveats, though, such as the fact that the service would no longer be tracked and follow the lifecycle of a particular datasource since there may be multiple data sources associated with a given EMFBuilder, etc.

-Mike
Re: REALLY dynamic database connection settings [message #699026 is a reply to message #698613] Wed, 20 July 2011 16:59 Go to previous messageGo to next message
ephikles is currently offline ephiklesFriend
Messages: 3
Registered: July 2011
Junior Member
Hi Mike,

thanks alot for your quick answer!!

Well, I now know you're right. I just read part of the JPA-specs: 5.3 Obtaining an Entity Manager Factory. According to the spec, Quote:
More than one entity manager factory instance may be available simultaneously in the JVM.
, but also that Quote:
There is only one entity manager factory per persistence unit


So if I want more than one EntityManagerFactory in order to access a different datasource, I also need a different punit for the EMF.

That means I either have to go the hard way and find a way to create my own EMFs, which seems to be more than painful, or I have to come begging to you. Wink

Gemini JPA, as it is, creates an EntityManagerFactoryBuilder. Sadly it's bound to the first EntityManagerFactory that is created by it. It would be nice if I could use the EMFB to create, as mentioned above, "more than one entity manager factory instance [to] be available simultaneously in the JVM". And I accept the fact that these EMFs must be for different persistence units; where, in my case, "different" would be different in name and datasource-binding.
I'm well aware that my idea is a break in concept for your EMFServiceProxyHandler which is bound to a punit and an EMF, at least as soon as the latter is created.
But I'm convinced that for someone who is as greatly blessed with skill as you, it won't pose much of a problem to make the gemini jpa project this little bit more flexible and hence usable in a multi-datasource environment! Smile

Thank you for reading my gibberish! A bug report, if you still want one, will come tomorrow..

..here it is: Bug ID 352708 (i'm not allowed to post links yet Sad )

[Updated on: Thu, 21 July 2011 08:36]

Report message to a moderator

Re: REALLY dynamic database connection settings [message #699493 is a reply to message #699026] Thu, 21 July 2011 16:22 Go to previous messageGo to next message
Michael Keith is currently offline Michael KeithFriend
Messages: 243
Registered: July 2009
Senior Member
Florian,

In order for this to work there needs to be EclipseLink support to create a dynamic persistence unit without there being any persistence.xml entry of that name. I have entered a bug against EclipseLink for this feature. As soon as it gets added then I'm pretty sure I can provide support for it in Gemini JPA. Sound fair?

-Mike
Re: REALLY dynamic database connection settings [message #699706 is a reply to message #699493] Fri, 22 July 2011 07:02 Go to previous message
ephikles is currently offline ephiklesFriend
Messages: 3
Registered: July 2011
Junior Member
Hey, sounds fair to me! Hope it works out fine.. well, time will tell, I suppose.
Thank you!
Previous Topic:Gemini JPA EntityManagerFactoryBuilder
Next Topic:Gemini Web 2.0.0.RC1
Goto Forum:
  


Current Time: Mon Sep 23 15:12:31 GMT 2024

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

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

Back to the top