AOP using multiple transaction managers on same method
Hi
I have spring file -
<bean id="transactionManager01" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
<property name="dataSource" ref="dataSource01"/>
</bean>
<!-- the transactional advice bean below) -->
<!-- the transactional semantics... -->
<tx:advice id="txAdvice01" transaction-manager="transactionManager01">
<tx:attributes>
<tx:method name="storeLhrSendData" propagation="REQUIRES_NEW" />
<tx:method name="storeLhrRecieveData" propagation="REQUIRES_NEW" />
</tx:attributes>
</tx:advice>
<bean id="transactionManager02" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
<property name="dataSource" ref="dataSource02"/>
</bean>
<tx:advice id="txAdvice02" transaction-manager="transactionManager02">
<tx:attributes>
<tx:method name="storeLhrSendData" propagation="REQUIRES_NEW" />
<tx:method name="storeLhrRecieveData" propagation="REQUIRES_NEW" />
</tx:attributes>
</tx:advice>
<bean id="transactionManager03" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
<property name="dataSource" ref="dataSource03"/>
</bean>
<tx:advice id="txAdvice03" transaction-manager="transactionManager03">
<tx:attributes>
<tx:method name="storeLhrSendData" propagation="REQUIRES_NEW" />
<tx:method name="storeLhrRecieveData" propagation="REQUIRES_NEW" />
</tx:attributes>
</tx:advice>
<bean id="transactionManager04" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
<property name="dataSource" ref="dataSource04"/>
</bean>
<tx:advice id="txAdvice04" transaction-manager="transactionManager04">
<tx:attributes>
<tx:method name="storeLhrSendData" propagation="REQUIRES_NEW" />
<tx:method name="storeLhrRecieveData" propagation="REQUIRES_NEW" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="storeServiceOperation" expression="execution(* sf.consumerrpt.dataaccess.bo.*.*(..))"/>
<aop:advisor advice-ref="txAdvice01" pointcut-ref="storeServiceOperation"/>
<aop:advisor advice-ref="txAdvice02" pointcut-ref="storeServiceOperation"/>
<aop:advisor advice-ref="txAdvice03" pointcut-ref="storeServiceOperation"/>
<aop:advisor advice-ref="txAdvice04" pointcut-ref="storeServiceOperation"/>
</aop:config>
The methods storeLhrSendData() && storeLhrRecieveData() performs inserts and updates in a table that exists across all 4 databases (using 4 diff jndi's). I get a parameter based on which we deteremine which database to connect.
Since all 4 transaction advices are applied irrespective of the database in use, How are these transactions managed.
For eg:
i used jdbc01 to perform operations on the above methods and we see all 4 advices are applied. Since txAdvice01 is applied it performs the commit or rollback. I want to know Is there any impact/overhead of using txAdvice02/03/04?????
I have seen the code working while connecting different jdbc connections 01/02/03/04 and works since all transactions are applied. I neeed to find out is there any overhead by configuring this way (like too many transactions open)
HELP Needed please in understanding the concept of how it works, before deploying my code in production. :(
Do and do only when needed
In your case, all 4 tx (transactions) are opened when one of these methods is called and are closed (commit or rollback) after the execution of the method. Yes there is overhead of opening 4 tx while you need only 1, 3 more tx management run time is no big deal, but there are other overheads:
- 4 tx are opened means at the database side, 4 databases have to do something to maintain these tx. If the run time of these methods is short, it's ok, but if it is not, the side effect is amplified in a way the performance is degraded even more.
- In some case, for ex, dataSource1 is gone, and because of that, your can not even change or get something from dataSource2 (or 3, 4), because the method call will fail at the beginning while trying to open tx of dataSource1
- 4 tx are bound together but the requirement is just 1 datasource used at once
In my opinion, this is design issue. I would advise you to make something with 4 different "pointcut" for 4 different datasource. Why not create 4 transactional beans (for ex, DAO) to do the data processing on different databases and determine at run time which DAO object to use.
Hoping this helps!