Spring 中的事务管理学习心得

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).

posted on 2007-09-10 17:18 刘铮 阅读(880) 评论(0)  编辑  收藏 所属分类: Spring


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

留言簿(1)

文章分类(141)

文章档案(147)

搜索

最新评论