首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

Spring整合hibernate-关于事物配置(无法回滚)解决方法

2012-06-19 
Spring整合hibernate-关于事物配置(无法回滚)在service层,保存用户的时候同时记录日志,在记录日志的DAO层

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="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> 



[解决办法]
<prop key="save*">PROPAGATION_REQUIRED</prop> 需要配置遇到异常回滚

例如:
Java code
<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>
[解决办法]
如果自己catch了异常,spring则不会管事务了 dao层不能包含try catch代码

热点排行