2017-05-27 12 views
0

私のプロジェクトでは、1つのトランザクションでさらに多くのデータベースを処理する必要があります。トランザクションの注釈またはxxx

1:しかし、XMLを使用して、それが正常に動作します:

<bean id="transactionManager1" 
     class="org.springframework.jdbc.DataSourceTransactionManager"> 
     <qualifier value="order"/> 
</bean> 
<bean id="transactionManager2" 
     class="org.springframework.jdbc.DataSourceTransactionManager"> 
     <qualifier value="account"/> 
</bean> 

2に従うとして、アノテーションを使用して、これがエラー "重複した注釈"

public class TransactionalService { 

@Transactional("order") 
@Transactional("account") 
public void processTwoDatabases(String name) { ... } 

} 

XMLセグメントを報告し

<tx:advice id="txAdvice1" transaction-manager="transactionManager1"> 
    <!-- 定义方法的过滤规则 --> 
    <tx:attributes> 
     <tx:method name="process*" propagation="REQUIRED" read-only="false" 
        rollback-for="java.lang.Exception"/> 
    </tx:attributes> 
</tx:advice> 


<aop:config proxy-target-class="true"> 
    <aop:pointcut expression="execution (* com.service.impl.*.*(..))" id="services1"/> 
    <aop:advisor advice-ref="txAdvice1" pointcut-ref="services1"/> 
</aop:config> 


<tx:advice id="txAdvice2" transaction-manager="transactionManager2"> 
    <tx:attributes> 
     <tx:method name="process*" propagation="REQUIRED" read-only="false" 
        rollback-for="java.lang.Exception"/> 
    </tx:attributes> 
</tx:advice> 


<aop:config proxy-target-class="true"> 
    <aop:pointcut expression="execution (* com.service.impl.*.*(..))" id="services2"/> 
    <aop:advisor advice-ref="txAdvice2" pointcut-ref="services2"/> 
</aop:config> 

答えて

0

Javaは同じ要素の同じ型の複数の注釈を許可しませんが、注釈は、@Repeatableで-annotated(メタ)である場合を除き:Multiple annotations of the same type on one element?

しかしTransactionalは、(メタ)ない、したがって@Transactionalのインスタンスが1つだけのタイプまたは方法に許可され、@Repeatableで-annotated。

この場合、XMLを使用する必要があります。

ただし、1回の取引は発生しませんのでご注意ください。実際には、2つの異なるトランザクション(トランザクションマネージャごとに1つ)を取得します。

また、トランザクションマネージャ定義にはデータソースを指定しないため、両方とも同じデータソース(および同じデータベース)を使用します。

実際に1つのトランザクションを持つには、XAトランザクションのサポートが必要な場合があります。ここではいくつかのリンクがあります:

  1. http://www.javaworld.com/article/2077714/java-web-development/xa-transactions-using-spring.html
  2. http://www.javaworld.com/article/2077963/open-source-tools/distributed-transactions-in-spring--with-and-without-xa.html
  3. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-jta.html
+0

2つのトランザクションのいずれかが失敗した場合、私はすべてのトランザクションをロールバックします。私の方法では、2つのtransations次のよう
'(1)(2)5( '' '(3)自分のビジネス・ロジック・コード' '(4)トランザクション2端' を開始トランザクション2 ' を開始トランザクション1 )transaction1 end ' なので、1-4の間に例外がスローされると、2つのトランザクションがロールバックされます。 (4)と(5)の間に例外がスローされ、外部トランザクションのみがロールバックされます 、内部トランザクションはコミットされます。 '@Roman Puchkovskiy' –

+0

XTトランザクションについての私の編集に注意してください –

関連する問題