how to setup spring data jpa with multiple datasources - spring

I am using Spring Data Jpa version 1.0.0.M2 here is the url:
http://static.springsource.org/spring-data/data-jpa/docs/1.0.0.M2/reference/pdf/spring-data-jpa-reference.pdf
All is promised to be very simple and nice, but when it comes to two datasources it breaks down. The question is how to setup with two data sources? The JpaRepository automatically searches for EntityManager, when it finds more than two it throws exceptions.
If you have any idea with EntityManager and how to setup the spring data jpa, please post a reply. Your help is truly appreciated!!!

<jpa:repositories base-package="org.springframework.data.jpa.repository.sample"
entity-manager-factory-ref="secondEntityManagerFactory" />

You can use a dynamic datasource that wrap your two datasources, as explained here:
http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/

Are you looking to use 'EntityManager-A' with Spring Data JPA and 'EntityManager-B' for another data access layer?
Mark

Related

Spring Boot - Replicating the EntityManager created by Spring-boot

I've been working on a Spring Boot project and i did all my database operations with the help of Spring Data JPA. So all i had was some entities and some repositories and all the CRUD operations worked fine.
But now, the project needed to talk to 2 databases. So what i did was i created all the new entities and repositories in a separate package. I noticed that now i need to have 2 EntityManagers, so i created 2 config files for both of the entity managers, configuring a datasource, an entityManager and a transactionManager. So now, instead of having 1 entityManager that spring boot gave me, i had 2 entityManagers that i created myself. And they worked fine, they both talked to the database they were supposed to talk to.
My problem is that i seem to have lost a lot of feature in my entityManager that spring-boot previously gave me. For example, my entity manager no longer created the schema based on its entities in my tests, which it previously did. Along with this, i am sure that i will run into other situations in which my entityManager will not be as properly configured as the one spring-boot would have given me.
So my idea was to only declare the second entity manager, and leave the config for the first one empty, however this did not work, and looking into JpaBaseConfiguration i noticed that spring will only create an entityManager if you do not already have one.
#ConditionalOnMissingBean({ LocalContainerEntityManagerFactoryBean.class,
EntityManagerFactory.class })
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
//some code that will give you an entity manager
);
And seeing as i MUST configure a second entityManager for the second database, it looks like im forced to create the first one as well, since spring-boot will skip creating one.
Which leads me to my question:
Is there anyway that i can configure my first entity manager the same way that spring-boot would have done it for me? Or perhaps can i force spring to give me that entityManager even though i have another one declared for the second database? Is there any other way of solving my situation (i am thinking about just using a JDBCTemplate for the second database, however this is kinda of admiting defeat :'( )?
Sorry for the long question. Any answer is appreciated! Thank you!

With Spring Data JPA, under what circumstances should I use EntityManager directly?

Spring Boot Data JPA does an amazing job of generating repositories and abstracting away the managemet of Datasources and EntityManager etc. But sometimes I see code where the EntityManager is included as a field and accessed directly, like in this Dzone example
I don't really understand from that example, when should I include the EntityManager in my class and interact with it directly and when can I just rely on the repository Spring Data JPA autogenerates? Can someone explain?
One good use of EntityManager that I can think of is when you want to get results from a database using complex queries such as join. You can directly query using EntityManager and use ResultTransformer to get into your custom model.

How to set multiple data sources in Spring Data R2DBC

I am using spring data r2dbc in my new project and need to connect multiple data sources like A data source and B data source.
Is there a way to connect multiple data sources using r2dbc?
Could I get an example or a document if there is a way?
My stacks are below:
Spring Boot 2.3.0.M4
Spring WebFlux
Spring Data R2DBC
If you want to use multiple datasources in a single application, check my multi r2dbc connection factories example.
If you need a multi-tenant like feature check this multi-tenancy-r2dbc example.
I solved this issue using AbstractRoutingConnectionFactory you can check Add support for AbstractRoutingConnectionFactory

Spring Boot - Let Hibernate initialize all datasources of AbstractRoutingDataSource

I've implemented the AbstractRoutingDataSource in order to horizontally partition my data [1]. I rely on Hibernates functionality to create and update the database schema. This works fine for the first datasource, which is resolved by the determineCurrentLookupKey() declared in AbstractRoutingDataSource.
Is there a way to apply the schema generation/update to the other datasources as well?
I've found the classes SchemaExport and SchemaUpdate respectively, that are capable of the required functionality. However, both require an instance of org.hibernate.boot.MetaData, which I don't how to obtain.
(I'm using Spring Boot in version 1.4.2.RELEASE.)
[1] https://spring.io/blog/2007/01/23/dynamic-datasource-routing/
Thanks!
I got the same issue and found a solution using the SchemaExport class of hibernate.
For each DataSourceEnum you can manually initialize the datasource.
here is my detailed answer to my own issue discription

Spring boot's XADataSourceAutoConfiguration vs AtomikosJtaConfiguration

I am trying to understand how the XADataSourceAutoConfiguration relates to the AtomikosJtaConfiguration and more generally the **JtaConfiguration.
More specifically how the two classes below relate to each other:
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration
org.springframework.boot.autoconfigure.transaction.jta.AtomikosJtaConfiguration
I need a spring boot spring batch application to participate in transactions involving two databases.
Can someone please tell me how the two classes relate to each other?
I was not able to find much documentation in the javadocs API or in the reference documentation about XADataSourceAutoConfiguration.
XADataSourceAutoConfiguration is responsible for taking an XADataSource and applying a transaction manager-specific wrapper. That wrapper is how the data source is enlisted in any XA transactions.
AtomikosJtaConfiguration is responsible for configuring Atomikos, including providing the Atomikos-specific XADataSource wrapper that will ensure that Atomikos knows about the XADataSource and enlists it in any XA transactions.

Resources