在service层,保存用户的时候同时记录日志,在记录日志的DAO层抛出RuntimeException,事物无法回滚,用户和日志都成功插入到数据库,并没有回滚,求解?事物配置哪里出问题?
<?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="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean> <bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean></beans>

解决方案 »

  1.   

     <prop key="save*">PROPAGATION_REQUIRED</prop> 需要配置遇到异常回滚例如:
    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
    <props>
    <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>

    </props>

    </property>
    </bean>
      

  2.   

    如果自己catch了异常,spring则不会管事务了 dao层不能包含try catch代码
      

  3.   


    默认是捕获RuntimeException就会事务回滚。
      

  4.   

    使用的MySQL数据库,设置中不支持事物,所以不会回滚;换做Oracle就OK