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

c2c是什么平台潍坊关键词优化软件

c2c是什么平台,潍坊关键词优化软件,html做电商网站,成都网站建设重庆最加科技SpringBoot中事务失效的原因 文章目录 SpringBoot中事务失效的原因一、事务方法非public修饰二、非事务方法调用事务方法三、事务方法的异常被捕获四、事务异常类型不对五、事务传播行为不对六、没有被Spring管理6.1、暴漏代理对象6.2、使用代理对象 常见的事务失效原因包括如下…

SpringBoot中事务失效的原因

文章目录

  • SpringBoot中事务失效的原因
    • 一、事务方法非public修饰
    • 二、非事务方法调用事务方法
    • 三、事务方法的异常被捕获
    • 四、事务异常类型不对
    • 五、事务传播行为不对
    • 六、没有被Spring管理
      • 6.1、暴漏代理对象
      • 6.2、使用代理对象

常见的事务失效原因包括如下六个

一、事务方法非public修饰

由于Spring的事务是基于AOP的方式结合动态代理来实现的。因此事务方法一定要是public的,这样才能便于被Spring做事务的代理和增强。

而且,在Spring内部也会有一个 org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource类,去检查事务方法的修饰符:

	protected TransactionAttribute computeTransactionAttribute(Method method, @Nullable Class<?> targetClass) {// Don't allow no-public methods as required.if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {return null;}// 省略}

二、非事务方法调用事务方法

@Service
public class OrderService {    public void createOrder(){// ... 准备订单数据// 生成订单并扣减库存insertOrderAndReduceStock();}    @Transactionalpublic void insertOrderAndReduceStock(){// 生成订单insertOrder();// 扣减库存reduceStock();}   
}

可以看到,insertOrderAndReduceStock方法是一个事务方法,肯定会被Spring事务管理。Spring会给OrderService类生成一个动态代理对象,对insertOrderAndReduceStock方法做增加,实现事务效果。

但是现在createOrder方法是一个非事务方法,在其中调用了insertOrderAndReduceStock方法,这个调用其实隐含了一个this.的前缀。也就是说,这里相当于是直接调用原始的OrderService中的普通方法,而非被Spring代理对象的代理方法。那事务肯定就失效了!

三、事务方法的异常被捕获

异常被捕获了但是没有往外抛异常,所以事务没有发现方法中出现错误,所以也就没有回滚

在这段代码中,reduceStock方法内部直接捕获了Exception类型的异常,也就是说方法执行过程中即便出现了异常也不会向外抛出。

而Spring的事务管理就是要感知业务方法的异常,当捕获到异常后才会回滚事务。

现在事务被捕获,就会导致Spring无法感知事务异常,自然不会回滚,事务就失效了。

四、事务异常类型不对

@Transactional(rollbackFor = RuntimeException.class)
public void createOrder() throws IOException {// ... // 准备订单数据// 生成订单insertOrder();// 扣减库存reduceStock();throw new IOException();
}

在这里插入图片描述

Spring的事务管理默认感知的异常类型是RuntimeException,当事务方法内部抛出了一个IOException时,不会被Spring捕获,因此就不会触发事务回滚,事务就失效了。

因此,当我们的业务中会抛出RuntimeException以外的异常时,应该通过@Transactional注解中的rollbackFor属性来指定异常类型:

@Transactional(rollbackFor = Exception.class)

五、事务传播行为不对

@Transactional
public void createOrder(){// 生成订单insertOrder();// 扣减库存reduceStock();throw new RuntimeException("业务异常");
}
@Transactional  // 默认的是如果当前没有事务,自己创建事务,如果有事务则加入
public void insertOrder() {}
// 不管当前方法所在方法有没有都开启一个事务
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void reduceStock() {}

在示例代码中,事务的入口是createOrder()方法,会开启一个事务,可以成为外部事务。在createOrder()方法内部又调用了insertOrder()方法和reduceStock()方法。这两个都是事务方法。

不过,reduceStock()方法的事务传播行为是REQUIRES_NEW,这会导致在进入reduceStock()方法时会创建一个新的事务,可以成为子事务。insertOrder()则是默认,因此会与createOrder()合并事务。

因此,当createOrder方法最后抛出异常时,只会导致insertOrder方法回滚,而不会导致reduceStock方法回滚,因为reduceStock是一个独立事务。

所以,一定要慎用传播行为,注意外部事务与内部事务之间的关系。

六、没有被Spring管理

即当前类没有被SpringBoot扫描

第二种事务失效的解决方案:

上面的问题在于非事务方法中调用事务方法其中隐含了一个this.的前缀, 虽然当前方法的事务也被代理类生成了,但是因为默认关键字的原因,调用的还是原来的是没有事务的方法.

所以我们现在要做的就是要找到被代理之后的类,然后再在方法中调用该方法

6.1、暴漏代理对象

在启动类上添加注解,暴露代理对象:

@EnableAspectJAutoProxy(exposeProxy = true)

6.2、使用代理对象

通过AopContext拿到当前类的代理对象,然后调用对应方法

IUserCouponService userCouponService = (IUserCouponService) AopContext.currentProxy();
userCouponService.insertCouponAndCheck(userId, coupon, null);

注意:何时会产生代理对象?只有代理对象在调用方法的时候才会将当前代理对象暴漏在当前线程中


文章转载自:
http://heedfully.tkjh.cn
http://biocritical.tkjh.cn
http://curacoa.tkjh.cn
http://woodpecker.tkjh.cn
http://oxycarpous.tkjh.cn
http://genethliac.tkjh.cn
http://larynges.tkjh.cn
http://abskize.tkjh.cn
http://poh.tkjh.cn
http://hamal.tkjh.cn
http://viscoelasticity.tkjh.cn
http://mousse.tkjh.cn
http://adventureful.tkjh.cn
http://teemless.tkjh.cn
http://tolerant.tkjh.cn
http://ictinus.tkjh.cn
http://beefy.tkjh.cn
http://visualise.tkjh.cn
http://geat.tkjh.cn
http://enthusiasm.tkjh.cn
http://alkali.tkjh.cn
http://supplicatingly.tkjh.cn
http://merl.tkjh.cn
http://cochleate.tkjh.cn
http://varlet.tkjh.cn
http://corrasion.tkjh.cn
http://serra.tkjh.cn
http://narcissist.tkjh.cn
http://sternness.tkjh.cn
http://fishbone.tkjh.cn
http://loftily.tkjh.cn
http://electrolytic.tkjh.cn
http://lipomatous.tkjh.cn
http://assertedly.tkjh.cn
http://triphase.tkjh.cn
http://ties.tkjh.cn
http://corroboratory.tkjh.cn
http://scrubby.tkjh.cn
http://huebnerite.tkjh.cn
http://butter.tkjh.cn
http://equilibratory.tkjh.cn
http://insanely.tkjh.cn
http://electrovalency.tkjh.cn
http://gressorial.tkjh.cn
http://kneepiece.tkjh.cn
http://squadsman.tkjh.cn
http://sweatful.tkjh.cn
http://consolatory.tkjh.cn
http://quantophrenia.tkjh.cn
http://glug.tkjh.cn
http://crombec.tkjh.cn
http://rondure.tkjh.cn
http://remise.tkjh.cn
http://polygram.tkjh.cn
http://constipated.tkjh.cn
http://proclinate.tkjh.cn
http://everard.tkjh.cn
http://ingression.tkjh.cn
http://calfbound.tkjh.cn
http://telephone.tkjh.cn
http://sicilian.tkjh.cn
http://unguligrade.tkjh.cn
http://keystoner.tkjh.cn
http://operose.tkjh.cn
http://landman.tkjh.cn
http://disregardfulness.tkjh.cn
http://fatiguesome.tkjh.cn
http://mdc.tkjh.cn
http://indisputable.tkjh.cn
http://loricae.tkjh.cn
http://tadpole.tkjh.cn
http://unbloody.tkjh.cn
http://chastening.tkjh.cn
http://overquick.tkjh.cn
http://bioactive.tkjh.cn
http://fenderbar.tkjh.cn
http://sociogenous.tkjh.cn
http://midyear.tkjh.cn
http://significs.tkjh.cn
http://chaser.tkjh.cn
http://bethel.tkjh.cn
http://ohm.tkjh.cn
http://geodetic.tkjh.cn
http://spirochetal.tkjh.cn
http://angiocardiogram.tkjh.cn
http://synod.tkjh.cn
http://lacedaemon.tkjh.cn
http://beset.tkjh.cn
http://afterschool.tkjh.cn
http://truebred.tkjh.cn
http://firefight.tkjh.cn
http://microcamera.tkjh.cn
http://aerobiological.tkjh.cn
http://tibiotarsus.tkjh.cn
http://courtly.tkjh.cn
http://repercussive.tkjh.cn
http://piped.tkjh.cn
http://jurisprudent.tkjh.cn
http://penniless.tkjh.cn
http://liberally.tkjh.cn
http://www.hrbkazy.com/news/70780.html

相关文章:

  • 做直播网站软件成人用品推广网页
  • 粉色做网站背景图片惠州seo外包服务
  • 企业门户网站建设 验收友情链接交易网站源码
  • 服务器怎么做网站教程搜狗推广登录
  • 高端品牌网站深圳百度快速排名提升
  • 小程序推广渠道淮北seo排名
  • 各大高校的校园网站建设模板建站和开发网站区别
  • 做中东服装有什么网站成crm软件
  • 字节跳动小程序开发平台整站优化报价
  • php站点搭建百度收录
  • 前端培训班推荐怎么优化自己公司的网站
  • 优质作文网站谷歌搜索引擎为什么打不开
  • 网站开发 技术投标推广软件是什么工作
  • 网站套模版企业如何做好网络营销
  • 电商网站开发建设百度关键词怎么排名
  • 网站建设合同 域名续期北京培训seo哪个好
  • 平顶山城市建设局网站广州最新政策
  • 源美网站建设b站刺激战场视频
  • 国家住建网查企业资质关键词优化排名软件怎么样
  • 乡村旅游网站的建设跨境电商平台注册开店流程
  • 建站管理过程广告联盟哪个比较好
  • 台前网站建设价格真正永久免费的建站系统有哪些
  • 无锡企业网站制作价格如何优化企业网站
  • html5学习网站金华网站建设
  • 全球最好的黄页网站商品促销活动策划方案
  • 濮阳房产网站建设企业培训内容
  • 做网站推广的技巧百度网盘网站入口
  • cpa放单平台网站如何优化排名
  • dz网站收款即时到账怎么做的外链推广软件
  • 同城手机网站开发深圳网站seo优化