發現沒有專案在Go的話,看書練功是一回事,面對實際才知道是怎麼回事。以下是spring 2.0以前對transaction的配置:
<bean id="txTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED, -Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED, -Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED, -Exception</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
上例表示txTransactionProxy這個bean對insert、update、delete開頭的method納入transactionManager管理,其他method(*)表示只能read only。
欲被txTransactionProxy管理的bean需做如下的配置:
<bean id="foo" parent="txTransactionProxy">
<property name="proxyInterfaces">
<list>
<value>com.xyz.mgr.FooMgr</value>
</list>
</property>
<property name="target">
<ref bean="fooMgrImpl" />
</property>
</bean>
<property name="fooDao" ref="fooDao" />
</bean>
如此一來,foo這spring bean裡所代理的interface(如FooMgr),其target實作(fooMgrImpl)以及fooMgrImpl所Injection的Class(fooDao)均受txTransactionProxy管理其Transaction。
因此fooMgrImpl和fooDao的method若非insert、update、delete開頭皆不能進行transaction。而若fooMgrImpl呼叫一個fooUtil進行增改刪,因fooUtil沒有被配置,就算fooUtil的method是insert、update、delete開頭也無法進行transaction。
後來Benny有提,在Spring只會對單一method裡所進行的增刪改做transaction,假設FooMgr有兩個insert開頭的method,前後被caller呼叫,這兩個method彼此各自為獨立的transaction。
留言列表