In section 5.3 of the user guide, the following has been stated:
Also, in section 5.6, the following is mentioned:Essentially, Grails automatically binds a Hibernate session to the currently executing request. This allows you to use the save and delete methods as well as other GORM methods transparently.
However, in section 8.1, the following is written:Grails is built on Spring and hence uses Spring's Transaction abstraction for dealing with programmatic transactions. However, GORM classes have been enhanced to make this more trivial through the withTransaction method which accepts a block the first argument to which is the Spring TransactionStatus object.
This is all too confusing.Services are typically involved with co-ordinating logic between domain classes, and hence often involved with persistence that spans large operations. Given the nature of services they frequently require transactional behaviour. You can of course use programmatic transactions with the withTransaction method, however this is repetitive and doesn't fully leverage the power of Spring's underlying transaction abstraction.
Services allow the enablement of transaction demarcation, which is essentially a declarative way of saying all methods within this service are to be made transactional. All services have transaction demarcation enabled by default - to disable it, simply set the transactional property to false.
Does Grails create a Hibernate session and bind it with every request regardless of whether a service is used?
If a service is NOT used, is the persistence code written in a controller running within a transaction or not?
If yes, is this transaction committed at the end of the request?
If yes, then how is this different from the transaction demarcation done at the service method level?
What use is the "withTransaction" method other than to rollback a transaction?
When I'm using Spring in a Java EE application, and I use the @Transactional annotation on my service methods, I know where the transaction starts and ends. But in Grails, it seems that if I make changes to a persistent object in a controller, those changes are saved, even if I don't call save; so that essentially means the session is open until the request is complete as in the "Open Session in View" pattern, which I don't use.
Is there a way to change this behavior?