Grails GORM save()没有事务外的flush,直接将数据保存在数据库中

时间:2016-04-01 13:40:37

标签: java mysql hibernate grails gorm

Grails 2.4.5

我没有在Config.groovy

中将autoFlush激活为true
@Secured(['ROLE_ADMIN'])
class TestController
{

    def testFlush() {
        User newUserNoFlushOutside = new User(
        firstname: 'testwithoutTransactionWithoutFlush',
        lastname: 'lastname',
        password: 'password',
        email: 'email@test.com').save(validate: false)  // Outside transaction ==> directly in database even without flush:true

        System.out.println("User id for user created outside transaction and without flush "+newUserNoFlushOutside.id) // break point the println line, the new user is already in database
    }

}

通过调试我深入编译类并验证Flush模式是“MANUAL”(不是“AUTO”),似乎SQL insert语句直接发送到数据库。因为我们在事务之外,并且由于MySQL默认配置是自动提交,所以insert语句肯定会执行到数据库(不能回滚,mysql自动更新它)。

我的问题是发生了什么?

为什么SQL语句被发送到MySQL数据库,尽管没有刷新GORM会话(我甚至试图指定save(flush:false))?

或许我错过了关于冲洗概念的一些内容?

0 个答案:

没有答案