0
votes

Having trouble creating an object "from scratch" (in code instead of from forms) in Grails. The following is the code snippet from a method in a controller (Assessment).

def newVol = new Vol(
              volType:type,
                app: assessmentInstance.app,
                assessment:assessmentInstance,
                dateFound: assessmentInstance.assDate,
                urlParam: url,
                volName: "TEST",
                volNote: "TEST"
         )

        newVol.save(validate:false,flush:true, failOnError:true)

result is this error:

Class
org.postgresql.util.PSQLException
Message
ERROR: cannot execute nextval() in a read-only transaction

the id is mapped to a generator in the Vol class:

   static mapping = {
    id column:'vol_id'

    id generator: 'sequence',params:[sequence:'scan_sequence']
}

I really don't understand how a save could be thought of as a read-only transaction. I tried messing around with the @transactional settings on my domain class, first putting readOnly to false and then removing the @transactional but it did not change things. It looks like I should look for alternatives to using a database sequence. What do you think?

UPDATE removed the readonly=true from the controller where the above code lives and that worked. But... what are the side effects of removing that?

1

1 Answers

2
votes

The question is why are you trying to do a write operation in a read-only transaction? If you are doing a write operation then removing readOnly=true is absolutely the right thing to do.

If you are only performing a read then you should not remove it. The reason is that having readOnly=true improves performance as Hibernate does not need to perform dirty checking for a read-only transaction.