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

企业3合1网站建设公司网络营销顾问招聘

企业3合1网站建设公司,网络营销顾问招聘,做一个app软件的步骤,网站如何在推广文章目录一、编程式事务二、声明式事务(常用)三、事务实战详解3.1)事务的回滚机制3.2)事务的传播3.3)事务超时时间3.4)事务隔离级别3.5)事务回滚条件Spring中对事务有两种支持方式,分…

文章目录

    • 一、编程式事务
    • 二、声明式事务(常用)
    • 三、事务实战详解
      • 3.1)事务的回滚机制
      • 3.2)事务的传播
      • 3.3)事务超时时间
      • 3.4)事务隔离级别
      • 3.5)事务回滚条件

Spring中对事务有两种支持方式,分别是编程式事务与声明式事务:

一、编程式事务

  可通过TransactionManagerTransactionTemplate两大内置事务管理对象来完成:

// Spring内置事务管理器对象
@Autowired
private PlatformTransactionManager transactionManager;/*** 通过编程式事务来控制数据库交互:* 更新菜品价格 -> 自定义transactionManager来控制事务*/
public int updateDishPrice(Map<String, Object> paramMap) {// 1. 首先定义默认的事务属性与隔离级别DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();transactionDefinition.setIsolationLevel(TransactionDefinition.ISOLATION_DEFAULT);// 2. 获取TransactionStatusTransactionStatus status = transactionManager.getTransaction(transactionDefinition);int result = 0;try {result = dishMapper.updateDish(paramMap);// int i = 1 / 0; 模拟报错请求// 提交事务transactionManager.commit(status);} catch (DataAccessException e) {LogUtil.error("DishServices.updateDishPrice", e.getMessage());// 回滚事务transactionManager.rollback(status);}return result;
}
// Spring内置事务模板对象
@Autowired
private TransactionTemplate transactionTemplate;/*** 通过编程式事务来控制数据库交互:* 更新菜品代码 -> 自定义transactionTemplate来控制事务*/
public int updateDishCode(Map<String, Object> paramMap) {// 设置事务隔离级别transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_DEFAULT);return transactionTemplate.execute(status -> {int result = 0;try {result = dishMapper.updateDish(paramMap);// int i = 1 / 0; // 模拟报错请求} catch (DataAccessException e) {LogUtil.error("MySpringBoot.updateDishCode", e.getMessage());status.setRollbackOnly();}return result;});
}

二、声明式事务(常用)

  声明式事务,通过在方法上加@Transactional注解实现,可以在注解中定义一些事务参数例如事务传播行为PROPAGATION、事务超时时间TIMEOUT、事务隔离级别ISOLATION、事务回滚条件ROLLBACKFOR

@Transactional(timeout = 30, isolation = Isolation.DEFAULT, rollbackFor = DataAccessException.class)
public int updateDishStatus(Map<String, Object> paramMap) {int result = dishMapper.updateDish(paramMap);// int i = 1 / 0; // 模拟报错请求return result;
}
PROPAGATION
PROPAGATION_REQUIRED这是事务的默认传播行为, 表示如果当前存在事务则加入该事务, 如果当前没有事务则创建一个新的事务.A->B, A如果有事务则B加入该事务(A影响B), 如果A没有则B会自己创建一个事务(B不影响A)
PROPAGATION_REQUIRES_NEW无论如何都创建一个新的事务, 如果当前存在事务则把当前事务挂起, 且开启的事务与外部事务相互独立, 互不干扰. A->B, A不影响B, 他们是两个独立的事务; B不影响A
PROPAGATION_NESTED如果当前存在事务就在当前事务中执行, 否则执行PROPAGATION_REQUIRED逻辑 A->B, A影响B, B不影响A
PROPAGATION_SUPPORTS如果当前存在事务, 则加入该事务; 如果当前没有事务, 则以非事务的方式继续运行
PROPAGATION_NOT_SUPPORTED以非事务的方式运行, 如果当前存在事务的话则把当前事务挂起
PROPAGATION_MANDATORY如果当前存在事务, 则加入该事务; 如果当前没有事务, 则抛出异常
PROPAGATION_NEVER以非事务方式运行, 如果当前存在事务, 则抛出异常

