日期:2014-05-20  浏览次数:20837 次

Spring 事务回滚问题 声明式事务回滚失败
以下是spring的配置文件
XML code

<!-- 指定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中的一个回滚失败的方法

Java code

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);
        }
    }
    
}