事务控制

  事务是什么?事务控制?

  事务这个词最早是在数据库中进行应用,讲的用户定义的一个数据库操作序列,这些操作要么全做要么全不做,是一个不可分割的工作单位。

  事务的管理是指一个事务的开启,内容添加,提交和回滚.

代码层次的事务控制

  事务控制原本是在数据库进行的,但由于ORM映射后,操作数据库的语句未必是SQL语句,事务控制也被迁移到了工程语言上(Java/C++/Python).Spring framework支持了事务管理的机制,通过ORM映射后可以在业务代码中实现事务控制.

  

事务控制形式

  编程式事务控制

    自己手动控制事务,jdbc和Hibernate提供这种事务管理方式.

    conn.setAutoCommit(false);  //jdbc设置手动控制事务

    session.beginTransaction();  //Hibernate开始一个事务

    这种方式可以控制到代码细节,灵活多变.

  声明式事务控制

    以方法为单位,可以在类的方法中织入事务控制的代码,从而解除了事务代码与业务代码的耦合.

    这种方式需要在配置文件中配置,虽然无法控制到代码细节(无法在某个方法中的某几行加入事务控制),但一般情况适用.

    

Spring事务控制

  利用AOP技术实现声明式事务控制,包括XML方式和注解方式.

  xml方式    

  1.导入jar包

  2.加入ioc,aop,apectj,tx等约束,并配置数据源,声明式事务,aop

<?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.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">

    <!-- 1. 数据源对象: C3P0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxStatements" value="100"></property>
        <property name="acquireIncrement" value="2"></property>
    </bean>

    <!-- 2. JdbcTemplate工具类实例 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 3. dao实例 -->
    <bean id="deptDao" class="com.harry.transaction.dao.DeptDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <!-- 4. service实例 -->
    <bean id="deptService" class="com.harry.transaction.service.DeptService">
        <property name="deptDao" ref="deptDao"></property>
    </bean>

    <!-- #############5. Spring声明式事务管理配置############### -->
    <!-- 5.1 配置事务管理器类 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 5.2 配置事务增强(如果管理事务?) -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <!-- 5.3 Aop配置: 拦截哪些方法(切入点表表达式) + 应用上面的事务增强配置 -->
    <aop:config>
        <aop:pointcut expression="execution(* ccm.harry.transaction.service.save*(..))" id="pt"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>

</beans>

applicationContext.xml

  

  注解方式

  1.导入jar包

  2.xml数据源配置,事务管理类配置,开启ioc,aop,事务注解扫描.

<?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.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">

    <!-- 1. 数据源对象: C3P0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxStatements" value="100"></property>
        <property name="acquireIncrement" value="2"></property>
    </bean>

    <!-- 2. JdbcTemplate工具类实例 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 事务管理器类 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.harry.transaction"></context:component-scan>

    <!-- 注解方式实现事务: 指定注解方式实现事务 -->
    <tx:annotation-driven transaction-manager="txManager"/>

</beans>      

applicationContext.xml

  3.在Dao中的类或者类方法上使用@Transactional来表明事务增强的切入点  

  

  事务属性

@Transactional(
            readOnly = false,  // 读写事务
            timeout = -1,       // 事务的超时时间不限制
            noRollbackFor = ArithmeticException.class,  // 遇到数学异常不回滚
            isolation = Isolation.DEFAULT,              // 事务的隔离级别,数据库的默认
            propagation = Propagation.REQUIRED            // 事务的传播行为
    )
    public void save(Dept dept){
        deptDao.save(dept);
        int i = 1/0;
        deptDao.save(dept);
    }

txProperty.java

  上述属性除了事务传播行为,其他都容易理解.重点需要理解事务的传播行为.

    Propagation.REQUIRED

      指定当前的方法必须在事务的环境下执行;

      如果当前运行的方法,已经存在事务, 就会加入当前的事务;

    Propagation.REQUIRED_NEW

      指定当前的方法必须在事务的环境下执行;

      如果当前运行的方法,已经存在事务:  事务会挂起; 会始终开启一个新的事务,执行完后;  刚才挂起的事务才继续运行。  

/***************************回滚,日志不会写入************************/
Class Log1{
        Propagation.REQUIRED  
        insertLog();
}

    Propagation.REQUIRED
    Void  saveDept(){
        insertLog();    // 加入当前事务
        .. 异常, 会回滚
        saveDept();
    }
