我将 grails 1.3.7 与 Oracle 11g 一起使用并尝试管理内部事务。我有一个 bean Person,它被传递给进行一些修改的事务(Propagation.REQUIRED)服务方法。然后将其传递给另一个事务性(propagation = Propagation.REQUIRES_NEW)方法,该方法进行一些其他修改,然后引发异常。我期望看到的是对第二个服务的所有修改的回滚,但对第一个服务的修改仍然有效。这是这种情况:
//outer transaction
class MyService {
def nestedService
@Transactional(propagation = Propagation.REQUIRED)
public void testRequiredWithError(Person person) {
person.name = 'Mark'
try {
nestedService.testRequiresNewWithError(person)
} catch (RuntimeException e) {
println person.age //this prints 15
println e
}
}
}//end MyService
//inner transaction
class NestedService{
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testRequiresNewWithError(Person person) {
person.age = 15 //expected after Exception will be discarded
throw new RuntimeException("Rollback this transaction!")
}
}
然后我运行 grails 控制台并在它结束后检查数据库。...
def p = Person.get(671)
def myService = ctx.myService
println p.name //'John'...from DB
println p.age //25...from DB
myService .testRequiredWithError(p)
println p.name // 'Mark'....correct
println p.age // 15....UNEXPECTED..
//same result checking on the DB after console ends and transaction flushes
我尝试在引导程序中激活它后使用 Propagation.NESTEDtransactionManager.setNestedTransactionAllowed(true)
并使用保存点,如这篇文章
grails transaction set savepoint
但仍然得到相同的结果。
我错过了什么????
先感谢您。