当前位置: 首页 > news >正文

网站建设工作室北京小俊哥python编写简单网页

网站建设工作室北京小俊哥,python编写简单网页,怎么注册免费网站,微信公众平台注册时间怎么看参考资料 导致 Spring 事务失效常见的几种情况SpringBoot2异常处理回滚事务详解(自动回滚/手动回滚/部分回滚#xff09;Spring#xff0c;为内部方法新起一个事务#xff0c;此处应有坑。PlatformTransactionManagerSpring 事务管理及失效总结我认真总结并分析了 Spring 事…参考资料 导致 Spring 事务失效常见的几种情况SpringBoot2异常处理回滚事务详解(自动回滚/手动回滚/部分回滚Spring为内部方法新起一个事务此处应有坑。PlatformTransactionManagerSpring 事务管理及失效总结我认真总结并分析了 Spring 事务失效的十种常见场景SpringBoot AOP配置全局事务 目录 一. 需求二. 前期准备2.1 Mapper2.2 自定义异常2.3 前台2.4 Controller层 三. 错误的使用方式四. 正确的使用方式4.1 方式1-抽取新类(声明式事务)4.2 方式2-编程式事务,事务管理器4.3 方式3-编程式事务,设置回滚点 五. 事务失效的几种情况 一. 需求 最近在项目中遇到了一个需求需要读取csv的数据到数据库的临时表 然后调用存储过程将临时表中的数据插入正式的数据库中。 ⏹在将csv数据插入临时表之前需要将该表中的数据清空如果在插入临时表时出现了问题同样需要将表中的数据清空。 ⏹需要对前台传入的数据进行业务校验如果校验失败需要抛出自定义异常同时临时表中的数据不能回滚需要被清空。 ⏹在调用存储过程时如果发生异常则事务回滚。 需要注意的是只回滚存储过程所涉及的表临时表中被删除的数据并不参与回滚直接删除。 二. 前期准备 2.1 Mapper import java.util.Map;public interface TestMapper02 {void deleteAllTempWork();void insertData(MapString, Object dataMap); }?xml version1.0 encodingUTF-8? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.example.demo.mapper.TestMapper02delete iddeleteAllTempWorkDELETE FROMtemp_work/deleteinsert idinsertData parameterTypemapINSERT INTO day16.user(id, username, birthday, email) VALUES (#{id}, #{username}, #{birthday}, #{email})/insert/mapper2.2 自定义异常 public class ValidationException extends RuntimeException {private static final long serialVersionUID 1L; }2.3 前台 !DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.org headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0title事务测试/title /head bodybutton idbtn1点击发送请求/button /body script th:src{/js/public/jquery-3.6.0.min.js}/script script th:inlinejavascript$(#btn1).click(function() {const data {from: 13,to: 14}$.ajax({url: /test02/transactional,type: POST,data: JSON.stringify(data),contentType: application/json;charsetutf-8,success: function (data, status, xhr) {console.log(data);}});}); /script /html2.4 Controller层 import java.util.Map; import javax.annotation.Resource;import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;Controller RequestMapping(/test02) public class Test02Controller {Resourceprivate Test02Service service;GetMapping(/init)public ModelAndView init() {ModelAndView modelAndView new ModelAndView();modelAndView.setViewName(test02);return modelAndView;}PostMapping(/transactional)public ResponseEntityVoid transactional(RequestBody MapString, Integer data) throws Exception {// 测试事务service.transactional1(data);// service.transactional2(data);// service.transactional3(data);// 无响应给前台return ResponseEntity.noContent().build();} }三. 错误的使用方式 transactional1主方法调用同一个类中的insertData子方法并且给子方法添加了Transactional注解。出错的原因 在应用系统调用声明 Transactional的目标方法时Spring Framework 默认使用 AOP 代理在代码运行时生成一个代理对象再由这个代理对象来统一管理。Spring 事务是使用 AOP 环绕通知和异常通知就是对方法进行拦截在方法执行前开启事务在捕获到异常时进行事务回滚在方法执行完成后提交事务。在 Spring 的 AOP 代理下只有目标方法由外部调用目标方法才由 Spring 生成的代理对象来管理。若同一类中的其他没有Transactional 注解的方法内部调用有Transactional 注解的方法有Transactional 注解的方法的事务被忽略不会发生回滚。spring是通过aop的方式对需要spring管理事务的bean生成了代理对象然后通过代理对象拦截了目标方法的执行在方法前后添加了事务的功能所以必须通过代理对象调用目标方法的时候事务才会起效。 import java.util.Date; import java.util.HashMap; import java.util.Map;import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.example.demo.mapper.TestMapper02;Service public class Test02Service {// 准备要向数据库插入的数据private final static MapString, Object dataMap new HashMap() {private static final long serialVersionUID 1L;{put(id, 99);put(username, test99);put(birthday, new Date());put(email, test99test.com);}};Resourceprivate TestMapper02 mapper02;// 存在事务的方法public void transactional1(MapString, Integer data) throws Exception {// 删除临时表中的所有数据mapper02.deleteAllTempWork();// 获取from和to的值Integer fromValue data.get(from);Integer toValue data.get(to);if (fromValue toValue) {// 抛出异常throw new Exception();}this.insertData(dataMap);}/*insertData方法被transactional1方法调用*/Transactional(rollbackFor Exception.class)public void insertData(MapString, Object dataMap) {mapper02.insertData(dataMap);// 模拟出现异常int reslt 1 / 0;} }四. 正确的使用方式 4.1 方式1-抽取新类(声明式事务) ⏹创建一个新类将需要被事务管理的部分放到此类中 import java.util.Map; import javax.annotation.Resource;import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;Service public class Test02SubService {Resourceprivate TestMapper02 mapper02;Transactional(rollbackFor Exception.class)public void insertData(MapString, Object dataMap) throws Exception {mapper02.insertData(dataMap);// 模拟运行时异常int reslt 1 / 0;} }⏹我们的主Service调用子Service子Service由Spring来管理会生成事务对象。 import java.util.Date; import java.util.HashMap; import java.util.Map;import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.example.demo.mapper.TestMapper02;Service public class Test02Service {// 准备要向数据库插入的数据private final static MapString, Object dataMap new HashMap() {private static final long serialVersionUID 1L;{put(id, 99);put(username, test99);put(birthday, new Date());put(email, test99test.com);}};Resourceprivate Test02SubService subService;Resourceprivate TestMapper02 mapper02;// 存在事务的方法public void transactional1(MapString, Integer data) throws Exception {// 删除临时表中的所有数据mapper02.deleteAllTempWork();// 获取from和to的值Integer fromValue data.get(from);Integer toValue data.get(to);if (fromValue toValue) {// 抛出异常throw new Exception();}// 如果调用时发生异常,只会回滚insertData方法中的内容subService.insertData(dataMap);} }4.2 方式2-编程式事务,事务管理器 package com.example.demo.service;import java.util.Date; import java.util.HashMap; import java.util.Map;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Service; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionStatus;Service public class Test02Service {private final static MapString, Object dataMap new HashMap() {private static final long serialVersionUID 1L;{put(id, 99);put(username, test99);put(birthday, new Date());put(email, test99test.com);}};Resourceprivate TestMapper02 mapper02;// 事务管理器Autowiredprivate DataSourceTransactionManager dataSourceTransactionManager;// 事务定义对象Autowiredprivate TransactionDefinition transactionDefinition;public void transactional2(MapString, Integer data) throws Exception {// 删除临时表,此部分不需要被事务管理mapper02.deleteAllTempWork();// 手动开启事务TransactionStatus transactionStatus dataSourceTransactionManager.getTransaction(transactionDefinition);try {mapper02.insertData(dataMap);// 模拟异常int a 1 / 0;} catch (Exception e) {// 手动回滚事务dataSourceTransactionManager.rollback(transactionStatus);// 抛出异常,防止程序继续执行throw new Exception();}// 若没有问题则,则提交事务dataSourceTransactionManager.commit(transactionStatus);} }4.3 方式3-编程式事务,设置回滚点 通过TransactionAspectSupport设置回滚点rollbackFor Exception.class指定该异常需要回滚noRollbackFor ValidationException.class指定该异常不需要回滚 import java.util.Date; import java.util.HashMap; import java.util.Map;import javax.annotation.Resource;import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.interceptor.TransactionAspectSupport;Service public class Test02Service {private final static MapString, Object dataMap new HashMap() {private static final long serialVersionUID 1L;{put(id, 99);put(username, test99);put(birthday, new Date());put(email, test99test.com);}};Resourceprivate TestMapper02 mapper02;// 指定抛出Exception异常时回滚;指定抛出ValidationException异常时不回滚Transactional(rollbackFor Exception.class, noRollbackFor ValidationException.class)public void transactional3(MapString, Integer data) throws Exception {// 如果出现了任何异常就进行捕获,然后就抛出自定义的ValidationException异常// 我们在声明式事务中指定了ValidationException异常不进行回滚,// 因此就算此处的try catch块中的异常被抛出,此处的事务也不会进行回滚try {mapper02.deleteAllTempWork();} catch (Exception e) {throw new ValidationException();}// 设置事务的回滚点Object savePoint TransactionAspectSupport.currentTransactionStatus().createSavepoint();try {mapper02.insertData(dataMap);// 模拟运行时异常int a 1 / 0;} catch (Exception e) {// 当发生异常的时候,将事务回滚到预设的回滚点处TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);}} }五. 事务失效的几种情况 EnableTransactionManagement注解未启用方法不是public类型的 Transaction 可以用在类上、接口上、public方法上如果将Trasaction用在了非public方法上事务将无效。 三. 错误的使用方式所示的自调用情况 spring是通过aop的方式对需要spring管理事务的bean生成了代理对象然后通过代理对象拦截了目标方法的执行在方法前后添加了事务的功能所以必须通过代理对象调用目标方法的时候事务才会起效。 抛出的异常被捕获 当业务方法抛出异常spring感知到异常的时候才会做事务回滚的操作若方法内部将异常给吞了那么事务无法感知到异常了事务就不会回滚了。 数据库非InnoDB引擎。 只有InnoDB引擎才支持事务而MyISAM引擎是不支持事务的
http://www.zqtcl.cn/news/159152/

相关文章:

  • 网站推广昔年下拉博客推广链接制作软件
  • php 小企业网站 cmswordpress导航分类
  • 婚恋网站女孩子都是做美容免费空间最大的网盘
  • 建立网站要钱吗找人做网站需求怎么写
  • 网站建设精品课程电商运营主要负责什么
  • 中职网站建设与维护考试题wordpress商店会员管理
  • 物流网站开发策划做提升自己的网站
  • 网站开发交接做网站首页尺寸大小
  • 临沂建网站公司一个工厂做网站有用吗
  • 网站建设代码编译的问题及解决方案天元建设集团有限公司第六分公司
  • 做亚马逊网站费用深圳好蜘蛛网站建设公司
  • 做网站需要办什么手续html简单网页代码实例
  • 中文网页设计模板免费下载超级优化小说
  • 做网站的流程前端做什么网站建设与管理专业学什么
  • 用wordpress做购物网站西安建设工程网站
  • 响应式网站免费模板下载电商怎么做如何从零开始视频
  • 江西网站开发学校联系我们网站制作
  • 做网站首页图片素材营销网站制作要素
  • 云阳网站建设百度对 wordpress 排名
  • 做电商网站需要多少时间网站建设答辩ppt
  • 营销型网站的案例江苏seo网站排名优化
  • 企业网站 备案 网站名称凡科做视频网站
  • 湘潭建设公司网站杭州网站优化
  • 工信部备案网站网站空间服务商
  • 深圳市企业网站seo营销工具桂林百姓网
  • 网站建设所需材料wordpress nginx配置文件
  • 给企业做网站运营广州制作网站公司
  • 一个网站可以有几个关键词网页游戏制作过程
  • 网站可视化后台桥西区网站建设
  • 个人怎么建设网站北京朝阳区最好的小区