zoukankan      html  css  js  c++  java
  • 04-配置事务管理

    配置事务管理

    为什么要配置事务管理?

    因为在某些情况下,我们需要把若干个操作放到一起执行,并且这几个操作中的某一个失败,则所有操作都不生效。

    在spring中配置事务管理有两种方式:

    • 基于xml方式
    • 基于注解方式

    基于xml方式

    一般来说,为了满足单一职责的原则,我们配置事务时建议单独写一个xml文件

    配置事务,首先需要配置切面Pointcut

    记忆这个表达式的方法:public String top.bigking.crowd.service.AdminService.saveAdmin(Admin)

    *表示通配符, .. 表示任意的包及子包,或任意方法的参数

    <aop:pointcut id="txPointcut" expression="execution(* top.bigking.crowd.service.impl.*ServiceImpl.*(..))"/>
    

    然后为了将切入点表达式和事务通知关联起来,我们需要写aop:advisor

    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    

    可以看到的是,事务通知的txAdvice我们还没有编写,所以接下来我们使用tx:Advice来配置事务通知

    整个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:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
        <!-- 配置自动扫描,主要是为了把service扫描到IOC容器中 -->
        <context:component-scan base-package="top.bigking.crowd.service"/>
        <!-- 配置事务管理 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 装配数据源,尽管现在报错,但是运行时不会出错 -->
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!-- 配置事务切面 AOP -->
        <aop:config>
            <!-- 记忆这个表达式的方法:public String top.bigking.crowd.service.AdminService.saveAdmin(Admin) -->
            <!-- *表示通配符,   .. 表示任意的包及子包,或任意方法的参数 -->
            <!-- 考虑到后面我们需要整合springSecurity,避免把UserDetailsService加入事务控制,让切入点表达式定位到Service的实现类Impl -->
            <aop:pointcut id="txPointcut" expression="execution(* top.bigking.crowd.service.impl.*ServiceImpl.*(..))"/>
            <!-- 将切入点表达式和事务通知关联起来 -->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
        </aop:config>
        <!-- 配置事务通知 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <!-- 配置事务属性 -->
            <tx:attributes>
                <!-- 一般在涉及数据库时的操作是使用select,insert。但是在service层一般使用find,query,save等 -->
    
                <!-- 查询方法:配置只读属性,让数据库知道这是一个查询操作,能够进行一定的优化 -->
                <tx:method name="get*" read-only="true"/>
                <tx:method name="find*" read-only="true"/>
                <tx:method name="query*" read-only="true"/>
                <tx:method name="count*" read-only="true"/>
    
                <!-- 增删改方法:配置事务传播行为、回滚异常 -->
                <!--
                    propagation属性:
                        REQUIRED:默认值,表示当前方法必须工作在事务中,如果当前线程上没有已经开启的事务,则自己开启新事务。如果已经有了,那么就使用这个已有的事务。可能“被”回滚。
                        REQUIRES_NEW:建议使用的值,同样也表示当前方法必须工作在事务中。但是即使已经有了事务,也会自己开启一个事务运行
                 -->
                <!--
                    rollback-for属性:配置事务方法针对什么样的异常回滚
                        默认:运行时异常回滚RuntimeException
                        建议:编译时异常和运行时异常都回滚
                -->
                <tx:method name="save*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
                <tx:method name="update*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
                <tx:method name="remove*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
                <tx:method name="batch*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
            </tx:attributes>
        </tx:advice>
    </beans>
    

    基于注解方式

    下次有空再补上

    金麟岂是池中物,一遇风云便化龙!
  • 相关阅读:
    DP大作战—状态压缩dp
    DP大作战—组合背包
    DP大作战——多重背包
    单链表的使用——计算多项式加法
    单链表逆置
    钢条切割问题
    哈夫曼树及解码
    双“11”的抉择
    矩阵链相乘助教版代码
    abs()函数的返回值问题
  • 原文地址:https://www.cnblogs.com/ABKing/p/12493509.html
Copyright © 2011-2022 走看看