事务管理: 管理事务,管理数据,数据完整性和一致性

事务[业务逻辑] : 由一系列的动作[查询书价格,更新库存,更新余额],组成一个单元[买书业务],

当我们动作当中有一个错了,全错~

ACID

原子性 隔离性 一致性 持久性

注解方式配置事务[编程方式-->@代码]

1.Spring框架当中需要配置事务管理器--> JDBC[Mybatis] Hibernate JTA-->数据源

2.启动事务注解[特意说了,事务管理器的id]

3.事务注解--> 可以放置的位置:@Transaction 类或者方法上

  1. 类上放置注解 方法当中注解[reaonly=true]
  2. rollbakFor
  3. 传播性 7个 默认值

XML方式配置事务

<!--

声明方式XML方式:完成事务的配置,需要使用到地方有AOP

-->

<!-- 配置事务管理器 -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>

<!-- 定义哪些方法需要被事务管理器进行管理 -->

<tx:advice id="serviceMethodAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="*" read-only="true"/>

</tx:attributes>

</tx:advice>

<!-- 需要哪些方法是被监控,并且是有事务管理 -->

<aop:config>

<aop:pointcut expression="execution(* com..service.*Service.*(..))" id="servicePointCut"/>

<!-- 代表的意思: service包下的说有类以Service结尾的类下的所有方法,都为只读状态 -->

<aop:advisor advice-ref="serviceMethodAdvice" pointcut-ref="servicePointCut"/>

</aop:config>

单元测试,推荐使用断言方式,需要再使用syso的输出方式

<!-- 定义哪些方法需要被事务管理器进行管理 -->

<tx:advice id="serviceMethodAdvice" transaction-manager="transactionManager">

<tx:attributes>

<!-- 约定大于配置 -->

<!-- 第一种配置方式

<tx:method name="*" read-only="true"/>

<tx:method name="add*" propagation="REQUIRED"/>

<tx:method name="insert*"/>

<tx:method name="create*"/>

<tx:method name="update*"/>

<tx:method name="edit*"/>

<tx:method name="mod*"/>

<tx:method name="change*"/>

<tx:method name="del*"/>

<tx:method name="remove*"/>

<tx:method name="cancel*"/>

-->

<!-- 第二种配置方式 -->

<tx:method name="*" read-only="false" />

<tx:method name="get*" read-only="true"/>

<tx:method name="load*" read-only="true"/>

<tx:method name="list*" read-only="true"/>

<tx:method name="find*" read-only="true"/>

<tx:method name="sel*" read-only="true"/>

<tx:method name="query*" read-only="true"/>

</tx:attributes>

</tx:advice>

在这里胖哥想说的,帮助文档一定要看,有一些事情我强调了很多很多遍,你们不走心,我也无能为力!

Again in keeping with Spring's philosophy, the TransactionException that can be thrown by any of the PlatformTransactionManager interface's methods is unchecked (that is, it extends the java.lang.RuntimeException class). Transaction infrastructure failures are almost invariably fatal. In rare cases where application code can actually recover from a transaction failure, the application developer can still choose to catch and handle TransactionException. The salient point is that developers are not forced to do so.

The getTransaction(..) method returns a TransactionStatus object, depending on a TransactionDefinition parameter. The returned TransactionStatus might represent a new transaction, or can represent an existing transaction if a matching transaction exists in the current call stack. The implication in this latter case is that, as with Java EE transaction contexts, a TransactionStatus is associated with a thread of execution.

The TransactionDefinition interface specifies:

  • Isolation: The degree to which this transaction is isolated from the work of other transactions. For example, can this transaction see uncommitted writes from other transactions?
  • Propagation: Typically, all code executed within a transaction scope will run in that transaction. However, you have the option of specifying the behavior in the event that a transactional method is executed when a transaction context already exists. For example, code can continue running in the existing transaction (the common case); or the existing transaction can be suspended and a new transaction created. Spring offers all of the transaction propagation options familiar from EJB CMT. To read about the semantics of transaction propagation in Spring, see Section 16.5.7, "Transaction propagation".
  • Timeout: How long this transaction runs before timing out and being rolled back automatically by the underlying transaction infrastructure.
  • Read-only status: A read-only transaction can be used when your code reads but does not modify data. Read-only transactions can be a useful optimization in some cases, such as when you are using Hibernate.

重点: 绝对是个人的建议

RuntimeException默认是不受审查,也是rollBackFor的默认值,如果你再Service层或者Dao层对其进行捕获的话,那么一定要做处理

个人的建议为: service和Dao不管遇到什么请求你都处理往外抛, 处理都放置在Controller 关于异常尽量都是用继承RuntimeException,根据你的代码情况进行不同异常的封装

今天的笔记做的不好! 累了!~~~ 付出的越多!~~~ 有的时候失望越多!~~~ 该何去何从!~~~~

