首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >春季PROPAGATION_REQUIRED行为?

春季PROPAGATION_REQUIRED行为?
EN

Stack Overflow用户
提问于 2014-12-15 12:40:01
回答 1查看 214关注 0票数 1

关于PROPAGATION_REQUIRED的行为,我很少有问题,但我不能在弹簧文档上澄清。

风景区1:-

代码语言:javascript
运行
复制
@Transactional
method1(){
// do some update without exception
}

当线程从method1中出来时,数据将被提交。对吧?

风景区2:-

代码语言:javascript
运行
复制
@Transactional
method1(){
// do some update without exception
method2();
}

@Transactional
method2(){
// do some update without exception
}

当线程从method1中出来时,数据将被提交。对吧?

风景区3:-

代码语言:javascript
运行
复制
@Transactional
method1(){
// do some update without exception
method2();
}

@Transactional
method2(){
 // some update in DB 
 throw new RunTimeException()
}

什么都不会被赞颂。对吧?

风景区4:-

代码语言:javascript
运行
复制
@Transactional
method1(){
// do some update without exception
method2();
}

@Transactional
method2(){
 // some update in DB 
 throw new SomeCheckedException()
}

当检查异常抛出method1线程时,整个事务将被提交。尽管我可以用@Transactional(rollbackFor=SomeCheckedException.class)改变这种行为,对吗?

如果上面的理解是正确的,请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-15 14:09:58

是的,选中的异常不会自动回滚活动事务;只有未选中的RuntimeException才会回滚。

如果要对某些选中的异常回滚,可以使用@Transactional(rollbackFor=SomeCheckedException.class)

注意:当您在内部调用该方法时,@Transactional没有任何效果:

代码语言:javascript
运行
复制
method1(){
  method2();
}

@Transactional
method2(){
  // some update in DB -> fails because there is no transaction
}

只有当Spring可以包装方法调用时,才能应用注释。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27484328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档