在开发过程中springboot提供的常见的事务解决方案是使用注解方式实现。

使用注解

在启动类上添加注解

@EnableTransactionManagement

在需要事务控制的方法添加@Transactional注解

这种方式问题是,我们需要在方法上添加注解,这样处理起来特别麻烦。

我们可以使用XML方式配置,配置好后,方法不需要加注解,这样我们使用起来就不需要再管注解的事情。

1.添加transaction.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: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"> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="query*" propagation="SUPPORTS" read-only="true"></tx:method>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"></tx:method>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"></tx:method>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"></tx:method>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution (* com.neo.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" order="0"/>
</aop:config> <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

这样我们只需只要在编写事务代码的时候遵循上面的规则,编写方法名称,就可以对事务进行拦截。

2.在启动类上引入此配置文件。

@SpringBootApplication
@ImportResource("classpath:transaction.xml")
@MapperScan({"com.neo.dao"})
public class DemoApplication {

这样springboot 就可以支持事务管理了。

3.测试事务是否生效

public void create(SaleOrder order){
orderDao.create(order);
throw new RuntimeException("出错了") ;
}

编写代码如下,在添加后抛出异常,发现数据并没有真正的插入。

注意事项:

使用事务需要引入:

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>

打印事务日志:

logging:
level:
com.neo.dao: debug
org.springframework.jdbc: debug

日志执行情况:

2018-10-16 23:17:17.702 DEBUG 9640 --- [nio-8000-exec-1] o.s.j.d.DataSourceTransactionManager     : Creating new transaction with name [com.neo.service.SaleOrderService.create]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,-Exception
2018-10-16 23:17:17.708 DEBUG 9640 --- [nio-8000-exec-1] o.s.j.d.DataSourceTransactionManager : Acquired Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@7f9c9817] for JDBC transaction
2018-10-16 23:17:17.713 DEBUG 9640 --- [nio-8000-exec-1] o.s.j.d.DataSourceTransactionManager : Switching JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@7f9c9817] to manual commit
2018-10-16 23:17:17.754 DEBUG 9640 --- [nio-8000-exec-1] com.neo.dao.SaleOrderDao.create : ==> Preparing: INSERT INTO SALE_ORDER (ID_,NAME_,TOTAL_,CREATOR_,CREATE_TIME_) VALUES (?, ?, ?, ?, ?)
2018-10-16 23:17:17.782 DEBUG 9640 --- [nio-8000-exec-1] com.neo.dao.SaleOrderDao.create : ==> Parameters: 1539703037695(String), zyg(String), 33.0(Double), AA(String), null
2018-10-16 23:17:17.784 DEBUG 9640 --- [nio-8000-exec-1] com.neo.dao.SaleOrderDao.create : <== Updates: 1
2018-10-16 23:17:17.785 DEBUG 9640 --- [nio-8000-exec-1] o.s.j.d.DataSourceTransactionManager : Initiating transaction rollback
2018-10-16 23:17:17.785 DEBUG 9640 --- [nio-8000-exec-1] o.s.j.d.DataSourceTransactionManager : Rolling back JDBC transaction on Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@7f9c9817]
2018-10-16 23:17:17.786 DEBUG 9640 --- [nio-8000-exec-1] o.s.j.d.DataSourceTransactionManager : Releasing JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@7f9c9817] after transaction
2018-10-16 23:17:17.787 DEBUG 9640 --- [nio-8000-exec-1] o.s.jdbc.datasource.DataSourceUtils : Returning JDBC Connection to DataSource
2018-10-16 23:17:17.797 ERROR 9640 --- [nio-8000-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/demo] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: 出错了] with root cause

