| JPA + EclipseLink in OSGi environment [message #759980] |
Wed, 30 November 2011 14:13  |
Eclipse User |
|
|
|
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 #761420 is a reply to message #759980] |
Tue, 06 December 2011 08:07   |
Eclipse User |
|
|
|
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.
|
|
|
|
Powered by
FUDForum. Page generated in 0.04165 seconds