问题描述
我有一个使用Java servlets / JSP的应用程序。有多个客户端使用我的应用程序,但每个客户端都有一个单独的数据库。所有的数据库都有相同的模式。我想确定在用户登录系统时使用哪个数据库连接。
例如,客户端A登录后,我确定客户端A属于数据库C,为数据库C获取连接并继续以我的快乐方式。 b
$ b
我使用JPA和Hibernate作为我的JPA提供程序。是否有可能使用多个持久性单元执行此操作并确定在登录时使用哪个单元?有没有更好的/首选的方式来做到这一点?
编辑添加:
我使用注释和EJB,因此Persistence Context正在EJB中设置与@PersistenceContext(unitName =blahblah),这可以在登录时确定?我可以在运行时更改unitName吗?
谢谢
)在你的 persistence.xml
中用不同的名称创建几个持久化单元。
$ b 2)创建必要数量的
EntityManagerFactory
s(1个持久性单元)并指定哪个持久性单元应该用于具体工厂: < bean id =authEntityManagerFactoryclass =org.springframework.orm.jpa.LocalEntityManagerFactoryBean>
< property name =persistenceUnitNamevalue =SpringSecurityManager/>
< / bean>
3)创建必要数量的 TransactionManager
s :
< bean id =authTransactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =authEntityManagerFactory/>
< / bean>
4)在您的DAO的类中,指定您想要哪个持久性单元(以及哪个EntityManagerFactory)工作:
public class AbstractAuthDao< T> {
@PersistenceContext(unitName =SpringSecurityManager)
受保护的EntityManager em;
$ b $ ...
}
5) -objects指定应使用哪个TransactionManager(此功能仅在Spring 3.0中受支持):
@Transactional(value =authTransactionManager ,readOnly = true)
public class UserServiceImpl implements UserService {
...
}
$ b $ 6)如果你的web.xml中有 OpenEntityManagerInViewFilter
,那么在它的init-param名称中指定必要的EntityManagerFactory(或者create几个带有相应init-blocks的过滤器):
< init-param>
< param-name> entityManagerFactoryBeanName< / param-name>
< param-value> authEntityManagerFactory< /参数值>
< / init-param>
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.
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.
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?
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?
Thanks
1) Create several persistent units in your persistence.xml
with different names.
2) Create necessary number of EntityManagerFactory
s (1 per persistence-unit) and specify which persistence-unit should be used for concrete factory:
<bean id="authEntityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SpringSecurityManager"/>
</bean>
3) Create necessary number of 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:
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):
@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):
<init-param>
<param-name>entityManagerFactoryBeanName</param-name>
<param-value>authEntityManagerFactory</param-value>
</init-param>
这篇关于如何使用JPA连接到多个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!