springboot xml声明式事务管理方案的更多相关文章

  1. Spring 声明式事务管理方式

    声明式事务管理,基于AOP对目标代理,添加环绕通知,比编码方案优势,不具有侵入式,不需要修改原来的代码. 1.基于XML配置的声明式事务管理方案(案例)      接口Service public i ...

  2. XML方式实现Spring声明式事务管理

    1.首先编写一个实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() ...

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

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

  4. Spring声明式事务管理(基于XML方式实现)

    --------------------siwuxie095                             Spring 声明式事务管理(基于 XML 方式实现)         以转账为例 ...

  5. 【Spring】Spring的事务管理 - 2、声明式事务管理(实现基于XML、Annotation的方式。)

    声明式事务管理 文章目录 声明式事务管理 基于XML方式的声明式事务 基于Annotation方式的声明式事务 简单记录 - 简单记录-Java EE企业级应用开发教程(Spring+Spring M ...

  6. 零基础学习java------39---------json格式交互,Restful(不懂),静态资源映射,SSM整合(ssm整合思想,application.xml文件详解(声明式事务管理),)

    一. json格式交互(知道) 1 . 回顾ajax基本语法 $.ajax({ url:"", // 请求的后台路径 data:{"":"" ...

  7. Spring—SSJ集成&声明式事务管理

    1.   课程介绍 1.  SSJ集成;(掌握) 2.  声明式事务管理;(掌握) 什么是三大框架 2.1.  ssh Struts/Struts2 Spring Hibernate 2.2.  ss ...

  8. spring声明式事务管理总结

    事务配置 首先在/WEB-INF/applicationContext.xml添加以下内容: <!-- 配置事务管理器 --> <bean id="transactionM ...

  9. Spring声明式事务管理

    一.Spring 的声明式事务管理概述 1.Spring 的声明式事务管理在底层是建立在 AOP 的基础之上的.其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法 ...

随机推荐

  1. Vue Baidu Map 插件的使用

    最近在做一个项目,技术采用的是Vue.js套餐,有个百度地图的需求,当时,大脑宕机,立马去引入百度地图API,当时想到两种方法,一种是在index.html中全局引入js,此法吾不喜,就采用了第二种异 ...

  2. f5 irules

    1.插入XFF when HTTP_REQUEST { if { [HTTP::header exists X-Forward-For] } { set old_xff [HTTP::header v ...

  3. linux下svn版本控制的常用命令大全

    1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/pro/domain 简写:s ...

  4. how2j网站前端项目——天猫前端(第一次)学习笔记6

    开始我的订单页面 学着学着,会觉得我这是在干啥呢?我要学的是Java不是吗?怎么要学这么久的前端啊?说实话,我很迷茫,不知道以后的工作具体是做什么?学的这些能用到吗? 不过,还是要把这个项目跟着走完! ...

  5. jquery字符串相等判断

    在jquery中字符串相等判断一直失败 原来是空格! string1, string2 若其中有一个为返回值或类似 $.trim(string1) == $.trim(string2) ------- ...

  6. 织梦文章里面的图片alt和title属性,用文章标题自动替换

    把{dede:field.body/}改成{dede:field.body runphp=yes}global $dsql,$id,$aid;$myid = isset($id) ? $id : $a ...

  7. C# 使用printDocument1.Print打印时不显示 正在打印对话框

    C#使用printDocument1.Print打印时不显示正在打印对话框有两种方法 第一种,使用PrintController       PrintController printControll ...

  8. IDEA 中javadoc插件不能设置的问题

    解决方案 1.手动下载插件 https://github.com/ranzou06/intellij-javadocs/blob/master/intellij-javadocs.zip?raw=tr ...

  9. (转)JavaScript escape() 函数(该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . / 。其他所有的字符都会被转义序列替换。)

    JavaScript escape() 函数 JavaScript 全局对象参考手册 定义和用法 escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串. 语法 escape ...

  10. PAT 1037 在霍格沃茨找零钱(20)(代码+思路)

    1037 在霍格沃茨找零钱(20)(20 分) 如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 -- 就如海格告诉哈利的:"十七个银西可(Sickle)兑一个加隆(Galleon) ...