第六节 事务XML方式[声明方式]的更多相关文章

  1. Spring中事务的XML方式[声明方式]

    事务管理: 管理事务,管理数据,数据完整性和一致性 事务[业务逻辑] : 由一系列的动作[查询书价格,更新库存,更新余额],组成一个单元[买书业务], 当我们动作当中有一个错了,全错~ ACID 原子 ...

  2. ES6第二节:新的声明方式

    通过上一节的环境搭建完成,接下来我们就可以愉快的探索ES6的新世界了!下面我们从新的声明方式开始: 在ES6里新加了两种声明方式:let 和 const,以前我们都是用var去作声明,接下来我们一一比 ...

  3. 【系统学习ES6】第一节:新的声明方式

    [系统学习ES6] 本专题旨在对ES6的常用技术点进行系统性梳理,帮助大家对其有更好的掌握.计划每周更新1-2篇,希望大家有所收获. 以前用ES5时,声明变量只能用var.ES6的出现,为我们带来了两 ...

  4. 第六节:WebApi的部署方式(自托管)

    一. 简单说明 开篇就介绍过WebApi和MVC相比,其中优势之一就是WebApi可以不依赖于IIS部署,可以自托管,当然这里指的是 .Net FrameWork 下的 WebApi 和 MVC 相比 ...

  5. ASP.NET MVC深入浅出系列(持续更新) ORM系列之Entity FrameWork详解(持续更新) 第十六节:语法总结(3)(C#6.0和C#7.0新语法) 第三节:深度剖析各类数据结构(Array、List、Queue、Stack)及线程安全问题和yeild关键字 各种通讯连接方式 设计模式篇 第十二节: 总结Quartz.Net几种部署模式(IIS、Exe、服务部署【借

    ASP.NET MVC深入浅出系列(持续更新)   一. ASP.NET体系 从事.Net开发以来,最先接触的Web开发框架是Asp.Net WebForm,该框架高度封装,为了隐藏Http的无状态模 ...

  6. android解析xml文件的方式

    android解析xml文件的方式   作者:东子哥 ,发布于2012-11-26,来源:博客园   在androd手机中处理xml数据时很常见的事情,通常在不同平台传输数据的时候,我们就可能使用xm ...

  7. 第一百二十六节,JavaScript,XPath操作xml节点

    第一百二十六节,JavaScript,XPath操作xml节点 学习要点: 1.IE中的XPath 2.W3C中的XPath 3.XPath跨浏览器兼容 XPath是一种节点查找手段,对比之前使用标准 ...

  8. spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)

    **这个整合.仅仅是最主要的整合,而且是xml配置文件的方式之中的一个,即当中的mybatis是採用非mapper接口的方式.(第二遍採用mapper接口方式.第三遍採用注解的方式:第四篇採用注解基于 ...

  9. MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息

    MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...

随机推荐

  1. 搭建django虚拟环境完整步骤

    一.建立虚拟环境 pip install virtualenv 要使用Django,首先要建立一个虚拟工作环境.我们先为项目建立一个文件夹learn,在文件夹中打开命令行(shift+右击),来建立另 ...

  2. C# Language Specification 5.0 (翻译)第四章 类型

    C# 语言的类型分为两大类:值类型(value type)和引用类型(reference type),而它们又都同时具有至少一个类型形参的泛型类型(generic type).类型形参(type pa ...

  3. tree的使用,显示行号,find命令应用

    第1章 linux启动过程 1.开机自检bios 2.mbr引导 3.GRUB 菜单:选择不同的内核 4.加载内核 5.运行init进程 6.读取/etc/inittab运行级别配置文件 7.执行 / ...

  4. 树莓派3b安装Nginx和php7和百度语音合成模块

    1.安装sox系统mp3音频播放模块(项目需要) sudo apt-get install lame sudo apt-get install sox sudo apt-get install lib ...

  5. Azkaban集群部署

    一.部署模式 solo-server模式  (使用内置h2存储元数据): two-server模式 (1个webServer,1个execServer在同一服务器上,使用mysql存储元数据): mu ...

  6. spring boot 2.0 源码分析(二)

    在上一章学习了spring boot 2.0启动的大概流程以后,今天我们来深挖一下SpringApplication实例变量的run函数. 先把这段run函数的代码贴出来: /** * Run the ...

  7. 图-图的表示、搜索算法及其Java实现

    1.图的表示方法 图:G=(V,E),V代表节点,E代表边. 图有两种表示方法:邻接链表和邻接矩阵 邻接链表因为在表示稀疏图(边的条数|E|远远小于|V|²的图)时非常紧凑而成为通常的选择. 如果需要 ...

  8. B1015 德才论 (25 分)

    19/25 #include<bits/stdc++.h> using namespace std; /* 1.de>=H && cai>=H 2.de> ...

  9. Fragment 使用总结

    1. 要深刻理解Fragment 的生命周期 2. Fragment.getActivity()并不能保证非空. 3.如果在Fragment中有异步的回调, 特别要注意此时Fragment 是否还at ...

  10. BugPhobia准备篇章:Scrum Meeting工作分析篇

    特别说明:此博客不计入正式开发过程的Scrum Meeting篇章,只是工作的基础分析 前端 王鹿鸣.钱林琛撰写初稿 能否前端完成一个页面后就能在本地跑起来进行测试? 能否在前端和后端完成对接后单页面 ...