Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Gemini » JPA + EclipseLink in OSGi environment
JPA + EclipseLink in OSGi environment [message #759980] Wed, 30 November 2011 19:13 Go to next message
Eugen Reiswich is currently offline Eugen ReiswichFriend
Messages: 35
Registered: July 2009
Member
Hi folks,

I've been recommended to post my problem in this forum, I hope this is the right place. I try to run JPA + EclipseLink in an OSGi environment with a specific architecture:

*.domain.customer // business objects
*.domain.account

*.service.customer
*.service.account

*.service.customer.dao
- orm.xml
- persistence.xml

*.service.account.dao
- orm.xml
- persistence.xml

*.service.dao_jpa // OSGi service providing the DB credentials in a single place
- IDatabaseCredentialsService

I try to avoid redundant database credentials within the two persistence.xml files using the EntityManagerFactoryBuilder which retrieves db-credentials from my own IDatabaseCredentialsService. But this solution seems to produce a lot of code and one has to be aware of race conditions.

private void createEntityManagerFactory() {
 assert emfBuilder != null : "emfBuilder != null";
 assert dbCredentials != null : "dbCredentials != null";

 Map<String, String> dbCredentioals = dbCredentials.getDBCredentioals();
 entityManagerFactory = emfBuilder
 .createEntityManagerFactory(dbCredentioals);

 createCustomer();
 }

 public void bindDBConnectionService(IDatabaseCredentialsService dbService) {
 dbCredentials = dbService;
 if (emfBuilder != null) {
 createEntityManagerFactory();
 }
 }

 public void bindEMFBuilder(EntityManagerFactoryBuilder builder) {
 emfBuilder = builder;
 if (dbCredentials != null) {
 createEntityManagerFactory();
 }
 }


I hope you can help me and point me to a better and simpler solution.

Eugen
Re: JPA + EclipseLink in OSGi environment [message #760315 is a reply to message #759980] Thu, 01 December 2011 20:31 Go to previous messageGo to next message
Michael Keith is currently offline Michael KeithFriend
Messages: 243
Registered: July 2009
Senior Member
Hi Eugen,

Your solution doesn't seem like too much code to me (although your additional IDatabaseCredentialsService impl wasn't included), but an alternative might be to use the Config Admin service as a means of setting and getting the config. Not sure how much that would save you, but it is at least an established service that already exists.

We have a feature request already open to integrate with the config admin service directly and once that is done it might be easier for you.

-Mike
Re: JPA + EclipseLink in OSGi environment [message #760779 is a reply to message #760315] Mon, 05 December 2011 09:14 Go to previous messageGo to next message
Eugen Reiswich is currently offline Eugen ReiswichFriend
Messages: 35
Registered: July 2009
Member
Hi Mike,

well one can surely argue whether this is a lot of glue code or not. I would prefer to provide a hook when the EntityManagers are registered within the OSGi-service registry and inject the DB-credentials in one single place rather than writing glue code in all my DAO-bundles. OSGi already provides hook possibilities during service registration but I haven't found a way how to update an EntityManager once it is initialized.

Eugen
Re: JPA + EclipseLink in OSGi environment [message #760923 is a reply to message #760779] Mon, 05 December 2011 15:49 Go to previous messageGo to next message
Michael Keith is currently offline Michael KeithFriend
Messages: 243
Registered: July 2009
Senior Member
Hi Eugen,

A few comments:

1) If you want less code the first thing I would suggest is to get rid of the outdated DAO pattern. Smile With standardized JPA entities DAOs should be a thing of the past.
2) If you are only talking about duplicating 4 JDBC properties in 2 XML files it is probably much more efficient to duplicate them than it is to write a bunch of Java code to do the dynamic hooking up. Of course, if these two are just the beginning and you plan on having many more persistence units that all access the same database then you may in fact want to do something that decouples them.
3) As I mentioned, there has been a feature request for this in the past and I entered a bug to track this. I hope to put this in the next release.

-Mike
Re: JPA + EclipseLink in OSGi environment [message #761382 is a reply to message #760923] Tue, 06 December 2011 11:44 Go to previous messageGo to next message
Eugen Reiswich is currently offline Eugen ReiswichFriend
Messages: 35
Registered: July 2009
Member
Hi Mike,

Quote:

1) If you want less code the first thing I would suggest is to get rid of the outdated DAO pattern. With standardized JPA entities DAOs should be a thing of the past.

I disagree. We believe that technology changes very often and business is far more stable. Thus we try to keep our business objects free of any technology. Hence we don't use JPA annotations but rather use mapping files in DAO bundles.

Quote:

2) ...Of course, if these two are just the beginning and you plan on having many more persistence units that all access the same database then you may in fact want to do something that decouples them.

That's what we plan to do.

Quote:

3) As I mentioned, there has been a feature request for this in the past and I entered a bug to track this. I hope to put this in the next release.

As I understand the ConfigAdminService won't help us in this situation as there is no point in time to decide/to know whether specific configurations are provided by the service or not. Due to the random start procedure for OSGi bundles the ConfigAdminService can be already available but without the needed configuration. So I often find myself in situations where I get an ConfigAdminService reference but it does not contain the needed configuration information. They might be set later in time.

Eugen
Re: JPA + EclipseLink in OSGi environment [message #761420 is a reply to message #759980] Tue, 06 December 2011 13:07 Go to previous messageGo to next message
Juergen Kissner is currently offline Juergen KissnerFriend
Messages: 25
Registered: March 2011
Junior Member
Hi Eugen,

Have you tried the following?
Remove the method call createEntityManagerFactory() from your bind methods.
In your component, you specify both references as mandatory: the reference to DatabaseCredentialsService as 1..1, to EntityManagerFactoryBuilderService as 1..1 or 1..n depending on your needs. After these references are satisfied, first the bind methods are called. After that the activate method of the component is called, see 112.5.8 of the OSGi Enterprise Specification Version 4.2. In that activate method, you know that both services are available, so can call createEntityManagerFactory()without without the danger of race conditions.

Does that help?

Best Regards,
Juergen

Ps.: By the way, the specification says: When binding services, the references are processed in the order in which they are specified in the component description. But doing the initialization in the activator might still be the better solution.
Re: JPA + EclipseLink in OSGi environment [message #761431 is a reply to message #761420] Tue, 06 December 2011 13:23 Go to previous message
Eugen Reiswich is currently offline Eugen ReiswichFriend
Messages: 35
Registered: July 2009
Member
Hi Juergen,

well this will only reduce the if statements but not the bind methods nor the effort within the component.xml.

Eugen
Previous Topic:Gemini JPA Package Refresh
Next Topic:Gemini management first milestone released
Goto Forum:
  


Current Time: Tue Apr 16 11:46:57 GMT 2024

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

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

Back to the top