以下是spring的配置文件<!-- 指定dataSource配置文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc_mysql.properties</value>
</property>
</bean>
<!-- dataSource (各个属性值由配置文件读取) -->
<bean id="dataSource" class="${dataSource}">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
</props>
</property>
<property name="mappingResources">
<!-- 实体类映射文件 -->
</property>
</bean>
<!-- Spring 事务配置 -->
<context:annotation-config />
<context:component-scan base-package="com.bluesky" />
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="interceptorPointCuts"
expression="execution(* com.epo.biz.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
</aop:config>
<!-- Dao配置略 -->
<!-- BaseDao继承自org.springframework.orm.hibernate3.support.HibernateDaoSupport -->
<bean id="baseDao" class="com.epo.dao.base.BaseDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="orderDao" class="com.epo.dao.impl.OrderDaoImpl" parent="baseDao" />
<bean id="orderDetailDao" class="com.epo.dao.impl.OrderDetailDaoImpl" parent="baseDao" />
<bean id="orderCodeDao" class="com.epo.dao.impl.OrderCodeDaoImpl" parent="baseDao" />
<!-- orderBiz 注入 -->
<bean id="orderBiz" class="com.epo.biz.impl.OrderBizImpl">
<constructor-arg ref="orderDao" />
<constructor-arg ref="orderDetailDao" />
<constructor-arg ref="orderCodeDao" />
</bean>
下面是OrderBiz中的一个回滚失败的方法
package com.epo.biz.impl;import .......public class OrderBizImpl implements OrderBiz { private OrderDao orderDao;

private OrderDetailDao orderDetailDao;

private orderCodeDao orderCodeDao;

public OrderBizImpl(OrderDao orderDao, OrderDetailDao orderDetailDao
, orderCodeDao orderCodeDao) {
this.orderDao = orderDao;
this.orderDetailDao = orderDetailDao;
this.orderCodeDao = orderCodeDao;
} // OrderBizImpl
public addOrder(Order order, List<Details> details) {
try {
this.orderDao.add(order);
this.orderDetailDao.add(details);
this.orderCodeDao.update(order.getOrderCode()); // 这行代码出错,以上两个插入成功
} catch (Exception e) {
System.out.println(e);
}
}

}
以上的配置,在运行程序的时候
this.orderCodeDao.update(order.getOrderCode());
这句话出错,前面的两句仍然执行成功,spring的事务回滚失败了,不知道哪里弄错了,各位高手,帮忙看下!

解决方案 »

  1.   

    修改为以下代码尝试下,不要在方法里对异常进行处理public addOrder(Order order, List<Details> details) {
                this.orderDao.add(order);
                this.orderDetailDao.add(details);
                this.orderCodeDao.update(order.getOrderCode()); // 这行代码出错,以上两个插入成功
        }
      

  2.   

    spring声明式事务必须是面向接口编程,你的程序是否有接口?
      

  3.   


    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>这配置搞得连查询也需要用到事务么? 不要这么搞。。虽然一个*看起来比较简单省事。。影响系统性能
      

  4.   

    LS的意见很中肯。Spring处理的异常信息是RuntimeException,一般的Exception,需要自己处理一下。
      

  5.   

    同意一楼。
     public addOrder(Order order, List<Details> details) {
            try {
                this.orderDao.add(order);
                this.orderDetailDao.add(details);
                this.orderCodeDao.update(order.getOrderCode()); // 这行代码出错,以上两个插入成功
            } catch (Exception e) {
                System.out.println(e);
                throw new RuntimeException();        }
        }
      

  6.   

     OrderBizImpl implements OrderBiz 
      

  7.   

    假设我throw的是一个我自己的自定义异常,能被spring事务管理吗?try {
      this.orderDao.add(order);
      this.orderDetailDao.add(details);
      this.orderCodeDao.update(order.getOrderCode()); // 这行代码出错,以上两个插入成功
    } catch (Exception e) {
      throw new MyException();
    }
      

  8.   


    怎么不能呢?
    <tx:method name="clear*" propagation="REQUIRED" rollback-for="java.lang.Exception" />自定义异常都会继承自Exception。。