Spring 中的事務管理學習心得
可以使用<bean id="autoproxy"class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
…
</bean>
進行自動代理,但是這里要注意的是自動代理針對的是advisor,advisor是由advice和pointcut(interceptor也是advice的一種)所組成的,所以單獨的interceptor不能使用,也就是說TransactionInterceptor不能使用。
所以應該建立advisor,在事務管理中也就是TransactionAttributeSourceAdvisor。
TransactionAttributeSourceAdvisor中需要配置TransactionInterceptor。
配置示例如下:
<bean id="transactionAdvisor"
class="org.springframework.transaction.interceptor.
TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor">
<ref bean="transactionInterceptor"/>
</property>
</bean>
TransactionInterceptor的配置如下:
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.
TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource"/>
</property>
</bean>
其中transactionManager的配置根據所使用的存儲方法不同而不同:
使用jdbc的配置如下:
<bean id="transactionManager" class="org.springframework.jdbc.
datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
使用hibernate的配置如下:
<bean id="transactionManager" class="org.springframework.
orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
而transactionAttributeSource是一個接口,一般使用NameMatchTransactionAttributeSource實現此接口
<bean id="transactionAttributeSource"
class="org.springframework.transaction.interceptor.
NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="get*">PROPAGATION_SUPPORTS</prop>
</props>
</property>
</bean>
注意其中的properties屬性是一個name/attribute 的map其中name是針對需要進行的事務管理的方法attribute是事務管理的屬性由TransactionAttribute決定
而TransactionAttribute擴展的事務定義基本類TransactionDefinition,在這個基本類上面加上了boolean rollbackOn(Throwable ex)方法
所以<prop key="get*">PROPAGATION_SUPPORTS</prop>的attribute還可以添加:
如<prop key="get*">PROPAGATION_SUPPORTS,
ISOLATION_SERIALIZABLE
,readonly</prop>
以上是從宏觀到微觀的分析,
以下是從微觀到宏觀的分析:
1。設置事務管理屬性
<bean id="transactionAttributeSource"
class="org.springframework.transaction.interceptor.
NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="get*v>PROPAGATION_SUPPORTS</prop>
</props>
</property>
</bean>
2。設置事務管理器,根據所使用的存儲方法不同而不同:以hibernate為例
<bean id="transactionManager" class="org.springframework.
orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
3。設置interceptor
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.
TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource"/>
</property>
</bean>
4。最后設置advisor:
<bean id="transactionAdvisor"
class="org.springframework.transaction.interceptor.
TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor">
<ref bean="transactionInterceptor"/>
</property>
</bean>
所以就可以通過自動代理進行代理了,
注意這里的advisor還是有pointcut的,pointcut在事務管理中設置了,例如:
<props>
<prop key="get*">PROPAGATION_SUPPORTS</prop>
</props>
還有由于Spring本身的框架的原因,如果在Log4j中的調試級別為debug的話,那么設置advisor時就不能使用property方法,而要使用constructor-arg方法
使用MethodMapTransactionAttributeSource可以對特定的類的方法進行事務管理
設置property------setMethodMap
Set a name/attribute map, consisting of "FQCN.method" method names (e.g. "com.mycompany.mycode.MyClass.myMethod") and
TransactionAttribute
instances (or Strings to be converted to
TransactionAttribute
instances).