【问题标题】:Spring - Is it possible to use multiple transaction managers in the same application?Spring - 是否可以在同一个应用程序中使用多个事务管理器?
【发布时间】:2011-05-24 07:32:25
【问题描述】:

我是 Spring 新手,我想知道是否可以在同一个应用程序中使用多个事务管理器?

我有两个数据访问层 - 一个用于两个数据库。我想知道,您如何将一个事务管理器用于一层,而将不同的事务管理器用于另一层。我还不需要跨两个数据库执行事务。但我确实需要单独对每个数据库执行事务。我创建了一张图片来帮助概述我的问题:

这是我的应用程序上下文配置:

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

以下是使用此配置的示例:

@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;
    }
}

因此,对于帐户存储库,我想使用实体管理器工厂,并将持久性单元设置为帐户。但是,对于我的 BusinessData Repository,我想使用具有不同持久性单元的实体管理器工厂。由于我只能定义一个事务管理器 bean,我该如何为不同的存储库使用不同的事务管理器?

感谢您的帮助。

【问题讨论】:

    标签: java spring jpa spring-transactions transactionmanager


    【解决方案1】:

    这个 Spring Jira 条目稍微讨论了这个问题:

    https://jira.spring.io/browse/SPR-3955

    如果您不使用两阶段提交,我认为每个连接可能只有一个事务管理器。您只需要创建两个事务管理器并使用适当的连接注入它们。

    但我必须问一个问题:您认为为什么需要两个事务管理器?您可以拥有多个数据库连接。使用连接的 DAO 可以并且应该由不同的服务实例化,每个服务都可以使用自己的事务设置进行注释。一位经理可以同时兼顾两者。为什么你认为你需要两个?

    【讨论】:

    • 我以为我需要两个事务管理器,因为事务管理器指定了实体管理器工厂,而后者又指定了要使用的持久性单元。
    • 您能举个例子,说明 1 个事务管理器可以处理多个拥有自己数据源的数据库中的事务吗?
    • 例子?这就是事务管理器的工作方式。他们管理涉及多个数据库的两阶段提交。
    【解决方案2】:

    在您使用@Transactional 注释的地方,您可以通过将属性集添加到bean 名称或限定符来specify the transaction manager to use。例如,如果您的应用程序上下文使用限定符定义了多个事务管理器:

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

    您可以使用限定符来指定要使用的事务管理器:

    public class TransactionalService {
    
        @Transactional("account")
        public void setSomethingInAccount() { ... }
    
        @Transactional("businessData")
        public void doSomethingInBusinessData() { ... }
    }
    

    【讨论】:

    • 如果有一种方法可以从数据库中读取数据,而您不需要使用@Transactional 注解进行数据库读取。
    • 感谢您的回答!但你失去了这个&lt;tx:annotation-driven/&gt;
    • 如果@Transactional 没有使用哪个事务管理器的限定符会发生什么
    猜你喜欢
    • 2015-10-15
    • 2015-04-22
    • 2021-06-16
    • 2015-08-11
    • 1970-01-01
    • 2013-02-07
    • 2019-06-12
    • 1970-01-01
    相关资源
    最近更新 更多