Search code examples
jpaejb-3.0weblogicjdeveloper

Dynamicly select datasource for entities runtime


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.


Solution

  • 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"?