I've got an app with a method that, for some reason I can't figure out, is saving an object that I load at the end of the controller method, even though I specify that it shouldn't.
def testMethod( Long id ) {
def target = SampleDomain.read( id );
// use target in a further query, but don't change target at all
// I've checked, and target.isDirty() is false here
target.discard()
render( view: 'blah', model: [ /* this model does NOT contain the target */ ] )
}
For some reason, if I turn on logSql, I can see that the target object is being saved at the end of this method, even though I use "read" and "discard". The version field seems to be the only db field being changed in the table.
Is there some way to annotate the method to make sure no "updates" are performed during the controller method's session? I admit to not being fully knowledgeable about the possible arguments to the @Transactional annotation.
I'm using Grails v2.2.4.
It would seem this is easier than I imagined. All I need is the readOnly argument to the Transactional annotation:
@Transactional(readOnly = true)
def testMethod( Long id ) {
def target = SampleDomain.read( id );
// use target in a further query, but don't change target at all
// I've checked, and target.isDirty() is false here
render( view: 'blah', model: [ /* this model does NOT contain the target */ ] )
}
I'm still not sure WHY it was updating the object after I used the discard() method... but now I don't even need it.