事务管理方式

1.编码方案 不建议使用,它具有侵入性。在原有的业务代码基础上去添加事务管理代码
2. 声明式事务控制,基于AOP对目标进行代理,添加around环绕通知。
这种方案,它不具有侵入性,不需要修改原来的业务代码

基于xml配置声明式事务管理方案

第一步:在applicationContext.xml文件中添加aop与tx的名称空间

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

第二步:在applicationContext.xml文件中配置
Spring提供的advice是传统的spring advice
1. 声明事务管理器

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

2.配置通知
Spring为我们提供了一个TransactionInterceptor来完成增强

对于这个增强,我们可以使用spring为我们提供的一个标签< tx:advice>来完成操作

<!-- 配置通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- name:必须的,对哪些方法进行事务控制isolation 可选 设置事务隔离级别 默认是DEFAULT propagation:可选 设置事务传播 默认值 REQUIREDtimeout 可选 超时时间 默认值-1 read-only 可选 默认值是false 如果不是只读,它可以对insert update delete操作,如果是只读不可以。rollback-for 可选 可以设置一个异常,如果产生这个异常,触发事务回滚no-rolback-for 可选 可以设置一个异常,如果产生这个异常,不会触发事务回滚--><tx:method name="account" /></tx:attributes></tx:advice>

3.配置切面
因为使用的是传统的spring的advice,需要使用

<!-- 配置切面 --><aop:config><aop:pointcut expression="execution(* cn.nwtxxb.service.IAccountService.account(..))" id="txPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config>

完整的applicationContext.xml配置

<?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: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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 引入外部的properties文件 --><context:property-placeholder location="classpath:db.properties" /><!-- 创建c3p0连接 --><bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!-- service --><bean id="accountService" class="cn.nwtxxb.service.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property></bean><!-- dao --><bean id="accountDao" class="cn.nwtxxb.dao.AccountDAOImpl"><!-- 当注入dataSource后,底层会自动创建一个JdbcTemplate --><property name="dataSource" ref="c3p0DataSource" /></bean><!-- 配置事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="c3p0DataSource"></property></bean><!-- 配置通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- name:必须的,对哪些方法进行事务控制isolation 可选 设置事务隔离级别 默认是DEFAULT propagation:可选 设置事务传播 默认值 REQUIREDtimeout 可选 超时时间 默认值-1 read-only 可选 默认值是false 如果不是只读,它可以对insert update delete操作,如果是只读不可以。rollback-for 可选 可以设置一个异常,如果产生这个异常,触发事务回滚no-rolback-for 可选 可以设置一个异常,如果产生这个异常,不会触发事务回滚--><tx:method name="account" /></tx:attributes></tx:advice><!-- 配置切面 --><aop:config><aop:pointcut expression="execution(* cn.nwtxxb.service.IAccountService.account(..))" id="txPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config><!--开启事务管理注解驱动--><tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

基于annotation声明式事务管理方案

可以使用@Transaction来在类或方法上添加声明式事务管理
注意:需要在applicationContext.xml文件中使用

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

相当于开启注解事务控制

问题:关于xml方式与annotation方式的优缺点?
从简单上来说,使用注解更方便。
使用配置的方案,可以对事务配置进行集中维护。

Spring声明式事务管理相关推荐

  1. Spring声明式事务管理、事务的传播行为xml配置

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 1. <tx:method name="insert*" propagat ...

  2. Spring配置文件详解三:Spring声明式事务管理

    1.声明式事务管理 Spring提供了声明式事务管理,这是通过Spring AOP实现的. 原理:Spring中进行事务管理的通常方式是利用AOP(面向切片编程)的方式,为普通java类封装事务控制, ...

  3. spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)

    1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...

  4. 【Spring学习笔记 九】Spring声明式事务管理实现机制

    什么是事务?事务就是把一系列的动作当成一个独立的工作单元,这些动作要么全部完成,要么全部不起作用,关乎数据准确性的地方我们一定要用到事务,防止业务逻辑出错. 什么是事务管理,事务管理对于企业应用而言至 ...

  5. Spring声明式事务管理的配置详解

    环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add ...

  6. Spring声明式事务管理实现及原理详解

    目录 1.实现步骤 1.1.配置事务管理器 1.2.启动事务注解 1.3.业务添加注解 2.代码演示 2.1.bean文件 2.2.目标类 2.3.测试类 3.Spring事务属性 3.1.传播行为 ...

  7. Spring声明式事务管理中的事务回滚

    一:使用 本文在spring + spring mvc + mybatis中使用 第一步配置xml:注意xml最前面tx名称空间一定要配置 <beans xmlns="http://w ...

  8. 全面分析 Spring 的编程式事务管理及声明式事务管理(转)

    摘要 Spring 的事务管理是 Spring 框架中一个比较重要的知识点,该知识点本身并不复杂,只是由于其比较灵活,导致初学者很难把握.本教程从基础知识开始,详细分析了 Spring 事务管理的使用 ...

  9. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

最新文章

  1. R语言ggplot2可视化:使用长表数据(窄表数据)( Long Data Format)可视化多个时间序列数据、在同一个可视化图像中可视化多个时间序列数据(Multiple Time Series)
  2. Qt5.7 + VS2015 环境搭建
  3. 【高级数据结构】[SPOJ QTREE]树链剖分/动态树各一模板
  4. 盲人可以也做软件工程师,反思一下老哥
  5. AI 到底是怎么「想」的?
  6. 三年Java开发经验,必须要掌握的知识技能树有哪些?
  7. Google 被祭天了!
  8. (转)Linux 系统性能分析工具图解读(一、二)
  9. mysql mysqlhotcopy_mysql中mysqlhotcopy备份数据库总结
  10. 时间的几种格式以及它们之间的相互转换 (js)
  11. 在Ubuntu 18.04上畅玩 Cataclysm: Dark Days Ahead:大灾变!
  12. word文件在线转换成pdf
  13. win7休眠设置在哪里_win7系统休眠功能如何关闭 win7系统休眠功能关闭步骤【图解】...
  14. Java获取时间,将当前时间减一天、一月、一年,并加以格式化
  15. 基于WebGIS的电子政务应用(基于J2EE的MVC架构)
  16. 亚洲众包网站悄然崛起:中国成最大“雇主”
  17. 从实验室研发到大规模生产 纳晶科技量子点技术商用多点开花
  18. MongoDB不同压缩算法的影响
  19. math_排序不等式的推导
  20. 顺丰菜鸟大战 本质是以数据获得企业竞争壁垒

热门文章

  1. 33万字!深度学习笔记在线版发布!
  2. AI基础:Pandas简易入门
  3. 重磅!2K图像90FPS,中科院开源轻量级通用人脸检测器
  4. 腾讯多任务模型MFH
  5. Trapper: Transformer模型都在此!
  6. 2020年AI领域有哪些让人惊艳的研究?
  7. 全面助推国产化进程,网易云信获鲲鹏技术认证
  8. 【直播预告】天黑请闭眼,杭州终极狼人大奖赛正在查杀——见证 4 万现金大奖的诞生!...
  9. 玩转可视化--来聊聊地图投影的学问
  10. 理解Fragment生命周期