Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TransactionManager for multiple databases

I'm working on a project which has several (already existing) different databases and one new database (my "local" db) that will be used only from this new application.

From this application i will be only reading all the other databases, not performing any changes. But for the local db i will be performing saving/updating/deleting operations in it.

In this scenario, i have some services with operations that for example persist some entities in my local database, and at the same time will be querying the external "read-only" databases. So, for this kind of services, can i use only a simple hibernate transaction manager bound to my local database? Or should i need some jta transaction for all the participants even that i will be only performing reads in the rest of the dbs?

As a side question: supposing that i need transaction between all the resources, and if all the databases lived in the same server, would i still need a jta transaction?

Thanks and excuse my poor english!

like image 267
Lucia Avatar asked Nov 28 '25 09:11

Lucia


1 Answers

RESOURCE_LOCAL

If you only plan to read the other databases, you are fine with local transactions.

In this case, You need to have all DataSource configured, and the Hibernate or JPA transaction manager must only take your local DB DataSource as a configuration parameter.

If you want to use Hibernate for the other data sources, you need to configure a SessionFactory for each DataSource you plan on querying.

The Hibernate/JPA transaction manager will open, commit and rollback transactions using the underlying SessionFactory DataSource.

JTA

If you wanted to a global transaction to update multiple data sources, you'd then have to supply XA data sources, use an XA transaction manager and the Spring JTA transaction manager facade. But the databases don't need to be collocated to a single server. JTA works even if the databases are on different machines.

like image 168
Vlad Mihalcea Avatar answered Nov 29 '25 22:11

Vlad Mihalcea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!