Forums: Open Discussion (Thread #22729)

トランザクション境界について (2009-05-14 15:34 by Anonymous #43771)

お世話になります。

トランザクション境界についてご質問させてください。

現在、BusinessLogicにトランザクション境界を設けようとしております。

下記のようにapplicationContext.xml、moduleContext.xmlの設定ファイル2点に対して
設定を記述しているのですが、Exception発生時にロールバック処理が行われません。

--------------------------------------------------------------------------------------------------------------------
●applicationContext.xml
<!-- データソース設定 -->
<bean id="TerasolunaDataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<!-- トランザクション設定 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="TerasolunaDataSource"/>
</property>
</bean>

<bean id="attrSource"
class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>
</props>
</property>
</bean>

<!-- トランザクション処理(インタセプタの定義) -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributeSource">
<ref local="attrSource"/>
</property>
</bean>


<!-- トランザクションのAutoProxy設定 -->
<bean id="autoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

<!-- トランザクションのAutoProxy設定 -->
<property name="interceptorNames">

<!-- 適用するインタセプタ -->
<property name="interceptorNames">
<list>
<idref local="transactionInterceptor"/>
</list>
</property>

<!-- トランザクション対象の、BusinessLogic -->
<property name="beanNames">
<list>
<value>*Blogic</value>
</list>
</property>
</bean>

●moduleContext.xml

<!-- BusinessLogic定義 -->
<bean id="sampleBlogic" scope="singleton"
class="com.sample.blogic.SampleBlogicImpl">
<property name="updateDAO"><ref bean="updateDAO"/></property>
<property name="queryDAO"><ref bean="queryDAO"/></property>
</bean>

●SampleBlogicImpl.java

/**
* 更新
*/
public int update(Sample bean) throws Exception{
省略
// 更新処理1
updateDAO.execute("updateSample1", bean);

// ロールバックテストの為、Exceptionをthrowする。
if (true) {
throw new Exception();
}

// 更新処理2
updateDAO.execute("updateSample2", bean);

--------------------------------------------------------------------------------------------------------------------

Actionから、BusinessLogicであるSampleBlogicImpl.javaで更新処理1を行い、
例外を発生させているのですが、処理の行われた更新処理1がロールバックされずにコミットされてしまいます。

DBアクセスも出来ていて、DAOに設定したデータソースと同じものをトランザクションに指定しているのですが
何か書き方でまずい点、もしくは記述の足らない箇所があるのでしょうか。

恐れ入りますがご教授お願いします。

Reply to #43771×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

RE: トランザクション境界について (2009-05-15 10:28 by hachihachi #43778)

ContextLoaderListenerで読み込まれるBean定義ファイルと、
ContextLoaderPlugIn(Web版)あるいはDispatcherServlet(Rich版)で読み込まれるBean定義ファイルでは、親子関係がある別のコンテナとなります。
前者が親、後者が子となります。

親で定義したBean定義は子からも参照できます。(逆は参照できません)
AutoProxyの設定は親で定義した設定は子には反映されません。
(親で定義されたAutoProxyは、子のBeanを参照できないため)
そのため、AutoProxyの定義だけは親と子の双方に設定する必要があります。
トランザクションマネージャ、インターセプタは親に定義するだけで親子で共用できます。

AOPの設定をスキーマで記述した場合は、子側で<aop:config/>と定義するだけで親側の設定が子側にも反映されます。
Reply to #43771

Reply to #43778×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

RE: トランザクション境界について (2009-05-15 16:35 by 質問者 #43791)

AutoProxyの定義を、親と子のBean両方に記述してみたところ
正常にロールバックされることが確認出来ました。

大変参考になりました。
ご教授ありがとうございます。
Reply to #43778

Reply to #43791×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login