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

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

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

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. Ajax 上传文件(input file FormData)

    FormData对象用以将数据编译成键值对,以便用XMLHttpRequest来发送数据.其主要用于发送表单数据,但亦可用于发送带键数据(keyed data),而独立于表单使用. jQuery Aj ...

  2. java拦截器(Interceptor)学习笔记

    1,拦截器的概念    java里的拦截器是动态拦截Action调用的对象,它提供了一种机制可以使开发者在一个Action执行的前后执行一段代码,也可以在一个Action执行前阻止其执行,同时也提供了 ...

  3. Flutter - ListView禁止用户上下滑动

    ListView禁止用户上下滑动可以使用physics属性 physics: const NeverScrollableScrollPhysics()

  4. python 函数学习

    print dic.items() #[('a', 'hello'), ('c', 'you'), ('b', 'how')] print dic.iteritems() #<dictionar ...

  5. fastDFS教程Ⅱ-文件服务器迁移

    在实际的项目应用中,由于服务器替换或项目变更难免会存在fastDFS文件服务器迁移的工作.本文重点介绍fastDFS文件系统在不同情况下的文件迁移处理方案. 1.迁移时IP地址不变 通过文件服务器存储 ...

  6. Asp.Net_Mvc3.5语法_<%%>的用法

    一. <%%>这种格式实际上就是和asp的用法一样的,只是asp中里面是vbscript或 者javascript代码,而在asp.net中用的是.net平台下支持的语言.特别 注意:服务 ...

  7. git 报错 error: insufficient permission for adding an object to repository database ./objects

    参照:http://stackoverflow.com/questions/1918524/error-pushing-to-github-insufficient-permission-for-ad ...

  8. 利用可道云kodexplorer在树莓派raspbian上搭建私有云网盘

    可道云kodexplorer是一款开源私有云系统,类似于owncloud,Dropbox.SkyDrive,seafile等.将可道云kodexplorer搭建在树莓派上,从而在树莓派上存储.管理家庭 ...

  9. PAT甲题题解-1012. The Best Rank (25)-排序水题

    排序,水题因为最后如果一个学生最好的排名有一样的,输出的课程有个优先级A>C>M>E那么按这个优先级顺序进行排序每次排序前先求当前课程的排名然后再与目前最好的排名比较.更新 至于查询 ...

  10. Python中元组,列表,字典的区别

    http://blog.csdn.net/yasi_xi/article/details/38384047