三、事务实战详解

3.1)事务的回滚机制

// 准备一个接口,一个service
@Transactional(propagation = Propagation.REQUIRED)
public int insertDishDefault() {Map<String, Object> paramMap = new HashMap<>();...int result = dishMapper.insertDishDefault(paramMap);throw new RuntimeException();
}

  查看数据库可以发现,期望的数据并没有被插入,但是切面日志表的数据是插入成功的,并且在进入该事务方法(insertDishDefault)前的其他方法操作也是成功执行的,所以事务的回滚不会影响到在它作用范围之外的sqlSession。

  仔细查看数据库中的数据变化,对比有事务控制与无事务控制的方法,会先发无事务的方法对DB的操作都是实时的(可以打断点查看),但是有事务的方法则是要等到事务提交才会影响DB,这和数据库里面的事务是一样的,Spring的底层也是用了AOP代理来完成事务控制的。

3.2)事务的传播

  Propagation属性默认是REQUIRED,事务的传播机制涉及到多种不同的情况,同时也是面试中的高频考点,下面列举几个常见的场景(下述场景均只讨论默认的事务传播机制):

  • 有事务Controller层 -> 无事务Services层

    @GetMapping("/insert")
    @Transactional(propagation = Propagation.REQUIRED)
    public int insert() {Ticket ticket = new Ticket();ticket.setDeparture("黑龙江");ticket.setDestination("广州");ticketMapper.insert(ticket);return dishServices.insertDishDefault(); // 无事务方法:插入数据,让其抛出异常
    }
    

      结果:Ticket与Dish表均插入数据失败。所以不同类中有事务的方法 -> 无事务的方法,后者会自动加入前者的事务,且它们是同一个事务,如果是前者抛异常,后者也会回滚,此处不演示。

  • 有事务Services层 -> 无事务Services层

    @Transactional(propagation = Propagation.REQUIRED)
    public int insertDishDefault() {Map<String, Object> paramMap = new HashMap<>();...testsw(); // testsw() 是同类的无事务方法:插入数据,让其抛出异常return result;
    }
    

      结果:Ticket与Dish表均插入数据失败。所以同一个类中有事务的方法 -> 无事务的方法,后者会自动加入前者的事务,且它们是同一个事务,如果是前者抛异常,后者也会回滚,此处不演示。

  • 无事务Controller层 -> 有事务Services层

    @GetMapping("/insert")
    public int insert() {Ticket ticket = new Ticket();ticket.setDeparture("黑龙江");ticket.setDestination("广州");ticketMapper.insert(ticket);return dishServices.insertDishDefault(); // 有事务方法:插入数据,让其抛出异常
    }
    

      结果:Ticket插入成功,Dish插入失败。所以不同类中无事务的方法 -> 有事务的方法,后者不会影响前者,也可以通过数据库看出来,当Controller层代码执行完之后,Tick就已经被实时插入了。如果是前者抛出异常,前者不会回滚,后者需要看无事务方法是否已经执行完毕,如果已经执行完毕则不会回滚(事务方法一旦被执行完毕就会提交)。

  • 无事务Services层 -> 有事务Services层(重要)

    // 同一个类中Dish插入无事务的方法调用有事务的方法
    @Transactional(propagation = Propagation.REQUIRED)
    public void testsw() {Ticket ticket = new Ticket();ticket.setDeparture("黑龙江");ticket.setDestination("广州");ticketMapper.insert(ticket);throw new RuntimeException(); // 同类中无事务调用该有有事务,有事务抛出异常
    }
    

      结果:Ticket与Dish均插入成功,Dish能插入成功因为其没有事务控制,实时就插入了,而Tick能插入成功是因为事务没有生效。如果是前者抛出异常,前者不会回滚,后者需要看无事务方法是否已经执行完毕,如果已经执行完毕则不会回滚(事务方法一旦被执行完毕就会提交)。

