我是用s2sh,事务是配置在service层,但我在service层的方法中调用两个dao层的插入方法A方法和B方法,在这两个方法之中故意写一段异常代码测试事务是否回滚,测试时程序是报错了,但A方法不会回滚,不知道是什么原因?请问一下大家是怎样配置声明式事务??我的配置<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf" />
</bean>

<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.tfl.shop.service.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config> <tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>

<tx:method name="*" read-only="true" /> 

<tx:method name="add*" propagation="REQUIRED"/>

<tx:method name="update*" propagation="REQUIRED"/>

<tx:method name="delete*" propagation="REQUIRED"/>

</tx:attributes>
</tx:advice>
我的包目录如下:
com.tfl.shop.action
com.tfl.shop.common
com.tfl.shop.dao
com.tfl.shop.dao.impl
com.tfl.shop.manage
com.tfl.shop.manage.impl
com.tfl.shop.model
com.tfl.shop.service
com.tfl.shop.vo

解决方案 »

  1.   

    下面是我的service类方法,add()相当于A方法,add2()相当于B方法//插入一条数据
    String admin = testDao.add();

    String [] a = null;

    //System.out.println(a[0]);

    String admin2 = testDao.add2();

    if("true".equals(admin) && "true".equals(admin2)){
    return "ture";
    }

    return "false";
      

  2.   

    默认只有RuntimeException才会回滚,想普通的Exception也回滚的话,需要加上配置
      

  3.   

    tx:method 里加上  rollback-for="java.lang.Throwable"
      

  4.   

    首先你先尝试着抛出runtime异常,看能不能回滚,OK?
      

  5.   

    你不是有<tx:method name="delete*" propagation="REQUIRED"/>
    在里面继续添加啊
    <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
      

  6.   

    我的配置很简单,annotation的配置就一句话,事务加在service层,dao层报错肯定不会回滚的。。检查程序吧!
      

  7.   

    请问你的applicationContext.xml是如何配置的?
      

  8.   

    我想是,dao中的方法需要向上抛出异常这样在service才可以捕获。
    public boolean add()throws Exception{}
      

  9.   


    这个配置给你参考吧: <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation"
    value="classpath:hibernate.cfg.xml">
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.show_sql">false</prop>
    <prop key="hibernate.format_sql">true</prop>
    <!-- <prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop> -->
    </props>
    </property>
    </bean>

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

    <!-- 通知哪个事务类来管理事务 -->

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!-- 设置它的属性 -->
    <tx:attributes>
    <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
    <tx:method name="save*" propagation="REQUIRED"/>
    <tx:method name="update*" propagation="REQUIRED"/>
    <tx:method name="delete*" propagation="REQUIRED"/>
    <tx:method name="updateStock" no-rollback-for="MyApplicationException"/>
    <tx:method name="*"/>
    </tx:attributes>
    </tx:advice>
    <!-- <aop:config/> 的定义,确保由txAdvice bean定义的事务通知在应用中具体点被执行 -->
    <aop:config>
    <aop:pointcut id="ServiceMethod" expression="execution(* com.wb.sms.service.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="ServiceMethod"/>
    </aop:config>
      

  10.   


    把 <tx:method name="*" read-only="true" />放到最后一个试试。。你这个直接放在最前面。。那后面还配置出来干嘛?
      

  11.   


     <property name="sessionFactory" ref="sf" />除了检查一下这里以外 我看不出你的配置里面哪里有问题了
    你的类应该是public修饰的吧、?你配置的是public类切入。。
      

  12.   

    我service层里的方法都是public的哎还没解决呢。。
      

  13.   

    你用没有 SpringMvc 之前遇到过这个问题 是因为
     事物没有起作用 被springmvc的框架包住了
     
      

  14.   

    我没有用springMvc,只用s2sh框架
      

  15.   

    <!-- 配置自动扫描路径扫描类中的元注释来实现bean管理 -->
    <context:component-scan base-package="com.softfz.dao.impl" />
    <context:component-scan base-package="com.softfz.service.impl" />
      

  16.   


    <!-- 事务管理 -->
        <bean id="tranManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <tx:advice id="advice" transaction-manager="tranManager">
         <tx:attributes>
         <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
         <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
         <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
         <tx:method name="*" propagation="REQUIRED" read-only="true"/>
         </tx:attributes>
        </tx:advice>
        <aop:config>
         <aop:pointcut id="bizMethods" expression="execution(* com.orderfood.biz..*.*(..))"/>
         <aop:advisor advice-ref="advice" pointcut-ref="bizMethods"/>
        </aop:config>这是我的事务配置
    public void deleteMenu(int menuId) throws Exception{
    this.menuDao.deleteMenu(menuId);
    }这是对应的事务监控方法
      

  17.   

    请问这一句是怎么解释:
    <aop:pointcut id="bizMethods" expression="execution(* com.orderfood.biz..*.*(..))"/>还有这里一定要抛throws Exception??
      

  18.   


    <aop:pointcut id="bizMethods" expression="execution(* com.orderfood.biz..*.*(..))"/>
    这一句是说明事务管理的路径,
     <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
    rollback-for指明异常类型
      

  19.   

    配置一个切面,和需要拦截的类和方法
    <aop:pointcut id="bizMethods" expression="execution(* com.orderfood.biz..*.*(..))"/>
      

  20.   

    楼主留下联系邮箱把!! 我发个Spring事务配置文档给你, 你自己慢慢看!!
      

  21.   

    不知道你说的那种?
    <!-- 定义一个事务拦截器 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="get*" read-only="true" />
    <tx:method name="*" />
    </tx:attributes>
    </tx:advice>  <!--定义模块的service切入 -->
        <aop:config>
    <aop:pointcut id="customerPoint"
    expression="execution(* com.quanguowuliu.service..*.*(..))" />
    <aop:advisor advice-ref="txAdvice"
    pointcut-ref="customerPoint" />
    </aop:config>第二种注解
    @Service
    @Transactional
    标示上就可以了
      

  22.   

    你配置的事务 SERVICE 方法名称必须要跟你设置的能匹配
    我是用s2sh,事务是配置在service层,但我在service层的方法中调用两个dao层的插入方法A方法和B方法,在这两个方法之中故意写一段异常代码测试事务是否回滚,测试时程序是报错了,但A方法不会回滚,不知道是什么原因?请问一下大家是怎样配置声明式事务??我的配置XML code    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sf" /> </bean> <aop:config> <aop:pointcut id="bussinessService" expression="execution(public * com.tfl.shop.service.*.*(..))" /> <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" read-only="true" /> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> </tx:attributes> </tx:advice>你设的SERVICE方法中的方法名必须以  add,update,delete 开头命门规则。2。楼上几位也说了,你必须声明抛出哪种异常时,SERVICE事务会回滚。希望对你有用