Spring的事务处理

Spring有两种事务处理方式,编程式事务处理(programmatic transaction management)与声明式事务处理(declarative transaction management)。

        编程式事务处理比传统的JDBC事务有所改进,但是还是要实现具体方法。虽然有三种实现Spring编程式事务处理的方法,但其中都必须写入特定的代码,这样做一是代码量增加,DAO里面每个方法都加上那些必须要写上的代码那也是很烦人的,二是耦合度也提高了,哪天哪天不用Spring用XX也说不定呀....-_-!。

        声明式事务处事,在DAO中你的对数据库操作的代码可以随意的写上,不用加任何其它代码,事务的处理交给Spring的TransactionProxyFactoryBean来代为处理。

 xxDAO:

  1. .......   
  2. //对数据库的操作      
  3. public void update(String name,String password){      
  4.           //业务处理方法   
  5.           //update1();   
  6.           //update2();      
  7.     
  8. ......   
 

Spring配置文件

  1. <bean id="dataSource"  
  2.     class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  3.     <property name="driverClassName">  
  4.         <value>com.microsoft.jdbc.sqlserver.SQLServerDrivervalue>  
  5.     property>  
  6.     <property name="url">  
  7.         <value>jdbc:microsoft:sqlserver://localhost:1433/xxvalue>  
  8.     property>  
  9.     <property name="name">  
  10.         <value>usenamevalue>  
  11.     property>  
  12.     <!-- 设定密码 -->  
  13.     <property name="msg">  
  14.         <value>passwordvalue>  
  15.     property>  
  16. bean>  
  17. <bean id="transactionManager"  
  18.     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  19.     <property name="dataSource">  
  20.         <ref bean="dataSource" />  
  21.     property>  
  22. bean>  
  23. <bean id="xxDAOProxy"  
  24.     class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  25.     <property name="transactionManager">  
  26.         <ref bean="transactionManager" />  
  27.     property>  
  28.     <property name="target">  
  29.         <ref bean="xxDAO" />  
  30.     property>  
  31.     <property name="transactionAttributes">  
  32.         <props>  
  33.             <prop key="update*">PROPAGATION_REQUIREDprop>  
  34.         props>  
  35.     property>  
  36. bean>  

还有一种配置文件的写法,更加的灵活点。

  1. <bean id="transactionInterceptor"  
  2.     class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  3.     <property name="transactionManager">  
  4.         <ref bean="transactionManager" />  
  5.     property>  
  6.     <property name="transactionAttributeSource">  
  7.         <value>org.eyeqq.DAO.xxDAO.update*=PROPAGATION_REQUIREDvalue>  
  8.     property>  
  9. bean>  
  10. <bean id="xxDAOProxy"  
  11.     class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  12.     <property name="interceptorNames">  
  13.         <value>transactionInterceptor,xxDAOvalue>  
  14.     property>  
  15. bean>  

如果使用Hibernate的话那么我们的TransactionManager应该换成这样子。

xml 代码
  1. <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">     
  2.     <property name="sessionFactory" ref="sessionFactory"/>            
  3. </bean>       
  4.   
  5. id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">     
  6.     <property name="dataSource" ref="dataSource" />           
  7.     <property name="mappingResources">     
  8.         <list>     
  9.             <value>org.eyeqq.vo.userVO.hbm.xml</value>     
  10.         </list>     
  11.     </property>     
  12.     <property name="hibernateProperties">     
  13.         <props>     
  14.             <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>     
  15.             <prop key="hibernate.show_sql">true</prop>     
  16.         </props>     
  17.     </property>     
  18. </bean>   

这样子,只要在service里面调用update方法,Spring就会自动给你完成事务的处理。

XML代码说明:

 数据源那很简单看看就明白了....

  1. <property name="target">     
  2.     <ref bean="xxDAO" />     
  3. property>     
  4. <property name="transactionAttributes">     
  5.     <props>     
  6.         <prop key="update*">PROPAGATION_REQUIREDprop>     
  7.     props>     
  8. property>  
 

target属性指定要被TransactionProxyFactoryBean代理处理事务的类为xxDAO。

transactionAttributes指定xxDAO中需要实现事务处理的方法。

事务属性值:

PROPAGATION_REQUIRED:如果当前没有事务,就新建一个事务。

PROPAGATION_SUPPORTS:如果当前没有事务,就以非事务方式执行。

PROPAGATION_MANDATORY:如果当前没有事务,就抛出异常。

PROPAGATION_REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。

PROPAGATION_NOT_SUPPORTED:以非事务的方式执行操作,如果当前存在事务,就把当前事务挂起。

PROPAGATION_NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值