72

私はSpringを初めて使用しますが、同じアプリケーションで多数のトランザクションマネージャーを使用できるかどうか疑問に思っています。

2つのデータアクセス層があります。1つは両方のデータベース用です。1つのレイヤーに1つのトランザクションマネージャーを使用し、もう1つのレイヤーに別のトランザクションマネージャーを使用するにはどうすればよいのでしょうか。両方のデータベース間でトランザクションを実行する必要はありません-まだです。ただし、各データベースで個別にトランザクションを実行する必要があります。問題の概要を説明するための画像を作成しました。

代替テキスト

これが私のアプリケーションコンテキスト設定です:

<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リポジトリでは、別の永続性ユニットを持つエンティティマネージャファクトリを使用したいと思います。定義できるトランザクションマネージャーBeanは1つだけですが、リポジトリごとに異なるトランザクションマネージャーを使用するにはどうすればよいですか?

助けてくれてありがとう。

4

2 に答える 2

94

@Transactionalアノテーションを使用する場合、Bean名または修飾子に属性セットを追加することにより、使用するトランザクションマネージャーを指定できます。たとえば、アプリケーションコンテキストで、修飾子を使用して複数のトランザクションマネージャーが定義されている場合:

<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() { ... }
}
于 2010-12-12T18:33:47.173 に答える
5

このSpringJiraエントリでは、この問題について少し説明しています。

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

2フェーズコミットを使用していない場合は、接続ごとに1つのトランザクションマネージャーになる可能性があると思います。2つのトランザクションマネージャーを作成し、適切な接続をそれらに注入する必要があります。

しかし、私は質問をしなければなりません:なぜあなたは2人のトランザクションマネージャーが必要だと思いますか?複数のデータベース接続を持つことができます。接続を使用するDAOは、さまざまなサービスによってインスタンス化でき、インスタンス化する必要があります。各サービスには、独自のトランザクション設定で注釈を付けることができます。1人のマネージャーが両方に対応できます。なぜ2つ必要だと思いますか?

于 2010-12-12T18:15:39.410 に答える