CF9 ORM TransactionAdvice

As with any new persistence technology, CF9's ORM functionality has necessitated porting my TransactionAdvice for ColdSpring.  The previous "CFML" version was really "CFML/SQL/ORM" and is still the way to go if you're not using ORM functionality.  The new version (cf9ormtransactionadvice.cfc) is specific to ORM applications on CF9.0.  It will not provide transactionality for SQL-based apps as it piggybacks on Hibernate's transaction layer, not the underlying JDBC one.

With CF9, Adobe made a rather fundamental flaw in the transaction implementation around ORM (which they've fixed, by the way) because a transaction (CFTRANSACTION or the new transaction block in CFSCRIPT) would close the current Hibernate session and then open a new one.  This made dealing with transactions hugely problematic because you'd constantly be left with detached persistent instances that you wanted to lazy load a relationship on but couldn't because the session had already been closed.  The solution is either to use entityMerge everywhere or skip the CFML transaction demarcation and use Hibernate's directly.  The latter is how the transaction advice works.

The mechanism is identical to the other versions, though this one is a bit simpler because CF9 finally grew a CFFINALLY tag!  Yay!  It also supports conditional logging of the transaction boundaries which, when combined with this.ormsettings.logSql = true, can assist greatly in debugging transaction problems.  But the gist of it is really simple and uninteresting, excatly the way transaction demarcation should be.

When the new session stuff is released for public consumption by Adobe, using the existing generic CFML/SQL transaction advice will be a better alternative to this.  It is useful for both ORM and plain SQL applications, and can even be used to properly protect hybrid apps where you use a mix of ORM and raw SQL queries.  The CF9 ORM version only does ORM.

And don't forget to disable flushAtRequestEnd.

Comments are closed.