Geert Schuring
Geert Schuring

Reputation: 2163

How to override JPA datasource JNDI name during deployment on glassfish 3?

I am trying to deploy my JPA application on 2 separate instances within the same glassfish 3 domain. Both instances will be looking up a datasource using the same JNDI name, but I want them to find different datasources. I tried to define 2 datasources and bind them to different targets, but the DAS does not allow 2 datasources using the same JNDI name even though they are bound to different targets.

I tried to use property substitution but that didn't work. Does anyone know how to solve this? It seems unlikely that there's no way to deploy an application twice in the same domain.

Upvotes: 2

Views: 487

Answers (1)

Alex Vaz
Alex Vaz

Reputation: 516

A JNDI name is an address for a specific object, and they must be unique. Having two JNDI names is like when you have 2 numbers in your cellphone for "alex". There is no way to know which are you dialing.

What I would do, which should work for any JPA implementation, is to have two PUs on your persistent.xml, one with a JNDI datasource and another for the other JNDI datasource. This also makes sense because you might not have the same business objects on both datasources.

Then, when you get you EntityManager, specify explicitly which PU you want. You might set this in a configuration file or decide it dynamically some other way.

entfactory = OpenJPAPersistence.createEntityManagerFactory( *persistentUnitName*, (String) null );

Hope this helps --

-Alex

Upvotes: 1

Related Questions