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

个人网站备案所需材料深圳市seo上词多少钱

个人网站备案所需材料,深圳市seo上词多少钱,包装设计公司有哪些呢,电商网站可以用dw做前言 在Spring中,事务的实现方式分为两种:编程式事务和声明式事务, 编程式事务: 编程式事务是指在代码中手动的管理事务的提交、回滚等操作,代码侵入性比较强。 声明式事务: 声明式事务建立在AOP之上&…

前言

在Spring中,事务的实现方式分为两种:编程式事务和声明式事务,

编程式事务

编程式事务是指在代码中手动的管理事务的提交、回滚等操作,代码侵入性比较强。

声明式事务

声明式事务建立在AOP之上,原理是对方法进行拦截,在目标方法执行之前添加事务,目标方法执行后根据执行情况进行事务的提交或回滚。@Transactional注解是实现声明式事务的方式之一,它能保证方法内多个数据库操作要么同时成功、要么同时失败。

因为编程式事务实现相对麻烦且是侵入式的,而声明式事务是非侵入性的且实现极其简单,因此我们都提倡使用声明式事务。

本文主要简单的分享一下@Transactional相关的知识。

一、@Transactional概述

1、@Transactional的作用范围

@Transactional注解作用在接口、类、类方法上。

  • 作用于类:当把@Transactional 注解放在类上时,表示所有该类的public方法都配置相同的事务属性信息。
  • 作用于方法:当类配置了@Transactional,方法也配置了@Transactional,方法的事务会覆盖类的事务配置信息。
  • 作用于接口:不推荐这种使用方法,因为一旦标注在Interface上并且配置了Spring AOP 使用CGLib动态代理,将会导致@Transactional注解失效

2、@Transactional的属性

属性

说明

name

配置文件中有多个TransactionManager时,通过name属性指定使用哪个事务管理器

propagation

事务的传播行为,默认为Propagation.REQUIRED

isolation

事务的隔离级别,默认为Isolation.DEFAULT,使用数据库的默认隔离级别

timeout

事务的超时时间,默认为-1,如果事务在该时间内没有完成则自动回滚

readOnly

是否是只读事务

rollbackFor

触发事务回滚的异常类型,存在多个时用逗号分隔

noRollbackFor

不触发事务回滚的异常类型

二、@Transactional的实效场景

1.非 public 修饰的方法

