Grails - @Transactional, Connection is read-only

2019-08-07 06:35发布

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

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()
    }
}

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] }
    }
}

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.

Any help would be appreciated!

1条回答
ゆ 、 Hurt°
2楼-- · 2019-08-07 07:27

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 .

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.

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)

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.

查看更多
登录 后发表回答