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.
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
Related
I have recently moved to Java EE (Wildfly) and I'd like to lookup an EntityManager from JNDI. At present I am defining a datasource in my standalone.xml and successfully retrieving this via JNDI but this provides me with only the Datasource and not an Entity Manager.
I am aware that I can create a persistence.xml and use #PersistenceContext but I am really looking at a way to avoid compile time knowledge of the JNDI name, so instead want to perform a lookup based on runtime information to retrieve the appropriate Entity Manager.
Unfortunately a persistence unit, from which an entity manager is derived can not be defined in a portable way without using a persistence.xml file.
If this is important for you please consider voting for JPA_SPEC-114 and additionally providing a comment there.
You can, more or less, make the persistence unit independent of the final JNDI name by using a resource-ref. A resource-ref does causes your code to become dependent on a container specific mechanism to switch what the resource-ref is pointing to.
An alternative, with its own cons unfortunately, is using a switchable data source approach. You can then define a data source using a fixed JNDI name and reference that from a persistence.xml file, and then use whatever method your switchable data source uses internally to go to the actual data source. This can then be either directly a data source implementation (such as shown in the link) or perhaps fetching another data source from JNDI (which effectively does what resource-ref does, but then using your own mechanism to switch)
How (or can) I specify programmatically which validation group OpenJPA should validate against during a persist or merge operation? Is this option only available via persistence.xml?
I'm drawing a blank.
Thanks.
The groups are configured per entity manager factory. If you obtain your entity manager factory programmatically via Persistence#createEntityManagerFactory() you can pass the groups to be validated during lifecycle validation using the properties javax.persistence.validation.group.{pre-persist|pre-update|pre-remove} but there is no (standardized) way for specifying the groups on a per-operation basis.
Yes, configuration is only via persistence.xml. I guess it would be open for JPA implementations to provide implementation specific ways, but that's not standardized. I am not sure whether OpenJPa offers such a provider specific option, but I don't think so.
I want to define a jndi object as simple string for each of my servers with in a server group;
I can define a jndi object as a simple string for a profile, and a profile can be attainable to a server-group, so when I define it in this way, all of my servers in this server group would see the same value, however I want to make the value to be different for each of my servers.
I am using a POJO (Non-EJB) class inside my JBoss server and creating multiple instances of it. Will JBoss create an object pool and manage this resource or will it simple create as many objects as I instantiate ?
Plain Old Java Objects are "unmanaged" the container has no way to know that when you say "new" you don't really mean "new".
EJBs have managed life cycles, are trivial to implement. If you want pooling why not use one?
I have an entity bean that will represent an expected result over multiple databases/datasources and can also be different queries executed, but same result always comming back. So the bean is re-used over different datasources that should be able to be dynamicly selected.
Is it possible with JPA to select during runtime the data source to be used to execute a query, and return the same type of entity bean?
Also, does my ejb/application need to define the datasources that will be used? Or can I always specify via jndi what datasource to use? Modifying the descriptor's and re-deploying an application everytime a new datasource is created is not an option.
Sorry if the question does not make 100% sense, rather difficult to get the idea through.
Is it possible with JPA to select during runtime the data source to be used to execute a query, and return the same type of entity bean?
You can't change the datasource of a persistence unit at runtime. However, you can configure several persistence unit and use one or another EntityManagerFactory. Maybe JPA is not the right tool for your use case.
Modifying the descriptor's and re-deploying an application everytime a new datasource is created is not an option.
And how will the application be aware of the "available datasources"?
You can change the JPA datasource at runtime, but the approach is tricky (introspection, JPA implementation specific, ...).
I've implemented my own implementation of javax.persistence.spi.PersistenceProviderwhich override the org.hibernate.ejb.HibernatePersistence and sets the datasource in both the Map and PersistenceUnitInfo of the PersistenceProvider just before creating the EntityManagerFactory. This way, my EntityManagerFactory has a datasource which has been configured at runtime. I keep my EntityManagerFactory until the application is undeployed.
You could use the same be approach and create N different EntityManagerFactory, each with its specific datasource. However keep in mind that each ÈntityManagerFactory uses a lot of memory.