Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transaction management for multiple database Using Spring & Hibernate

Am coming up with a Spring & Hibernate application which has Inserts/Update to its own system database and it even has to Insert/update other systems with different database types.

How can i achieve the transaction management with Hibernate while performing with different databases.

-Bharat

like image 855
Bharat Avatar asked Apr 22 '16 20:04

Bharat


2 Answers

Assuming you are looking for transactions involving multiple datasources, I highly recommend you give this series of posts by David Syer (spring lead) on your options:

http://www.javaworld.com/article/2077963/open-source-tools/distributed-transactions-in-spring--with-and-without-xa.html

For example, some databases even support transactions across databases schemas under certain circumstances without distributed transactions (see here).

There are other options other than XA 2-phase commit transaction managers (as they introduce significant overhead and complexity) but they involve different trade-offs.

like image 96
leeor Avatar answered Oct 22 '22 21:10

leeor


You can specify two separate transaction managers.

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

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

Once your set up is done you can use the qualifier to specify the transaction manager:

    public class TransactionalService {

@Transactional("account")
public void setSomethingInAccount() { ... }

@Transactional("businessData")
public void doSomethingInBusinessData() { ... }
}
like image 43
Dinesh Arora Avatar answered Oct 22 '22 19:10

Dinesh Arora