简体   繁体   English

grails.gorm.transactions.Transactional不回滚

[英]grails.gorm.transactions.Transactional not rolling back

I am having issues rolling back a transaction in my service layer with the following: 我在使用以下内容回滚服务层中的事务时遇到问题:

Grails 3.3.8 Grails 3.3.8

GORM 6.1.10.RELEASE GORM 6.1.10。发布

I have the following service method: 我有以下服务方法:

import grails.gorm.transactions.Transactional

@Transactional(rollbackFor = Exception.class)
class TestingService {

    void testServiceMethod(List<Factory> factories) {
        try {
            factories.each {
                if (it.name == 'second') {
                    throw new Exception('second')
                }
                it.testField = 'Edited'
                it.save()
                println(it.name + ' saved')
            }
        } catch (Exception e) {
            println('Exception Caught ' + e)
        }
    }
}

I have the following integration test created then also: 我还创建了以下集成测试:

@Integration
@Rollback
class TestServiceIntSpec extends Specification {

    @Autowired
    TestingService testingService

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {
        when:
        Factory factoryOne = new Factory(name: "first").save(flush: true)
        Factory factoryTwo = new Factory(name: "second").save(flush: true)
        List<Factory> factories = [factoryOne, factoryTwo]
        testingService.testServiceMethod(factories)

        then:
        factoryOne.testField == null
        factoryTwo.testField == null
    }
}

I also have the following controller method: 我也有以下控制器方法:

class TestController {

    TestingService testingService

    def index() {
        Factory factoryOne = new Factory(name: "first").save(flush: true)
        Factory factoryTwo = new Factory(name: "second").save(flush: true)
        List<Factory> factories = [factoryOne, factoryTwo]
        testingService.testServiceMethod(factories)
        println "First Factory: $factoryOne.testField"
        println "First Factory: $factoryTwo.testField"
        render 'Check Console'
    }
}

I would have expected the test to pass as I thought the transaction would of rolled back after I threw new exception, the it.testField is persisting though however? 我本以为测试会通过,因为我认为在抛出新异常后,事务将回滚,但是it.testField仍然存在吗? Also when I ping the TestController it is outputting factoryOne.testField as 'edited'. 另外,当我对TestController进行ping操作时,它会将factoryOne.testField输出为“已编辑”。 Am I misunderstanding this correctly from the documentation? 我是否从文档中正确地理解了这一点?

"Services enable transaction demarcation, which is a declarative way of defining which methods are to be made transactional. To enable transactions on a service use the Transactional transform: The result is that all methods are wrapped in a transaction and automatic rollback occurs if a method throws an exception (both Checked or Runtime exceptions) or an Error." “服务启用事务划分,这是定义要使哪些方法成为事务性的一种声明方式。要在服务上启用事务,请使用事务转换:结果是,所有方法都包装在事务中,如果某个方法自动发生回滚引发异常(已检查异常或运行时异常)或错误。”

Source: https://docs.grails.org/latest/guide/services.html#declarativeTransactions 来源: https//docs.grails.org/latest/guide/services.html#declarativeTransactions

I can't see what I'm doing different from this other Stackoverflow answer either: 我也看不到我在做什么,而与此其他Stackoverflow答案不同:

https://stackoverflow.com/a/25739582/6887293 https://stackoverflow.com/a/25739582/6887293

The issue can be recreated by pulling the following Github project and running /factory/factory/src/integration-test/groovy/com/mycompany/myapp/TestServiceIntSpec.groovy or pinging /factory/factory/grails-app/controllers/com/mycompany/myapp/TestController.groovy 可以通过拉以下Github项目并运行/factory/factory/src/integration-test/groovy/com/mycompany/myapp/TestServiceIntSpec.groovy或ping / factory / factory / grails-app / controllers / com /来重新创建此问题。 mycompany / myapp / TestController.groovy

https://github.com/georgy3k/IntegrationTestRollBack/tree/8addd2b95a8ffa4570e70eccb3b023b0ccfef5aa https://github.com/georgy3k/IntegrationTestRollBack/tree/8addd2b95a8ffa4570e70eccb3b023b0ccfef5aa

Thanks in advance ... 提前致谢 ...

In your catch block you need to re-throw the exception. 在您的catch块中,您需要重新引发异常。

catch (Exception e) {
        println('Exception Caught ' + e)
        throw e;
}

The problem as i see it, is that the exception never escapes the method. 我所看到的问题是,异常永远不会逸出该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM