SPRING控制事务及回滚

FROM:http://hua3392.blog.163.com/blog/static/8630511920091013112859610/

Spring 事务配置例:

<!-- hibernate事务管理 器-->
 <bean id="myTransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean> 

 

<!-- springmvc事务管理 器-->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>
  
 <!-- 声明式事务管理 -->
 <!-- 事务拦截器 -->
 <bean id="myTransactionInterceptor"
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager">
            <ref bean="myTransactionManager"/>
        </property>
        <property name="transactionAttributes">
     <!-- 下面定义事务传播属性-->
     <props>
      <prop key="save*">PROPAGATION_REQUIRED</prop>
      <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
      <prop key="delete*">PROPAGATION_REQUIRED</prop>
      <prop key="update*">PROPAGATION_REQUIRED</prop>
      <prop key="*">PROPAGATION_REQUIRED</prop>
     </props>
    </property>
    </bean>
     <!-- BeanNameAutoProxyCreator 自动生成事务代理-->
    <bean id="autoProxyCreator"
      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="interceptorNames">
      <list>     
       <value>myTransactionInterceptor</value>          
      </list>
    </property>
    <property name="beanNames">
      <list>
       <value>ProductService</value>
       <value>PersonService</value>
         <!-- 在此添加Bean --> 
      </list>
    </property>
    </bean>
 
  <!-- 应用BEAN配置 -->
 <bean id="PersonDAO" class="com.myweb.object.person.PersonDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
  <bean id="myPersonServiceTarget" class="com.myweb.object.person.PersonServiceImpl">
        <property name="dao">
            <ref bean="PersonDAO"/>
        </property>
    </bean> 
    <bean id="PersonService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces">
            <value>com.myweb.object.person.PersonService</value>
        </property>  
        <property name="interceptorNames">
            <list>
                <value>myHibernateInterceptor</value>
             <value>myPersonServiceTarget</value>
            </list>
        </property>
    </bean> 
  <bean id="ProductDAO" class="com.myweb.object.product.ProductDAO">
     <property name="sessionFactory">
      <ref bean="sessionFactory" />
     </property>
    </bean>
    <bean id="ProductService" class="com.myweb.object.product.ProductService">
        <property name="dao">
            <ref bean="ProductDAO"/>
        </property>
    </bean> 

 

在ProductService和PersonService的update*等方法中,不能对RuntimeException异常进行处理,否则事物不会回滚.

public void updateName()
 { 
  for(int i=0;i<10;i++)
  {   
   PersonId personid = new PersonId();
   personid.setId(27);
   personid.setAge(i+10+"");
   personid.setName("test"+i);
   Person person = new Person();
   person.setId(personid);
   dao.save(person);
   int j = 1/0;   //不能对该异常用try...catch处理,否则不会回滚事务
  }
 }

 public static void main(String[] args)
 {
  try
  {
   ApplicationContext ctx = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/classes/applicationContext.xml");
   PersonService per = (PersonService)ctx.getBean("PersonService");
   ProductService pro = (ProductService)ctx.getBean("ProductService");
   per.updateName();
   //pro.updateName();
  }catch(Exception e)
  {
   e.printStackTrace();
  }
  
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值