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

spring事务不回滚
初学spring,然而调试了好久,都没能让其声明式事务发挥作用,无奈,求助!

spring配置文件:

XML code

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
        <property name="url"><value>jdbc:mysql://localhost:3306/orm</value></property>
          <property name="username"><value>root</value></property>
        <property name="password"><value>root</value></property>
    </bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="mappingResources">
    <list>
    <value>com/itlover/hibernate/User.hbm.xml</value>
    <value>com/itlover/hibernate/Card.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    
    <bean id="baseDao" class="com.itlover.dao.DefaultDaoImpl">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    
    <bean id="baseService" class="com.itlover.service.DefaultServiceImpl">
    <property name="baseDao"><ref local="baseDao"/></property>
    </bean>
    
    <bean id="defaultDaoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager"><ref bean="transactionManager"/></property>
    <property name="target"><ref local="baseDao"/></property>
    <property name="transactionAttributes">
        <props>
            <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
            </props>
    </property>
    </bean>
    
    <bean name="/hibernate" class="com.itlover.struts.action.HibernateAction" singleton="false">
    <property name="baseService"><ref bean="baseService"/></property>
    </bean>
    
</beans>



执行操作的是保存一个 User, User跟Card进行cascade级联,即保存User时也让Hibernate保存Card

,程序中故意把 card.setCardBankname("招商银行"); 给注释掉(此属性不能为空),就是希望在此处可以看到
spring的事务是否生效...


具体代码为:

Java code

Action.java

public ActionForward test(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        User user = new User();
        user.setUserName("susan");
        user.setUserAge(21);
        
        Card card = new Card();
    //    card.setCardBankname("招商银行");
        card.setCardNumber("6225888811558899");
        card.setUser(user);
        
        user.getCards().add(card);
        
        baseService.saveEntity(user);
        
        return null;
    }


Service.java

public class DefaultServiceImpl implements BaseService
{
    private BaseDao baseDao;
    public void saveEntity(Object entity)
    {
        baseDao.saveEntity(entity);
    }
    
    public void setBaseDao(BaseDao baseDao)
    {
        this.baseDao = baseDao;
    }
}


Dao.java

public class DefaultDaoImpl extends HibernateDaoSupport implements BaseDao
{
    public void saveEntity(Object entity)
    {
        getHibernateTemplate().save(entity);
    }
}