java - How to delegate Spring services to a different database? -


I have different classes, which are in the extension of the crad registrar; spring data of JPA framework , This uses the underlying global EntityManager bean.

Now I want to create a service that should be connected to a different database. This means that I have to inject a different M / Datasource. But how can I force a database for spring beans / services?

There are several ways to do this. What we have done in one of our projects is that it defines two different packages for different data sources and define the beans separately for them:

   & Lt; Jpa: repository base-package = "com.def.repository" transaction-manager-riff = "defTransactionManager" unit-manager-factory -fraf = "defEntityManagerFactory" /> & Lt; Bean id = "abcTransactionManager" class = "org.springframework.orm.jpa.JpaTransactionManager" & gt; & Lt; Property Name = "entityManagerFactory" ref = "abcEntityManagerFactory" /> & Lt; Qualifier value = "abcTransactionManager" & gt; & Lt; / Qualifier & gt; & Lt; / Bean & gt; & Lt; Bean id = "defTransactionManager" class = "org.springframework.orm.jpa.JpaTransactionManager" & gt; & Lt; Property Name = "entityManagerFactory" reference = "defEntityManagerFactory" /> & Lt; Qualifier value = "defTransactionManager" /> & Lt; / Bean & gt; & Lt; / Bean & gt;   

Similarly, defining the entity manager factory. In this manner, whatever repositories are defined in package ABC will use abcEntityManager and similar to DF.

The way I worked on it is in other ways implementing the transaction with the appropriate unit manager:

  @ Transaction ("abcTransactionManager")    

Comments

Popular posts from this blog

c# - passing input text from view to contoller with FacebookContext using Facebook app -

ios - Does Core Data autoupdate a many to many relationship on saving -

Calling a C++ function from C# by passing a string with variable size to it -