日期:2014-05-19  浏览次数:20669 次

Spring整合hibernate-关于事物配置(无法回滚)
在service层,保存用户的时候同时记录日志,在记录日志的DAO层抛出RuntimeException,事物无法回滚,用户和日志都成功插入到数据库,并没有回滚,求解?事物配置哪里出问题?

Java code

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName">
            <value>net.sourceforge.jdbclogger.JdbcLoggerDriver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/spring</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>root</value>
        </property>
    </bean>

    <bean id="userDAO" class="com.dao.impl.UserDAOImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="logDAO" class="com.dao.impl.LogDAOImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="userService" class="com.service.impl.UserServiceImpl">
        <property name="userDAO" ref="userDAO"></property>
        <property name="logDAO" ref="logDAO"></property>
    </bean>
    <bean id="logService" class="com.service.impl.LogServiceImpl">
        <property name="logDAO" ref="logDAO"></property>
    </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <list>
                <value>user.hbm.xml</value>
                <value>log.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>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="current_session_context_class">thread</prop>
            </props>
        </property>
        <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"/> -->
    </bean>

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