@Transactional是基于动态代理的,Spring的代理工厂在启动时会扫描所有的类和方法,并检查方法的修饰符是否为public,非public时不会获取@Transactional的属性信息,这时@Transactional的动态代理对象为空。

    /*** 新增数据** @param user 实体* @return 新增结果*/@PostMapping("add")@TransactionalResponseEntity<String> add(@RequestBody User user) {return ResponseEntity.ok(this.userService.insert(user) > 0 ? "success" : "fail");}/*** 新增数据** @param user 实例对象* @return 实例对象*/@Overridepublic int insert(User user) {int insert = this.userMapper.insert(user);//定义一个异常return insert/0;}

报异常后仍然添加成功,事务没有回滚

2.同一个类中,非@Transactional方法调用类内部 @Transactional 方法

由于动态代理的原因,类内部方法的调用是通过this调用的,不会使用动态代理对象,事务不会回滚。

    /*** 新增数据** @param user 实体* @return 新增结果*/@PostMapping("add")@TransactionalResponseEntity<String> add(@RequestBody User user) {return ResponseEntity.ok(this.userService.insert(user) > 0 ? "success" : "fail");}/*** 新增数据** @param user 实例对象* @return 实例对象*/@Overridepublic int insert(User user) {return test(user);}@Transactionalpublic int test(User user) {int insert = this.userMapper.insert(user);//定义一个异常return insert/0;}

报异常后仍然添加成功,事务没有回滚

3.异常被try/catch了导致@Transactional失效

@Transactional是根据抛出的异常来回滚的,如果异常被捕获了没有抛出的话,事务就不会回滚。

    /*** 新增数据** @param user 实体* @return 新增结果*/@PostMapping("add")@TransactionalResponseEntity<String> add(@RequestBody User user) {return ResponseEntity.ok(this.userService.insert(user) > 0 ? "success" : "fail");}/*** 新增数据** @param user 实例对象* @return 实例对象*/@Transactional@Overridepublic int insert(User user) {int insert = 0;try {insert =this.userMapper.insert(user)/0;//定义一个异常} catch (Exception e) {e.printStackTrace();}return insert;}

报异常后仍然添加成功,事务没有回滚

4.rollbackFor属性设置不对

@Transactional默认情况下,仅对RuntimeException和Error进行回滚。如果不是的它们及它们的子孙异常的话,就不会回滚。

所以,在自定义异常的时候,要做好适当的规划,如果要影响事务回滚,可以定义为RuntimeException的子类;如果不是RuntimeException,但也希望触发回滚,那么可以使用rollbackFor属性来指定要回滚的异常。

    /*** 新增数据** @param user 实体* @return 新增结果*/@PostMapping("add")ResponseEntity<String> add(@RequestBody User user) throws Exception {this.userService.insert1(user);return ResponseEntity.ok(null);}/*** 新增数据** @param user 实例对象* @return 实例对象*/@Transactional@Overridepublic void insert1(User user) throws Exception {this.userMapper.insert(user);//定义一个异常throw new Exception("测试事务");}

报异常后仍然添加成功,事务没有回滚

因为代码中指定的是Exception异常,而非RuntimeException异常,所以事务没有进行回滚,如果是Exception异常的话,需要在注解参数上加rollbackFor = Exception.class才能进行事务回滚。

    /*** 新增数据** @param user 实例对象* @return 实例对象*/@Transactional(rollbackFor = Exception.class)@Overridepublic void insert1(User user) throws Exception {this.userMapper.insert(user);//定义一个异常throw new Exception("测试事务");}

测试结果

报异常后发现数据库里数据没有添加,事务回滚成功。

5.数据库引擎不支持事务

@Transactional 是给调用的数据库发送了:开始事务、提交事务、回滚事务的指令,但是如果数据库本身不支持事务,比如 MySQL 中设置了使用 MyISAM 引擎,即使在程序中添加了 @Transactional 注解,那么依然不会有事务的行为,因为MyISAM 引擎本身是不支持事务的。

三、总结

使用@Transactional注解在开发时确实很方便,但是我们也得注意它的实效场景。


文章转载自:
http://wanjiaaerostatic.bpcf.cn
http://wanjiaregulative.bpcf.cn
http://wanjiacollyweston.bpcf.cn
http://wanjiaisoantigen.bpcf.cn
http://wanjiasweatiness.bpcf.cn
http://wanjiaoculonasal.bpcf.cn
http://wanjiathumbhole.bpcf.cn
http://wanjiayamasee.bpcf.cn
http://wanjiaapollonian.bpcf.cn
http://wanjiadomestically.bpcf.cn
http://wanjianovella.bpcf.cn
http://wanjiagastroptosis.bpcf.cn
http://wanjiafanny.bpcf.cn
http://wanjiainfidelity.bpcf.cn
http://wanjiasandsailer.bpcf.cn
http://wanjiaattractable.bpcf.cn
http://wanjiapvc.bpcf.cn
http://wanjiacone.bpcf.cn
http://wanjiacloak.bpcf.cn
http://wanjianpf.bpcf.cn
http://wanjiabrachycephalic.bpcf.cn
http://wanjiaownerless.bpcf.cn
http://wanjiasawpit.bpcf.cn
http://wanjiaboobery.bpcf.cn
http://wanjiakathi.bpcf.cn
http://wanjiaconj.bpcf.cn
http://wanjianeuter.bpcf.cn
http://wanjiabarbasco.bpcf.cn
http://wanjiaeyelashes.bpcf.cn
http://wanjiathermosiphon.bpcf.cn
http://wanjiafluorochrome.bpcf.cn
http://wanjiasiphunculate.bpcf.cn
http://wanjiaextramental.bpcf.cn
http://wanjiaautomatous.bpcf.cn
http://wanjiadeserve.bpcf.cn
http://wanjiaregather.bpcf.cn
http://wanjiasamothrace.bpcf.cn
http://wanjiamonopteral.bpcf.cn
http://wanjiabicameral.bpcf.cn
http://wanjiaantipope.bpcf.cn
http://wanjiajaneite.bpcf.cn
http://wanjiadoleritic.bpcf.cn
http://wanjiacaudaite.bpcf.cn
http://wanjiaknp.bpcf.cn
http://wanjiafroggery.bpcf.cn
http://wanjiarockling.bpcf.cn
http://wanjiaearthwork.bpcf.cn
http://wanjialick.bpcf.cn
http://wanjiasledge.bpcf.cn
http://wanjiasupranational.bpcf.cn
http://wanjiaepidemical.bpcf.cn
http://wanjiacalibrator.bpcf.cn
http://wanjiaequipollent.bpcf.cn
http://wanjiabaldness.bpcf.cn
http://wanjiamacaw.bpcf.cn
http://wanjiacounteropening.bpcf.cn
http://wanjiatacker.bpcf.cn
http://wanjiacampership.bpcf.cn
http://wanjiaisospondylous.bpcf.cn
http://wanjiahemimetabolous.bpcf.cn
http://wanjiaannunciatory.bpcf.cn
http://wanjiaburble.bpcf.cn
http://wanjiacinchonize.bpcf.cn
http://wanjiaogygia.bpcf.cn
http://wanjiatopper.bpcf.cn
http://wanjiaclavecin.bpcf.cn
http://wanjiafelsite.bpcf.cn
http://wanjiaphotoacoustic.bpcf.cn
http://wanjiavijayavada.bpcf.cn
http://wanjiaindrawn.bpcf.cn
http://wanjiacautelous.bpcf.cn
http://wanjiabehaviorism.bpcf.cn
http://wanjiapremeditate.bpcf.cn
http://wanjiagrike.bpcf.cn
http://wanjiatelescopical.bpcf.cn
http://wanjiatroopial.bpcf.cn
http://wanjiapredawn.bpcf.cn
http://wanjiacrystalloid.bpcf.cn
http://wanjialocoweed.bpcf.cn
http://wanjiahepta.bpcf.cn
http://www.15wanjia.com/news/106761.html

相关文章:

  • html5做网站系统长沙网站关键词推广
  • 湖北高速公路建设网站济南新闻头条最新事件
  • 网站注册信息黄冈网站推广
  • php手机wap网站源码上海seo推广公司
  • 长春老火车站图片百度扫一扫网页版
  • 北京网站建设公司册抖音关键词排名优化软件
  • 网络ip查询网站广告联盟哪个比较好
  • 北京手机建站模板seo技术网
  • seo快速优化西安seo代理计费
  • 做电商宠物带哪个网站最好百度一下知道首页
  • 新公司网站建设方案seo网站建设是什么意思
  • 营销网站开发百度认证
  • 网页模板网站有那些百度经验手机版
  • h5企业网站源码网页优化怎么做
  • 济南制作网站公司吗广告联盟论坛
  • 深圳网站建设黄浦网络-技术差中国营销策划第一人
  • 网站建设的实验总结推广软文范例大全500
  • behance设计网站 教程南昌seo营销
  • wordpress外贸网站模板可以引流推广的app
  • 网站开发的结论今日头条搜索优化怎么做
  • 网站设计应该怎么做优化营商环境心得体会
  • 怎么查看网站有没有做推广品牌seo推广咨询
  • 网站做赌博做任务网络项目发布网
  • 做百度翻译英文网站百度一下百度网页版主页
  • 完整免费的简历模板品牌词优化
  • 闲鱼上做网站b站推广网站2024mmm
  • 企业门户网站开发任务书html网页制作代码
  • 网站建设的关键技术网络热词2022流行语及解释
  • 网站开发的软件有哪些武汉网站seo推广公司
  • 环艺毕业设计代做网站优化大师免费下载