事物回滚出现:No transaction aspect-managed TransactionStatus in scope异常处理

No transaction aspect-managed TransactionStatus in scope异常处理

org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionStatus in scope异常处理
    
    

1.业务场景  

在一段数据库操作中添加了事务注解@Transactional(rollbackFor = Exception.class),但是代码中有try catch所以相当于自己捕获,未往外抛,相当于没有异常,所以事务不起作用

2.解决办法

在catch中,添加手动回滚

try{}

		<p>catch (Exception e) {<br>
		&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;logger.error(e.getMessage(),e);<br>
		&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//手动回滚<br>
		&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();<br>
		&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}</p>
		</td>
	</tr></tbody></table><p>3.遇到问题:运行中,抛异常</p>
org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionStatus in scope
 
 

原因分析:手动回滚之前必须声明该方法被事务管理@Transactional(rollbackFor = Exception.class),此时手动回滚才能生效,事务注解相当于开启事务transactionManager.commit(status);所以,

@Transactional(rollbackFor = Exception.class)+TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

手动回滚才能生效

4.

在web项目中,很多时候要用到@Transactional 注解方法或者类进行事务处理,自动事务提交有时候就会有问题,这个时候就要用到手动进行事务提交 ,在try catch 异常抛出里面手动回滚事务处理TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

使用Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint(); 设置回滚点,

使用TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);回滚到savePoint。

				<script>
					(function(){
						function setArticleH(btnReadmore,posi){
							var winH = $(window).height();
							var articleBox = $("div.article_content");
							var artH = articleBox.height();
							if(artH > winH*posi){
								articleBox.css({
									'height':winH*posi+'px',
									'overflow':'hidden'
								})
								btnReadmore.click(function(){
									articleBox.removeAttr("style");
									$(this).parent().remove();
								})
							}else{
								btnReadmore.parent().remove();
							}
						}
						var btnReadmore = $("#btn-readmore");
						if(btnReadmore.length>0){
							if(currentUserName){
								setArticleH(btnReadmore,3);
							}else{
								setArticleH(btnReadmore,1.2);
							}
						}
					})()
				</script>
				</article>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值