Spring异常抛出触发事务回滚策略

切面中的异常处理是否会影响事务的回滚 在 Spring 框架中,AOP(面向切面编程)和事务管理是两个重要的特性。当使用 AOP 切面时,特别是在业务逻辑周围添加日志记录、性能监控等横切关注点时,可能会遇到切面代码抛出异常是否会影响主业务方法的事务回滚的问题。 阅读详情

Spring、EJB的声明式事务默认情况下都是在抛出unchecked exception后才会触发事务的回滚 

测试用业务逻辑方法: 

 


整个包裹起来,则这个业务方法也就等于脱离了spring事务的管理,因为没有任何异常会从业务方法中抛出!全被捕获并吞掉,导致spring异常抛出触发事务回滚策略失效。 
不过,如果在catch代码块中采用页面硬编码的方式使用spring api对事务做显式的回滚,这样写也未尝不可。 

Java代码   收藏代码
  1. /** 
  2.  * 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*)配置事务在什么情况下回滚(格式:-引起回滚的异常类型) 
  3.  * 则spring默认只会在service方法抛出unchecked exception才会触发回滚 
  4.  */  
  5. public class TestServiceImpl extends Service implements TestService {  
  6.   
  7.     /** 
  8.      * 测试spring异常触发事务回滚的service方法 
  9.      */  
  10.     public void testAddPerson(String name) throws Exception {  
  11.         TestPerson p = new TestPerson();  
  12.         p.setName(name);  
  13.         this.getHibernateGenericController().save(p);  
  14.           
  15.         /* 
  16.          * 制造RuntimeException 
  17.          * 在spring默认的异常回滚策略下,该异常的抛出会触发事务的回滚,数据不会被插入 
  18.          */  
  19.         throw new RuntimeException("抛出个运行时异常");  
  20.           
  21.         /** 
  22.          * 同样是unchecked exception,spring默认的异常回滚策略下Error的抛出也会触发事务的回滚,数据不会被插入 
  23.          */  
  24. //      throw new Error();  
  25.           
  26.         /** 
  27.          * 即使是在spring默认的异常触发事务回滚策略(unchecked exception触发回滚)下, 
  28.          * 如果使用try-catch捕获抛出的unchecked异常后没有在catch块中采用页面硬编码的方式使用spring api对事务做显式的回滚,则事务不会回滚,数据被插入 
  29.          * “将异常捕获,并且在catch块中不对事务做显式提交=生吞掉异常” 
  30.          */  
  31. //      try {  
  32. //          throw new RuntimeException("这个抛出的运行时异常会被try-catch捕获");  
  33. //      } catch(Exception e) {  
  34. //          System.out.println("捕获到异常: " + e.getMessage());  
  35. //      }  
  36.           
  37.         /** 
  38.          * 因为Exception类是个checked exception,所以这里抛出的异常不会触发事务的回滚,数据被插入 
  39.          */  
  40. //      throw new Exception("a Exception instance");  
  41.           
  42.         /** 
  43.          * 该BaseException继承自Exception,也为checked exception,抛出它后: 
  44.          * spring默认的回滚策略下,事务未回滚, 数据被插入; 
  45.          * 在TransactionProxyFactoryBean的transactionAttributes中配置 
  46.                     <prop key="test*"> 
  47.                         ... ...,-BaseException 
  48.                     </prop> 
  49.             后,事务回滚,数据未被插入 
  50.          */  
  51. //      throw new BaseException("一个BaseException");  
  52.     }  
  53.   
  54. }  


关于TransactionProxyFactoryBean的transactionAttributes中字符串的值(定义自TransactionAttributeEditor): 
is a transaction attribute descriptors that parsed via TransactionAttributeEditor 
见本博客:http://wuaner.iteye.com/admin/blogs/567792 

单元测试代码: 

Java代码   收藏代码
  1. public class TestSpringDefaultRollback extends TestCase {  
  2.   
  3.     private static ApplicationContext context = new ClassPathXmlApplicationContext("resource/xxx/applicationContext.xml");  
  4.     public void testDefaultRollback() throws Exception{  
  5.         TestService testServiceImpl = (TestService)context.getBean("testService");  
  6.         testServiceImpl.testAddPerson("张三");  
  7.     }  
  8. }  

 




