We have been experiencing some unexpected (to us) ORM behavior and are trying to reason through it. We are running cf2016, cb 5.3, and cborm but not using the included aop.
An entity is loaded from the database, updated from user input, but then doesn’t pass validation so we do not call save. Later on in an interceptor listening for postProcess() we save an object related to database backed session information and the item that didn’t pass validation still persists its data to the database.
After doing some research we came across a relevant line in the cf transaction docs https://helpx.adobe.com/coldfusion/developing-applications/coldfusion-orm/transaction-and-concurrency.html.
Whenbegins, any existing ORM session is flushed and closed, and a new ORM session is created.
This explains the behavior we are seeing. We have an entity tracked by hibernate that has been updated, when a transaction is opened (in the BaseORMService.save() called from our postProcess() interceptor) it flushes everything previously and saves the entity.
A simple, stripped down example
`
var item = entityLoadByPK( ‘Your_Entity’, ‘id’ );
// update a property
item.setActive( !item.getActive() );
// the orm session is flushed because a new transaction is opened
transaction {
// correct me if I’m wrong but - nothing in here matters?
}
`
This leads me to a couple questions
-
What is the point of the transaction{} code in the BaseORMService.$transactioned() method? This is the default behavior, but if the ORMSession is flushed when the transaction is opened doesn’t that mean it has nothing to do with the code that called save() in the first place?
-
What is the appropriate pattern for working with our entities? Should we be wrapping everything in a transaction (aop?)? Should we be calling ORMClearSession() when we’ve decided not to save something? Should we turn off the default transaction in the BaseORMService and manually call ORMFlush()?
Thanks in advance for any help in understanding this.