English 中文(简体)
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.

最佳回答

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.





相关问题
query must begin with SELECT or FROM: delete [delete

I am using JPA in addition to spring(3.0.0.M4). While deleting multiple records using query.executeUpdate() i am getting the following exception. org.springframework.web.util.NestedServletException: ...

Last update timestamp with JPA

I m playing around a bit with JPA(Eclipselink to be specific). The below entity have a timestamp that s supposed to reflect whenever that entity was last updated. What are the strategies for making ...

@IdClass with non primative @Id

I m trying to add a composite primary key to a class and having a bit of trouble. Here are the classes. class User { private long id; ... } class Token { private User user; private ...

Database not dropped in between unit test

Hello good people i came accross a weird behaviour in my test.I m using JPA hibernate annotation with spring. let say i have an Class MyObject and it s property email is marqued @Column(name="EMAIL", ...

Toplink trying to persist null object

I have an object "Instance" with another object "Course" inside. When trying to persist a new Instance object, I get the following error if Course is null: java.lang.IllegalStateException: During ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签