春 - 同じアプリケーションで複数のトランザクションマネージャーを使用することは可能ですか?
-
09-10-2019 - |
質問
私は春が初めてで、同じアプリケーションで多数のトランザクションマネージャーを使用できるかどうか疑問に思っていますか?
2つのデータアクセスレイヤーがあります。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リポジトリを使用すると、別のPersistenceユニットを持つエンティティマネージャーファクトリーを使用したいと思います。 1つのトランザクションマネージャーBeanしか定義できないので、異なるリポジトリに異なるトランザクションマネージャーを使用するにはどうすればよいですか?
助けてくれてありがとう。
解決
あなたが使用する場所 @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() { ... }
}
他のヒント
この春のジラのエントリは、この問題について少し議論しています。
https://jira.spring.io/browse/spr-3955
2フェーズのコミットを使用していない場合、接続ごとに1つのトランザクションマネージャーになる可能性があると思います。 2つのトランザクションマネージャーを作成し、適切な接続でそれらを挿入するだけです。
しかし、私は質問をしなければなりません:なぜあなたは2つのトランザクションマネージャーが必要だと思いますか?複数のデータベース接続を使用できます。接続を使用するDAOは、異なるサービスによってインスタンス化できます。 1人のマネージャーが両方に対応できます。なぜ2つが必要だと思いますか?