Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure transaction management for working with 2 different db in Spring?

I have 2 databases (MySql and HSQLDB). I configured 2 data sources and 2 EntityManagerFactory beans. I can also configure 2 correspondent JpaTransactionManager beans.

But I don't know how to specify which of them should be used to manage transactions for concrete service-class. I want to use @Transactional annotation for that purpose, but I actually can specify only one of txManagers:

<tx:annotation-driven transaction-manager="manager"/> 

What is the way out from this situation?

like image 673
Roman Avatar asked Dec 25 '09 17:12

Roman


People also ask

How do you achieve transactions when dealing with two different databases?

If your data is distributed across multiple databases, you may wish to update one database while reading from one or more other databases. This type of access can be performed within a single unit of work (transaction). This type of database access is called multisite update or two-phase commit.

Can we configure 2 DB in spring boot?

Multiple Databases in Spring BootSpring Boot can simplify the configuration above. Now we have defined the data source properties inside persistence-multiple-db-boot.

Is it possible to define multiple transaction manager in spring?

You can have more than one database connection.


2 Answers

Declare your <tx:annotation-driven> without transaction-manager attribute, declare qualifiers for transaction managers like this:

<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">     <property name="entityManagerFactory" ref="entityManagerFactory" />     <qualifier value="txManager1"/> </bean> 

Use this qualifier in @Transactional as a value to select one of transaction managers:

@Transactional("txManager1") 

or, with more properties:

@Transactional(value = "txManager1", readOnly = true)    
like image 89
axtavt Avatar answered Oct 08 '22 03:10

axtavt


The javadoc for JpaTransactionManager has some advice on this:

This transaction manager is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access. JTA (usually through JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction. Note that you need to configure your JPA provider accordingly in order to make it participate in JTA transactions.

In other words, if you find yourself with multiple entity managers, with corresponding tx managers, then you should consider using a single JtaTransactionManager instead. The entity managers should be able to participate in JTA transactions, and this will give you full transactionality across both entity managers, without hacving to worry about which entity manager you're in at any one time.

Of course, JtaTransactionManager does require a full JTA-supporting application server, rather than a vanilla servlet engine like Tomcat.

like image 44
skaffman Avatar answered Oct 08 '22 05:10

skaffman