为何好端端的事务会失效?

1)Spring的事务注解@Transactional只能放在public方法上才起作用;

2)方法用final或static修饰了,用这两个关键字修饰的方法Spring无法对目标方法进行重写,因此自然也就不存在事务了;

3)如果采用spring+spring mvc,则context:component-scan重复扫描问题可能会引起事务失败;

4)数据库引擎不支持,如使用mysql且引擎是MyISAM,则事务会不起作用,原因是MyISAM不支持事务,可以改成InnoDB引擎;

5)本类中的方法互相调用,不会经过Spring的代理类,所以如果在调用方法前没有事务控制的话,调用后也是没有的(Spring事务是通过AOP代理来实现的)。

3.3)事务超时时间

  timeout属性默认是-1,不超时,在实际生产环境中一般设置为30秒、60秒,注意这个时间是所有事务的执行时间综合。

@Transactional(propagation = Propagation.REQUIRED, timeout = 5)
public int insertDishDefault() {Map<String, Object> paramMap = new HashMap<>();...try {Thread.sleep(6000); //让线程暂停6秒,而事务的回滚时间阈值是5秒} catch (InterruptedException e) {throw new RuntimeException(e);}int result = dishMapper.insertDishDefault(paramMap); // 插入失败return result;
}

3.4)事务隔离级别

  isolation属性默认是-1,表示使用数据库的隔离级别,用一个实验来测试隔离性,如下面代码所示,该查询方法可以查看未提交的事务。

@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public String selectDishDefault(String dishId) {Dish dish =  dishMapper.selectDishDefault(dishId);if (dish == null) {return "没有该用户";} else {return JsonUtil.objectToJson(dish);}
}

  在第一行打上断点,然后执行一个sql插入语句不提交,实际证明该方法可以读取到我们没提交的这个sqlSession中的数据,可以用datagrip来手动提交事务:

image-20230309224709390

image-20230309224814007

