PROPAGATION_REQUIRED在春天的行为?

时间:2014-12-15 12:40:01

标签: java spring transactional

我对PROPAGATION_REQUIRED行为的问题很少,我无法在Spring Docs澄清。

Scenario1: -

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

当线程来自method1时,将提交数据。对吗?

Scenario2: -

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

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

当线程来自method1时,将提交数据。对吗?

场景3: -

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

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

什么都不会被提交。对吗?

Scenario4: -

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

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

整个事务将作为线程从method1作为已检查异常提交 被扔了。虽然我可以用@Transactional(rollbackFor=SomeCheckedException.class)改变这种行为吧?

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

1 个答案:

答案 0 :(得分:0)

是的,已检查的异常不会自动回滚活动事务;只取消选中RuntimeException

如果要对某些已检查的例外进行回滚,可以使用@Transactional(rollbackFor=SomeCheckedException.class)

注意:在内部调用方法时,@Transactional无效:

method1(){
  method2();
}

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

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