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{} |
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>