3.5)事务回滚条件

  rollbackFor属性默认是空,表示捕获所有异常。指定了异常种类后,只有在出现了指定的异常才会回滚。

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = DataAccessException.class)
public int insertDishDefault() {Map<String, Object> paramMap = new HashMap<>();...int result = dishMapper.insertDishDefault(paramMap);try {int i = 1/0; // 会抛出ArithmeticException异常,但不是回滚的异常条件,数据插入成功} catch (ArithmeticException e) {e.printStackTrace();}return result;
}

文章转载自:
http://victorian.sqxr.cn
http://spearman.sqxr.cn
http://skybridge.sqxr.cn
http://kootenai.sqxr.cn
http://knoxville.sqxr.cn
http://wizen.sqxr.cn
http://beaten.sqxr.cn
http://enwheel.sqxr.cn
http://insole.sqxr.cn
http://projector.sqxr.cn
http://burgundy.sqxr.cn
http://extramental.sqxr.cn
http://matriarch.sqxr.cn
http://capacitance.sqxr.cn
http://motility.sqxr.cn
http://lms.sqxr.cn
http://address.sqxr.cn
http://stumble.sqxr.cn
http://condonation.sqxr.cn
http://placet.sqxr.cn
http://potatotrap.sqxr.cn
http://ganoblast.sqxr.cn
http://theurgist.sqxr.cn
http://roumansh.sqxr.cn
http://qpm.sqxr.cn
http://nonfulfilment.sqxr.cn
http://polypetalous.sqxr.cn
http://arenation.sqxr.cn
http://phototroph.sqxr.cn
http://stagy.sqxr.cn
http://abo.sqxr.cn
http://archdove.sqxr.cn
http://scalpriform.sqxr.cn
http://aerosiderolite.sqxr.cn
http://wildcard.sqxr.cn
http://deadwood.sqxr.cn
http://oops.sqxr.cn
http://tagalog.sqxr.cn
http://legendize.sqxr.cn
http://unipartite.sqxr.cn
http://overhand.sqxr.cn
http://perspicuous.sqxr.cn
http://tightwad.sqxr.cn
http://deceased.sqxr.cn
http://rifter.sqxr.cn
http://sierran.sqxr.cn
http://annunciation.sqxr.cn
http://helminthoid.sqxr.cn
http://fungistat.sqxr.cn
http://silanize.sqxr.cn
http://thiophosphate.sqxr.cn
http://pearson.sqxr.cn
http://succorance.sqxr.cn
http://ganglionic.sqxr.cn
http://repine.sqxr.cn
http://neutercane.sqxr.cn
http://atomist.sqxr.cn
http://semifeudal.sqxr.cn
http://invalidation.sqxr.cn
http://bisulphide.sqxr.cn
http://sulfapyrazine.sqxr.cn
http://loincloth.sqxr.cn
http://admit.sqxr.cn
http://manama.sqxr.cn
http://toney.sqxr.cn
http://gibraltar.sqxr.cn
http://tuberculose.sqxr.cn
http://repo.sqxr.cn
http://subminiature.sqxr.cn
http://partaker.sqxr.cn
http://constructionist.sqxr.cn
http://pepsi.sqxr.cn
http://choline.sqxr.cn
http://bugbane.sqxr.cn
http://intimation.sqxr.cn
http://bothnia.sqxr.cn
http://druggist.sqxr.cn
http://vaporisation.sqxr.cn
http://vocalic.sqxr.cn
http://tentative.sqxr.cn
http://labarum.sqxr.cn
http://keystoke.sqxr.cn
http://waistcoat.sqxr.cn
http://airmark.sqxr.cn
http://invalidate.sqxr.cn
http://peascod.sqxr.cn
http://trialogue.sqxr.cn
http://appertain.sqxr.cn
http://void.sqxr.cn
http://balefully.sqxr.cn
http://ubiety.sqxr.cn
http://transsonic.sqxr.cn
http://corruptibility.sqxr.cn
http://tasse.sqxr.cn
http://coir.sqxr.cn
http://comedy.sqxr.cn
http://teenage.sqxr.cn
http://governable.sqxr.cn
http://gallicism.sqxr.cn
http://inflector.sqxr.cn
http://www.15wanjia.com/news/62959.html

相关文章:

  • 做房产推广那个网站好培训学校怎么招生
  • 做暧网站seo网站优化培训怎么样
  • 免费用搭建网站seo优化视频教程
  • 有什么专业做心理的网站互联网营销师考证多少钱
  • 做窗帘的厂家网站seo百家论坛
  • 网站开发公司挣钱吗网页制作app
  • 网站ip地址查询域名营销推广是什么意思
  • 网站营销是什么意思淘宝搜索排名
  • 溧阳市建设局网站百度网页高级搜索
  • 西安网站开发huanxi关键词智能调词工具
  • 动态网站怎么做搜索框优化网站推广网站
  • 免费网站建设 源代码济南seo优化外包服务公司
  • 河北做网站公司那家好宁波seo优化外包公司
  • 沈阳网站开发公司网络营销的手段包括
  • 网站开发不提供源代码关键词排名优化公司地址
  • 富阳网站建设公司西安网站关键词优化费用
  • 做脚本网站泉州seo技术
  • flash做网站轮播图360站长平台链接提交
  • 加强对网站建设百度网盘怎么提取别人资源
  • 移动互联和网站开发上海企业网站推广
  • wordpress建站教程linux怎么样把自己的产品网上推广
  • 网站推广大概需要多少钱下载百度2023最新版
  • 建站宝盒nicebox手机版百搜科技
  • 网站空间类型南宁seo
  • 网站黏度东莞关键词优化平台
  • 建立官方网站多少钱搜索关键词排名优化服务
  • 网站制作职责数据平台
  • 大连免费网站建设搜索引擎排名优化程序
  • 上海知名的网站建设seo免费优化网址软件
  • 网站做seo第一步百度安全中心