Spring - Is it possible to use multiple transaction managers in the same application?

Brian DiCasa :

I'm new to Spring and I'm wondering if its possible to use numerous transaction managers in the same application?

I have two data access layers - one for both of the databases. I'm wondering, how do you go about using one transaction managers for one layer and different transaction manager for the other layer. I don't need to perform transactions across both databases - yet. But I do need perform transactions on each database individually. I've created an image to help outline my problem:

alt text

Here is my application context configuration:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:component-scan base-package="cheetah.repositories" />
    <tx:annotation-driven />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="accounts" />
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

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

</beans>

Here is an example that uses this configuration:

@Repository
public class JpaAccountRepository implements AccountRepository {

    @PersistenceContext(unitName = "cheetahAccounts")
    private EntityManager accountManager;

    @Override
    @Transactional
    public Account findById(long id) {

        Account account = accountManager.find(Account.class, id);
        return account;
    }
}

So for the account repository, I want to use an entity manager factory with the persistence unit set to accounts. However, with my BusinessData Repository, I want to use an entity manager factory with a different persistence unit. Since I can only define one transaction manager bean, how can I go about using different transaction managers for the different repositories?

Thanks for any help.

Chin Huang :

Where you use a @Transactional annotation, you can specify the transaction manager to use by adding an attribute set to a bean name or qualifier. For example, if your application context defines multiple transaction managers with qualifiers:

<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>

You can use the qualifier to specify the transaction manager to use:

public class TransactionalService {

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

    @Transactional("businessData")
    public void doSomethingInBusinessData() { ... }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Multiple DataSources with Multiple Transaction Managers in Spring

Multiple Transaction Managers in Spring Boot for different EntityManagers

JPA Multiple Transaction Managers

Multiple transaction managers NoUniqueBeanDefinitionException

MyBatis-Spring rollback not working with multiple transaction managers

Manage transactions with multiple datasource, entity managers for same application code

Java - Is it Possible to Use Spring Cloud Stream Kafka and RabbitMQ for the Same Application

Spring: Multiple Cache Managers

wrapping multiple calls in a transaction in spring boot application?

Nested transactions with different transaction managers in spring

Spring switch between 2 transaction managers (no single transaction behavior needed)

Is it possible to have multiple builds of the same application on TestFlight?

How to handle multiple entity update in the same transaction in Spring Data REST

Is it possible to use multiple desktop environments on same system?

Protractor: Is it possible to use multiple .get() at the same time?

Is it possible to use multiple for loop at same time?

Is it possible to use the same object in multiple files?

Is it possible to use same stopwatch multiple times

Is it possible to use Spring to create a console application?

Use multiple mongo DBs in same application for same model & same Repository

how to use multiple dispatchers in same cherrypy application?

Is it possible to use multiple @Qualifier annotation in Spring?

How to use Symfony autowiring with multiple entity managers

Is it possible to use the same app multiple times on the same page with VueJS?

Is it possible use multiple classes under the same namespace, in the same file

Use transaction for multiple save

How to use multiple controllers in Spring application?

Multiple ssl certificates (multiple domains) to same spring boot application

Is it possible to create multiple SSL certificates for providers that send APNs to the same Application?