Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring multiple @Transactional datasources

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="data.emf" />
</bean>

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


<bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="data.emf" />
</bean>

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


In my service layer, can I use @Transactional(name="transactionManager2"); to identify which transaction manager I use if I have multiple transaction managers?

like image 551
cometta Avatar asked Dec 25 '09 15:12

cometta


People also ask

How do I add multiple datasources in Spring?

So, to use multiple data sources, we need to declare multiple beans with different mappings within Spring's application context. The configuration for the data sources must look like this: spring: datasource: todos: url: ... username: ...

Is it possible to define multiple transaction manager in Spring?

You just need to create two transaction managers and inject them with the appropriate connection. But I must ask the question: why do you think you need two transaction managers? You can have more than one database connection.

How does Spring boot handle two datasources?

Multiple Databases in Spring Boot The interesting part is annotating the data source bean creation method with @ConfigurationProperties. We just need to specify the corresponding config prefix. Inside this method, we're using a DataSourceBuilder, and Spring Boot will automatically take care of the rest.

Can we connect to multiple databases in Spring boot?

Spring boot allows you to connect to multiple databases by configuring multiple data sources in a single spring boot application using hibernate and JPA. Spring boot enables repositories to connect to multiple databases using JPA from a single application.


2 Answers

You can specify which tx manager to use with @Transactional using the value attribute:

A qualifier value for the specified transaction.

May be used to determine the target transaction manager, matching the qualifier value (or the bean name) of a specific PlatformTransactionManager bean definition.

For example:

@Transactional("txManager1");

Alternatively, you can use the more explicit TransactionProxyFactoryBean, which gives you finer-grained control over what objects gets proxied by what tx managers. This still uses the annotations, but it doesn't auto-detect beans, it's configured explicitly on a bean-by-bean basis.

This normally isn't an issue, but it's not wise to have multiple transaction managers unless you have a very good reason to do so. If you find yourself needing two tx managers, it's usually better to see if you can make do with one. For example, if you have two data sources configured in your app server, you can incorporate both in a single JtaTransactionManager, rather than two seperate JpaTransactionManager or DataSourceTransactionmanagers.

like image 153
skaffman Avatar answered Oct 03 '22 15:10

skaffman


More on the need for more than one transaction manager. You might be trying to do nested or separate transactions in sequence -- then you can use different propagation settings. You can achieve that with configuration using single transaction manager see Transaction propagation.

like image 30
Zoran Regvart Avatar answered Oct 03 '22 13:10

Zoran Regvart