Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to multiple databases using JPA?

Tags:

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

like image 990
kgrad Avatar asked Feb 22 '10 14:02

kgrad


People also ask

How do I use two databases in spring boot JPA?

Multiple Databases in Spring Boot The interesting part is annotating the data source bean creation method with @ConfigurationProperties. We just need to specify the corresponding config prefix. Inside this method, we're using a DataSourceBuilder, and Spring Boot will automatically take care of the rest.

Is it possible to connect to multiple databases in java?

Connection mysqlCon = DriverManager. getConnection(mysqlUrl, "root", "password"); To connect to multiple databases in a single JDBC program you need to connect to the two (or more) databases simultaneously using the above steps.

Can we configure multiple database in spring boot?

Overview. The typical scenario for a Spring Boot application is to store data in a single relational database. But we sometimes need to access multiple databases. In this tutorial, we'll learn how to configure and use multiple data sources with Spring Boot.


1 Answers

1) Create several persistent units in your persistence.xml with different names.

2) Create necessary number of EntityManagerFactorys (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> 
like image 108
Roman Avatar answered Oct 24 '22 22:10

Roman