Spring 中的事务控制

编程式事务、这里不讲,就是将事务的开启关闭写在代码里。不做重点。

1、Spring的声明式事务控制

编程式事务管理将数据层提交事务的代码加入逻辑层,与Spring无侵入式编程思想的主思想冲突,实际开发过程中,往往采用声明式事务管理形式

通过编程式事务管理的代码不难看出,在业务逻辑层对应的业务上添加某些代码即可完成整体事务管理的操作,SpringAOP的思想,将公共的代码加入后,即可完成对应的工作,这就是声明式事务管理的核心机制。

1‘.1、基于XML的声明式事务配置

a、导入spring事务支持的jar包spring-tx-3.2.0.RELEASE.jar

b、引入spring事务的名称空间
这里写图片描述

c、配置spring的事务支持

<?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"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans   
                           http://www.springframework.org/schema/beans/spring-beans.xsd   
                           http://www.springframework.org/schema/tx   
                           http://www.springframework.org/schema/tx/spring-tx.xsd  
                           http://www.springframework.org/schema/aop   
                           http://www.springframework.org/schema/aop/spring-aop.xsd">  
    <!-- 把Dao交给Spring管理 -->  
    <bean id="accountDao" class="springTX.dao.impl.AccountDaoImpl">  
        <!-- 为dao的父类JdbcDaoSupport注入一个数据源 -->  
        <property name="dataSource" ref="driverManagerDataSource"></property>  
    </bean>  

    <!-- 把业务层也交给Spring -->  
    <bean id="accountService" class="springTX.service.impl.AccountServiceImpl">  
        <property name="accountDao" ref="accountDao"></property>  
    </bean>  

    <!-- 事务控制:声明式事务,重点。基于XML的。  
         前期准备:  
            1.导入aop的jar包  
            2.确保spring-tx-xxxx.jar在你的工作空间中  
         步骤:  
            1.配置一个事务管理器,并为其注入数据源  
            2.配置事务的通知.id是为其指定一个唯一标识。transaction-manager指定事务通知使用的管理器  
            3.配置AOP,指定切面使用的通知。<aop:advisor advice-ref="" pointcut="切入点表达式"/>它就是指定已经配置好的通知类型。  
    -->  
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="driverManagerDataSource"></property>  
    </bean>  
    <tx:advice id="txAdivce" transaction-manager="transactionManager">  
        <!-- 配置事务的相关属性 -->  
        <tx:attributes>  
            <!-- tx:method 是用于指定方法名称和事务属性的关系。  
                 name:指定方法的名称。可以使用通配符*,也可以部分通配  
                 isolation:事务的隔离级别。默认值是:DEFAULT。当是default时是看数据库的隔离级别。   
                 rollback-for:该属性用于配置一个异常,当产生该异常时回滚。  
                 no-rollback-for:该属性用于配置一个异常,当产生该异常时不回滚。  
                 propagation:指定事务的传播行为。 默认值是:REQUIRED  
                 read-only:指定当前事务是否是只读事务。默认值是false,不是只读的。  
                 timeout:指定超时时间。默认值是-1。以秒为单位  
             -->  
            <tx:method name="*" />  
            <tx:method name="find*" read-only="true"/>  
        </tx:attributes>  
    </tx:advice>  
    <aop:config>  
        <aop:advisor advice-ref="txAdivce" pointcut="execution(* springTX..*.*(..))"/>  
    </aop:config>  
    <!-- 配置Spring的内置数据源 -->  
    <bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <!-- 给数据源注入参数 -->  
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
        <property name="url" value="jdbc:mysql://localhost:3306/spring_tx"></property>  
        <property name="username" value="root"></property>  
        <property name="password" value="1234"></property>  
    </bean>  
</beans>  

OK、基于xml的声明式事务、配置完毕。

2、基于注解的声明式事务配置

a、把spring容器管理改为注解配置的方式(开启要扫描的包)

b、开启注解事务的支持
这里写图片描述

配置结果如下:

<?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:context="http://www.springframework.org/schema/context"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans   
                           http://www.springframework.org/schema/beans/spring-beans.xsd   
                           http://www.springframework.org/schema/tx   
                           http://www.springframework.org/schema/tx/spring-tx.xsd  
                           http://www.springframework.org/schema/aop   
                           http://www.springframework.org/schema/aop/spring-aop.xsd   
                           http://www.springframework.org/schema/context   
                           http://www.springframework.org/schema/context/spring-context.xsd">  
    <!-- 事务控制:声明式事务,重点。基于注解的。  
         前期准备:  
            1.导入context的名称空间  
            2.确保spring-tx-xxxx.jar在你的工作空间中  
         步骤:  
            1.设置Spring要扫描的包  
            2.开始Spring对注解式事务的支持。并且指定事务管理器类  
            3.把Dao和service都交给Spring管理  
            4.由于我们使用了注解,所以不能用JdbcDaoSupport,这个时候我们需要定义JdbcTemplate,为其注入数据源  
    -->  
    <context:component-scan base-package="springTX"></context:component-scan>  

    <tx:annotation-driven transaction-manager="transactionManager"/>  

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="driverManagerDataSource"></property>  
    </bean>  
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
        <property name="dataSource" ref="driverManagerDataSource"></property>  
    </bean>  

    <!-- 配置Spring的内置数据源 -->  
    <bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <!-- 给数据源注入参数 -->  
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
        <property name="url" value="jdbc:mysql://localhost:3306/srping_tx"></property>  
        <property name="username" value="root"></property>  
        <property name="password" value="1234"></property>  
    </bean>  
</beans>  

c、在需要事务支持的地方加入@Transactional注解(一般在service层控制事务)
d、@Transactional注解配置的位置

用在业务实现类上:该类所有的方法都在事务的控制范围
这里写图片描述

用在业务接口上:该接口的所有实现类都起作用

这里写图片描述
通过注解指定事务的定义信息
这里写图片描述

OK、基于注解的事务也整理完毕!

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值