Spring的事务配置,在service中发生异常后,整个方法内的数据库操作全部回滚,现在我想当方法内的更新或者删除语句返回值为0的时候也要全部回滚,spring有这样的配置吗?(除了判断返回值手动抛异常)Spring数据库事务

解决方案 »

  1.   

    可以自己实现函数,控制commit或者非commit;
    至于spring有么有,我不知道了。
      

  2.   

    谢谢!网上我也没查到spring相关的配置。
      

  3.   

    你看这个能帮到你么 用的spring的txAdvice和aop
    aop中配置事务作用路径,需要处理事务的方法名后加 TX 代表此方法是进行事务的。<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory" />
    </property>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <!-- all methods starting with 'get' are read-only -->
    <tx:method name="*TX" propagation="REQUIRED" />
    <!-- other methods use the default transaction settings (see below) -->
    <tx:method name="*" read-only="true" />
    </tx:attributes>
    </tx:advice>
    <aop:config>
    <aop:pointcut id="transactioncut"
    expression="execution(* *..*BPO.*(..))" />
    <aop:advisor advice-ref="txAdvice"
    pointcut-ref="transactioncut" />
    </aop:config>
    <bean id="daoSupport"
    class="路径"
    abstract="true">
    <property name="sessionFactory">
    <ref local="sessionFactory" />
    </property>
    <property name="ds">
    <ref local="dataSource" />
    </property>
    </bean>
      

  4.   

    不能编辑- -  配置文件开头还要引用一下
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
      

  5.   

    非常感谢,我也是这么配置的,但是这只是对service方法发生异常时管用,但是更新删除0条时不发生异常就没法控制回滚了。
      

  6.   

    其实我还是不明白  你既然返回0了  就表示已经commit了 又怎么回滚呢  
      

  7.   

    一个service方法内会调很多个Dao方法去操作数据库,如果没有发生异常则事务提交,如果方法内发生异常抛出去了整个方法的数据库操作就全部回滚,现在是想中间弄个更新或者删除Dao方法返回0,也要整个方法回滚。
      

  8.   

    你先按照删除和修改的条件查询一下   如果返回的list大小为0 就不执行  如果不为0再全部执行事务
      

  9.   

    要不然 你执行增删改 有返回值那肯定是已经commit了  不能回滚了   我是这么想的 呵
      

  10.   

    你看看aop切面编程,讲的就是这方面的!
      

  11.   

    谢谢大家的回答,用aop切面编程也还是没有找到具体的做法。明天继续找。