将异常捕获,并且在catch块中不对事务做显式提交(或其他应该做的操作如关闭资源等)=生吞掉异常 

spring的事务边界是在调用业务方法之前开始的,业务方法执行完毕之后来执行commit or rollback(Spring默认取决于是否抛出runtime异常). 
如果抛出runtime exception 并在你的业务方法中没有catch到的话,事务会回滚。 
一般不需要在业务方法中catch异常,如果非要catch,在做完你想做的工作后(比如关闭文件等)一定要抛出runtime exception,否则spring会将你的操作commit,这样就会产生脏数据.所以你的catch代码是画蛇添足。



 

笔记汇总 | 斯坦福 CS229 机器学习 本文为斯坦福大学 2014 秋季 CS229 机器学习课程学习笔记本文主体部分转载自黄海广博士,文末已给出,大家有兴趣可以直接访问笔记首页,下载对应课程资料及作业代码。 阅读详情

相关推荐

Quartus II设计程序计数器PC、由PC产生ROM的地址(含仿真)

设计程序计数器PC,正常工作模式下,系统启动,设置PC的初始值,之后每来一个clk,PC的输出值应该增加1;当程序发生跳转时,PC的值应该发生改变,级将跳转的地址值赋进行输出。PC后端连接一个ROM,由PC来产生ROM的地址,仿真进行验证。

m0_73700621的博客 1815

(转载)spring异常抛出触发事务回滚策略

