Java 如何使用 JPA 连接到多个数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2311614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I connect to multiple databases using JPA?
提问by kgrad
I have an application using Java servlets/JSP's. There are multiple clients using my app, however each client has a separate database. All the databases have the same schema. I would like to determine which database connection to use at the time when a user logs into the system.
我有一个使用 Java servlet/JSP 的应用程序。有多个客户端使用我的应用程序,但是每个客户端都有一个单独的数据库。所有数据库都具有相同的架构。我想确定在用户登录系统时使用哪个数据库连接。
For example client A logs in, I determine that client A belongs to database C, grab the connection for database C and continue on my merry way.
例如客户端A登录,我确定客户端A属于数据库C,抓取数据库C的连接并继续我的快乐之路。
I am using JPA with Hibernate as my JPA provider. Is it possible to do this using multiple persistence units and determining which unit to use at login time? Is there a better/preferred way to do this?
我使用 JPA 和 Hibernate 作为我的 JPA 提供程序。是否可以使用多个持久性单元并确定在登录时使用哪个单元来做到这一点?有没有更好/更喜欢的方法来做到这一点?
Edited to add: I am using annotations and EJB's so the Persistence Context is being set in the EJB with @PersistenceContext(unitName = "blahblah"), can this be determined at login time? Can I change the unitName at runtime?
编辑添加:我正在使用注释和 EJB,所以持久性上下文是在 EJB 中使用 @PersistenceContext(unitName = "blahblah") 设置的,这可以在登录时确定吗?我可以在运行时更改 unitName 吗?
Thanks
谢谢
采纳答案by Roman
1) Create several persistent units in your persistence.xml
with different names.
1)在您的persistence.xml
名称中创建多个持久单元。
2) Create necessary number of EntityManagerFactory
s (1 per persistence-unit) and specify which persistence-unit should be used for concrete factory:
2) 创建必要数量的EntityManagerFactory
s(每个持久单元 1 个)并指定具体工厂应使用哪个持久单元:
<bean id="authEntityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SpringSecurityManager"/>
</bean>
3) Create necessary number of TransactionManager
s:
3) 创建必要数量的TransactionManager
s:
<bean id="authTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="authEntityManagerFactory" />
</bean>
4) In your DAO's classes specify with which persistence-unit (and so with which EntityManagerFactory) you want to work:
4) 在你的 DAO 的类中指定你想要使用哪个持久性单元(以及哪个 EntityManagerFactory):
public class AbstractAuthDao<T> {
@PersistenceContext (unitName = "SpringSecurityManager")
protected EntityManager em;
...
}
5) In your service-objects specify which TransactionManager should be used (this feature is supported only in Spring 3.0):
5)在您的服务对象中指定应该使用哪个 TransactionManager (此功能仅在 Spring 3.0 中受支持):
@Transactional (value = "authTransactionManager", readOnly = true)
public class UserServiceImpl implements UserService {
...
}
6) If you have OpenEntityManagerInViewFilter
in your web.xml, then specify in its init-param name of necessary EntityManagerFactory (or create several filters with correspondent init-blocks):
6)如果你OpenEntityManagerInViewFilter
在你的 web.xml 中有,那么在它的 init-param 名称中指定必要的 EntityManagerFactory(或者用相应的 init-blocks 创建几个过滤器):
<init-param>
<param-name>entityManagerFactoryBeanName</param-name>
<param-value>authEntityManagerFactory</param-value>
</init-param>