• [刘阳Java]_Spring对Transaction的支持_第12讲


    Spring对数据库事务管理是非常优秀,它利用AOP方式来管理数据库操作的事务。优点:主要体现了业务功能设计不会和事务代码耦合。在使用Spring对Transaction支持中建议采用声明式事务管理来完成。

    1. Spring对Transaction设计的代码步骤如下(重点关注配置文件编写,我们以JDBC事务管理来给大家进行阐述

    • 配置数据源
    • 配置事务管理器,DataSourceTransactionManager。这是事务管理器针对管理JDBC事务
    • 通过AOP让service包下所有Bean的所有方法拥有事务
    • 配置JdbcTemplate
    • 配置context:component-scan(扫描加入Spring注解的类),将标注Spring注解的类自动转化Bean,同时完成Bean的注入

    2. 配置文件代码如下

    <?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:p="http://www.springframework.org/schema/p"
        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-3.0.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
        <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
        <context:component-scan base-package="com.gxaedu.dao"/>
        <context:component-scan base-package="com.gxaedu.service"/>
    
        <!-- 配置数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close" 
            p:driverClassName="com.mysql.jdbc.Driver"
            p:url="jdbc:mysql://localhost:3306/sampledb" 
            p:username="root"
            p:password="1234" />
    
        <!-- 配置Jdbc模板  -->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
            p:dataSource-ref="dataSource" />
            
        <!-- 配置事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
            p:dataSource-ref="dataSource" />
            
        <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
        <aop:config proxy-target-class="true">
            <aop:pointcut id="serviceMethod"
                expression=" execution(* com.gxaedu.service..*(..))" />
            <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
        </aop:config>
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="add*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="*" read-only="true"/>
            </tx:attributes>
        </tx:advice>
    </beans>

     注意上面的配置文件有几个关键点,下面我会逐一的介绍一下

    • proxy-target-class
    • tx:advice, tx:attributes, tx:method

    3. 我们也不知道上面的配置是否真正的成功,请按照下面的方式进行测试。这一点大家要注意

    • 注意如果配置事务是接口代理则测试程序是接口
    • 交叉测试,一个Service里面有两个以上的Dao对象。让第一个Dao对象SQL语句能够执行成功,但是第二个Dao对象SQL语句操作失败,看事务能不能管理Service中的方法,让数据全部回滚

    4. proxy-target-class是什么意思

    • Spring在事务管理的时候是通过代理的方式来实现事务。
    • 但是Spring的代理方式采用AOP,在AOP中代理有两种:JDK动态代理和CGLIB类。所以proxy-target-class就是来设置代理方式的
    • proxy-target-class="true",则是基于类的代理(这时需要cglib库)
    • proxy-target-class="false",则是标准的JDK接口的代理

    5. tx:advice,tx:attributes,tx:method是什么意思

    • tx:advice,这个是事务中的建议
    • tx:attributes,它需要告知需要管理业务代码中的哪种操作类型的方法(如:数据库的增,删,改,查)
    • tx:method,指定具体的方法是哪些,一般会通过通配符来指定。如:insert*,则代表凡是以"insert"当头的方法需要进行事务管理。但是怎么管理,还需给tx:method标签中设置属性propagation="REQUIRED"
  • 相关阅读:
    NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.
    QOpenGLShaderProgram: could not create shader program
    ubuntu loading initial ramdisk 卡住
    ubuntu 下pip install sasl报错fatal error: sasl/sasl.h: No such file or directory
    ImportError: No module named managers
    python docker 多进程提供 稳定tensorflow gpu 线上服务
    侧脸生成正脸概论与精析(一)Global and Local Perception GAN
    pytorch安装 caffe2 安装:git 慢 caffe2 cannot find -lopencv_dep_cudart ,undefined reference to 'pthread_create'
    undefined symbol: PyFPE_jbuf
    Metasploit后渗透模块开发
  • 原文地址:https://www.cnblogs.com/liuyangjava/p/6692542.html
Copyright © 2020-2023  润新知