Spring、EJB的声明式事务默认情况下都是在抛出unchecked exception后才会触发事务的回滚 测试用业务逻辑方法:  Java代码   /**   * 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*)配置事务在什么情况下回滚(格式:-引起回滚的异常类型)   * 则spring默认只会在service方法抛出unchecked ex...

litxuf的专栏 398

西门子机床数据采集实战:从828D到840DSL的CNC设备联网与实时监控

本文深入探讨了西门子机床数据采集的实战方案,重点解析了从828D到840DSL等CNC设备如何实现免授权、低成本联网与实时监控。文章通过对比不同型号系统的数据接口差异,提供了详细的配置步骤、核心数据点清单以及避坑指南,旨在帮助工厂技术人员经济高效地挖掘机床数据价值,提升生产管理效率。

weixin_29181861的博客 1327

controller的异常处理以及service层的事务控制___controller层 trycatch不影响service层抛出的异常

controller的异常处理以及service层的事务控制 最近写代码涉及到一些事务,上午终于把代码给理顺了,之前不太清楚在哪里做异常处理,导致代码遍地try-catch,相当难看。 还是基于controller-service-dao三层来写代码,从入口开始,controller层的方法对应的是某个url,面向的是应用人员,应该返回他们能读懂的信息,所以controller必须做异常处理,一般来说会有统一的异常处理方法; service层面向的是controller,service层中的某些方法,必须保

qq_43842093的博客 8791

DaVinci Developer中枚举类型变量的创建与实战应用

本文详细介绍了在DaVinci Developer中创建和使用枚举类型变量的方法与实战应用。通过智能温控系统的案例,阐述了枚举类型在嵌入式开发中的核心价值,包括提升代码可读性、类型安全性和维护便捷性。文章还提供了枚举类型在状态机实现和多模块协同中的高级应用技巧,以及调试与优化的实用建议。

ik678901234的博客 511

房产平台系统小程序源码

🏠 一键解锁购房新体验 🔓房产小程序 实现前端 发布二手房 租房 商品 求租 求购等信息;后台发布 新房,二手房租房 商品 写字楼 求租求购等房源信息;功能完善的一块房产小程序。

2401_84413757的博客 795

spring下加了@Transaction注解的其中间抛出异常,还会执行异常下面的代码吗?

注解标注的方法中抛出异常时,由于Spring会自动回滚该方法的所有数据库操作,所以处理异常的方式会有所不同。换句话说,执行异常下面的代码是不可能的,因为整个方法执行过程被回滚了,相关的状态都被还原了。在这种情况下,建议对异常进行适当的处理,然后重新抛出该异常(或者抛出另一个异常),以便上级调用者能够收到该异常并进行相应的处理。在上面的示例中,当在更新用户时发生异常时,我们首先进行了一些处理,例如记录异常信息,最后重新抛出该异常。会自动回滚事务,我们必须确保异常处理的方式不会影响到整个事务的回滚行为。

weixin_42759398的博客 1765

Android 13 录音APK在后台无数据的处理

未获取到录音数据

chenchen4310的博客 1182

Spring事务

事务管理这个代码的固定模式可以作为一种横切关注点 但是自己要写切面类很麻烦。因为多线程, 1.不同线程获取的connection不一样。所以要保证线程同步性。 2.定义connection哪种异常需要回滚。 但是这个切面已经有了 事务切面===事务管理器 事务管理器是个接口,里面有很多实现类。 用原生jdbc操作数据库, 可以用DataSourceTransactionManger事务管理器。 这个事务切面。 持久层框架来用不同的事务管理器。 这个事务管理器就可以在目标方法运行前后进行事务控制 (事..

weixin_42561980的博客 231

ADBMS1818芯片

 可测量多达18串电池电压 3 mV最大总测量误差 内置isoSPI接口 使用单根双绞线,长达100米 290 μs内可完成系统中所有单体电池电压测量。

m0_61973119的博客 4271

3、全面掌握QGIS开发:从调试到应用的全方位指南

本文全面介绍了QGIS开发的各个环节,涵盖插件调试、PyQGIS API导航、插件创建与分发、独立地理空间应用开发,以及全局和项目级首选项的管理。通过详细的步骤说明、代码示例和流程图,帮助开发者系统掌握QGIS扩展与集成的核心技能,并展望了未来在功能拓展、跨平台兼容性和技术融合方面的可能性。

hill8的博客 89

阿里官方最新上线!Java面试八股文属于程序员的宝藏

面试题包括了:Java面试、Spring、MyBatis、ZK、Dubbo、EL、Redis、MySQL、并发编程、微服务、Linux、Springboot、SpringCloud、MQ、Kafka 面试专题。177. 一张自增表里面总共有 7 条数据,删除了最后 2 条数据,重启 MySQL 数据库,又插入了一条数据,此时 id 是几?164. kafka 同时设置了 7天和 10G 清除数据,到第五天的时候消息达到了 10G,这个时候 kafka 将如何处理?session与cookie的区别是什么?

BASK2311的博客 1172

AMDGPU SVM VRAM Migration 实现详解

AMD正在基于DRM SVM框架重构SVM实现,该框架即将广泛应用。本文分析了AMD开发者提交的POC验证版本的技术方案。该架构分为三层:AMDGPU SVM映射层、DRM Pagemap通用框架层和AMDGPU迁移后端硬件驱动层。系统涉及三个地址空间转换:VRAM分配器空间、HPA/PFN空间和GPU PTE地址空间。关键数据结构包括设备级迁移基础设施amdgpu_pagemap、迁移临时BO包装器amdgpu_svm_bo等。该方案通过地址空间转换实现VRAM迁移功能,目前仍处于POC阶段,设计可能变更

图形/计算GPU的驱动与虚拟化技术分享园地 862

spring异常抛出触发事务回滚策略导读

[转自:http://cn.dydou.cn/ws/201108/0QQI132011.html]spring异常抛出触发事务回滚策略导读:Spring、EJB的声明式事务 默认 情况下都是在 抛出unchecked exception 后才会触发事务的回滚 测试用业务逻辑方法: Java代码 /** *如果在spring事务配置中不为切入点(如这里的切入点可以定义...... ------

L-BAKE 2282

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

java 异常事务回滚_(转)spring异常抛出触发事务回滚策略

背景:在面试时候问到事务方法在调用过程中出现异常,是否会传递的问题,平时接触的比较少,有些懵逼。Spring、EJB的声明式事务默认情况下都是在抛出unchecked exception后才会触发事务的回滚测试用业务逻辑方法:/*** 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*)配置事务在什么情况下回滚(格式:-引起回滚的异常类型)* 则spring默认只会在ser...

weixin_28710999的博客 1443

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

(转)spring异常抛出触发事务回滚策略

背景:在面试时候问到事务方法在调用过程中出现异常,是否会传递的问题,平时接触的比较少,有些懵逼。 spring异常抛出触发事务回滚策略 Spring、EJB的声明式事务默认情况下都是在抛出unchecked exception后才会触发事务的回滚测试用业务逻辑方法: /** * 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*)配置事务在什么情况下回滚...

weixin_30814223的博客 276

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

spring异常抛出触发事务回滚策略

Spring、EJB的声明式事务[color=red]默认[/color]情况下都是在[color=red]抛出unchecked exception[/color]后才会触发事务的回滚 测试用业务逻辑方法: [code="java"] /** * 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*)配置事务在什么情况下回滚(格式:-引起回滚的异常类型) ...

Wuaner的博客 126

关于Spring事务回滚@Transactional使用记录

测试@Transactional的在处理异常时是否生效, 首先这里随便写一个update的方法,对数据库进行更新操作,然后在操作完之后抛一个异常 @Override @Transactional(rollbackFor = {Exception.class}) public String alterUser(){ try { User ...

qq_25079615的博客 1538

spring事物管理,异常回滚,如果使用try catch进行捕获不抛出,事物不回滚

1.在今天测试代码的正确性,遇到了事物不进行回滚操作. 例子:(伪代码) 当第二个insert的时候,第一个方法没有进行回滚.(查询数据库,数据表有产品信息)   2.查询解答: 默认情况下spring事务只在发生未被捕获的 RuntimeException时才回滚(运行时异常) try catch这种把整个包裹起来,这种业务方法也就等于脱离了spring事务的管理,因为没有任何异常...

wang1360535056的专栏 2782

java事务什么时候回滚_spring事务什么时候会自动回滚

在java中异常的基类为Throwable,他有两个子类Exception与Errors。同时RuntimeException就是Exception的子类,只有RuntimeException才会进行回滚;1,Spring事务回滚机制是这样的:当所拦截的方法有指定异常抛出,事务才会自动进行回滚!我们需要注意的地方有四点:如果你在开发当中引入Spring进行事务管理,但是事务没能正常的自动回滚,可...

weixin_39894778的博客 571

java 自定义异常 未回滚_抛出自定义异常,spring AOP事务不回滚的解决方案

Spring AOP默认对RuntimeException()异常或是其子类进行事务回滚,也就是说事务回滚:throw new RuntimeException("xxxxxxxxxxxx");事物不回滚:throw new Exception("xxxxxxxxxxxx");那么,如何在抛出Exception或者自定义异常时,事务也进行回滚呢?有以下几种方案:1、在捕获到异常或者显示的抛出R...

weixin_36337389的博客 227

Spring事务异常回滚,try catch 捕获异常不回滚

例:一个方法报异常,另一个方法不会回滚 try { userDao.save(user); userCapabilityQuotaDao.save(capabilityQuota); } catch (Exception e) { } 例:一个方法报异常,另一个方法回滚 在catch语句中最后增加throw new RuntimeException()语句,以便让aop...

一无是处的靓仔 193

Spring管理事务默认回滚的异常是什么?

  问题: Spring管理事务默认(即没有rollBackFor的情况下)可以回滚的异常是什么? 回答: RuntimeException或者Error。 抛出运行时异常,是否回滚?Yes @Transactional public boolean rollbackOn(Throwable ex) { return new Runtime...

zzc1684的博客 939

事务@Transactional 回滚情况分析与try catch结合使用

前提 Spring 事务注解 @Transactional 本来可以保证原子性,如果事务内有报错的话,整个事务可以保证回滚,但是加上try catch或者事务嵌套,可能会导致事务回滚失败。 测试一 @Transactional public void test() throws Exception { ProductCategoryJd model = new ProductCategoryJd(); model.setCatId((long) 100);

博客 1785
上一篇: css基础布局
下一篇: css基础下拉菜单
Sunset-
博客等级 码龄12年 0粉丝 11原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值