Grails - @Transactional,Connection是只读的 [英] Grails - @Transactional, Connection is read-only

查看:819
本文介绍了Grails - @Transactional,Connection是只读的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用静态脚手架为我的 Test 域类创建CRUD方法。当使用创建的 TestController 只有我没有问题,无论如何保存新的测试对象。但是,我想扩展功能并实现相应的 TestService 类。这样做我现在总是得到一个错误保存测试对象:连接是只读的。不允许导致数据修改的查询

I used static scaffolding to create CRUD-methods for my Test domain class. When using the created TestController only I had no problems whatsoever saving new Test objects. However, I wanted to extend the functionality and implemented a corresponding TestService class. Doing so I now always get an error on saving a Test object: Connection is read-only. Queries leading to data modification are not allowed

这里开始出现故障。按照 TestService 的代码。如果我正确理解它,就不需要使用 @Transactional 注释和两个 save 和<$

And here it started to malfunction. Following the code of TestService. If I understood it correctly, it would not be necessary to use the @Transactional annotation with the two save and delete methods as they would inherit that functionality directly from the class.

package test.services

import grails.transaction.Transactional
import test.Test

@Transactional
class TestService {
    @Transactional(readOnly = true)
    def tests() {
        Test.list()
    }

    @Transactional(readOnly = true)
    def count() {
        Test.count()
    }

    @Transactional
    def save(test) {
         test.save()
    }

    @Transactional
    def delete(test) {
        test.delete()
    }
}

这里也是在 TestController Test c $ c>:

Here is also the code for saving a Test object in the TestController:

def save(Test test) {
    if (testInstance == null) {
        notFound()
        return
    }

    if (testInstance.hasErrors()) {
        respond testInstance.errors, view:'create'
        return
    }

    testService.save(testInstance)

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.created.message', args: [message(code: 'test.label', default: 'Test'), testInstance.id])
            redirect testInstance
        }
        '*' { respond testInstance, [status: CREATED] }
    }
}

我改变的唯一的是状态 testService.save(testInstance)的行。在此之前,脚手架过程放置 testInstance.save flush:true

The only thing I changed is the line which states testService.save(testInstance). Before that, the scaffolding process put testInstance.save flush: true

我真的看不到错误。特别是,作为另一个域类的对象可以毫无问题地使用或多或少相同的配置保存。

I don't really see a mistake. Especially, as another domain class' objects can be saved without problems using more or less the same configuration.

任何帮助将不胜感激!

推荐答案

首先确保你使用的是最近的hibernate或hibernate4插件版本。
目前最新的hibernate插件版本是3.6.10.17,hibernate4插件版本是4.3.5.5。

First make sure you are using a recent hibernate or hibernate4 plugin version. Currently the newest hibernate plugin version is 3.6.10.17 and hibernate4 plugin version is 4.3.5.5 .

这个问题可能是由只读集成引起的在Grails 2.3.0中的 GRAILS-10063 。默认行为由 GRAILS-11177 修复,并由此提交修复: https://github.com/grails/grails-data-mapping/commit/6447be90

This problem might be caused by the read-only integration added by GRAILS-10063 in Grails 2.3.0 . The default behaviour was fixed by GRAILS-11177 and was fixed by this commit: https://github.com/grails/grails-data-mapping/commit/6447be90.

还请确保您在Config.groovy中缺少这些设置或设置为false

Also make sure you have these settings missing or set to false in Config.groovy

grails.hibernate.pass.readonly = false
grails.hibernate.osiv.readonly = false

(如果缺少这些设置,则不必添加这些设置)

(you don't have to add these settings if they are missing)

只有使用带有hibernate插件的只读集成功能hibernate3)与在DataSource.groovy中的hibernate配置中的singleSession = false设置。 Spring的OSIVI for Hibernate 4不支持singleSession = false模式。

It only makes sense to use the readonly integration feature with hibernate plugin (hibernate3) with the singleSession = false setting in hibernate configuration in DataSource.groovy . The singleSession=false mode isn't supported in Spring's OSIVI for Hibernate 4.

这篇关于Grails - @Transactional,Connection是只读的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