/****************回滚,日志会开启新事务并进行写入****************/
Class Log2{
        Propagation.REQUIRED_NEW  
        insertLog();
}

    Propagation.REQUIRED
    Void  saveDept(){
        insertLog();    // 始终开启事务
        .. 异常, 日志不会回滚
        saveDept();
    }

txPropagation.java

  

    

框架应用:Spring framework (四) - 事务管理的更多相关文章

  1. Spring Framework之事务管理

    目录 问题 数据库事务 事务的定义 事务的目的 事务的特性 事务隔离级别 数据并发问题 事务隔离级别对数据并发问题的作用 快照读 Spring事务管理 事务管理接口 TransactionDefini ...

  2. Spring编程式事务管理及声明式事务管理

    本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...

  3. Spring中的事务管理

    事务简介: 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和一致性 事务就是一系列的动作,它们被当作一个单独的工作单元.这些动作要么全部完成,要么全部不起作用 事务的四个关键属性( ...

  4. Spring中的事务管理详解

    在这里主要介绍Spring对事务管理的一些理论知识,实战方面参考上一篇博文: http://www.cnblogs.com/longshiyVip/p/5061547.html 1. 事务简介: 事务 ...

  5. Spring声明式事务管理与配置介绍

    转至:http://java.9sssd.com/javafw/art/1215 [摘要]本文介绍Spring声明式事务管理与配置,包括Spring声明式事务配置的五种方式.事务的传播属性(Propa ...

  6. SSH框架之Spring第四篇

    1.1 JdbcTemplate概述 : 它是spring框架中提供的一个对象,是对原始JdbcAPI对象的简单封装.spring框架为我们提供了很多的操作模板类. ORM持久化技术 模板类 JDBC ...

  7. Spring详解------事务管理

    目录 1.事务介绍 2.事务的四个特性(ACID) 3.Spring 事务管理的核心接口 4. PlatformTransactionManager  事务管理器 5.TransactionStatu ...

  8. spring 声明式事务管理

    简单理解事务: 比如你去ATM机取5000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉5000元钱:然后ATM出5000元钱.这两个步骤必须是要么都执行要么都不执行.如果银行卡扣除了5000块但 ...

  9. Spring声明式事务管理基于tx/aop命名空间

    目的:通过Spring AOP 实现Spring声明式事务管理; Spring支持编程式事务管理和声明式事务管理两种方式. 而声明式事务管理也有两种常用的方式,一种是基于tx/aop命名空间的xml配 ...

随机推荐

  1. spring学习笔记2---MVC处理器映射(handlerMapping)三种方式(附源码)

    一.根据Beanname访问controller: 在springmmvc-servlet.xml的配置handlermapping中加入beanname,通过该beanname找到对应的contro ...

  2. R语言安装加载包

    问题描述 在国内因为镜像的原因,直接使用:install.packages("plyr")往往无法成功添加安装包 解决办法 使用国内镜像进行安装,添加repo参数,参考如下: in ...

  3. 电源库(Sources)

  4. Jquery笔记之第二天

    Jquery笔记之第二天 jQuery - 获取内容和属性 获得内容 - text().html() 以及 val() <script> $(document).ready(functio ...

  5. 写给后端的前端笔记:定位(position)

    写给后端的前端笔记:定位(position) 既然都写了一篇浮动布局,干脆把定位(position)也写了,这样后端基本上能学会css布局了. 类别 我们所说的定位position主要有三类:固定定位 ...

  6. 【BUG】插入或者更新超过限制后写入数据库失败

      Error Code: 1064 - You have an error in your SQL syntax; check the manual that corresponds to your ...

  7. MIPI DSI转LVDS芯片方案TC358775XBG

    型号:TC358775XBG功能:MIPI转LVDS通信方式:IIC/MIPI Command mode分辨率:1920*1080电源:3.3/1.8/1.2封装形式:BGA64深圳长期现货 ,提供技 ...

  8. selenium 对chrome浏览器操作

    参照http://www.testwo.com/blog/6931博客内容 1.下载ChromeDriver驱动包(下载地址: http://chromedriver.storage.googleap ...

  9. ASP.NET Core 运行原理解剖[5]:Authentication

    在现代应用程序中,认证已不再是简单的将用户凭证保存在浏览器中,而要适应多种场景,如App,WebAPI,第三方登录等等.在 ASP.NET 4.x 时代的Windows认证和Forms认证已无法满足现 ...

  10. [转] Java se 7新特性研究(二)

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp82   今天主要研究Java se 7中异常处理的新功能.从今天开始正在 ...