java spring配置事务回滚的案例方法

spring | 2019-09-13 10:02:39

以前都不玩事务,或让hibernate做数据库事务回滚,不安全也不方便。为了规范自己的编码习惯,就做了spring管理事务,我的思路是action调用services,aop所有的services,只要services的任何地方出错,就回滚这个service方法。理解起来简单,配置起来也简单:

1.spring applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
        default-autowire="byName">
        <!-- 数据库配置 -->
        <bean id="dataSource"
                class="org.apache.commons.dbcp.BasicDataSource">
                <property name="driverClassName"
                        value="com.mysql.jdbc.Driver">
                </property>
                <property name="url" value="jdbc:mysql://localhost:3306/ckd_ios?characterEncoding=utf-8"></property>
                <property name="username" value="root"></property>
                <property name="password" value=""></property>
        </bean>
        <bean id="sessionFactory"
                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                <property name="dataSource">
                        <ref bean="dataSource" />
                </property>
                <property name="hibernateProperties">
                        <props>
                                <prop key="hibernate.dialect">
                                        org.hibernate.dialect.MySQLDialect
                                </prop>
                                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                        </props>
                </property>
                <property name="mappingResources">
                        <list>
                                <!--配置dao也要在这里配置一下-->
                                <value>dao/user/User.hbm.xml</value>
                        </list>
                </property>
        </bean>
        <!--配置事物回滚-->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory">
                        <ref bean="sessionFactory" />
                </property>
        </bean>
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
                <tx:attributes>
                        <tx:method name="*" propagation="REQUIRED" />
                </tx:attributes>
        </tx:advice>
        <aop:config>
                <aop:pointcut id="allDaoMethod" expression="execution(* service.*.*(..))" />
                <aop:advisor advice-ref="txAdvice" pointcut-ref="allDaoMethod" />
        </aop:config>
        
        
        <!-- action -->
        <bean id="userAction" class="action.UserAction" scope="prototype" />
        
        <!-- services -->
        <bean id="userService" class="service.UserService" />
        
        <!-- dao -->
        <bean id="userDAO" class="dao.user.UserDAO">
                <property name="sessionFactory">
                        <ref bean="sessionFactory" />
                </property>
        </bean>
        
        
</beans>



2.配置说明
只需要看事务回滚的部分就可以了,复制到自己的代码中也只要复制配置事物回滚这部分就可以了。
最重要的就是<aop:pointcut id="allDaoMethod" expression="execution(* service.*.*(..))" />
就是监听services包下的所有方法,只要这个方法出异常就回滚。

另外<tx:method name="*" propagation="REQUIRED" />
PROPAGATION_REQUIRED
如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。

登录后即可回复 登录 | 注册
    
关注编程学问公众号