plalx
plalx

Reputation: 43718

Mix raw queries and ORM operations within the same cftransaction

Is it safe to mix raw queries using cfquery and ORM operations within the same cftransaction tag?

<cftransaction>
    <cfquery datasource="test">...</cfquery> <!--- A --->
    <cfset var e = entityLoadByPK('SomeEntity', someId)> <!--- B --->
    <cfset e.setSomeProperty('test')> <!--- C --->
    <cfquery datasource="test">...</cfquery> <!--- D --->
</cftransaction>

By having a look at the SQL profiler, it seems that the queries won't get executed sequentially, they appear in the profiler in the following order: A, B, D, C. From what I understand, this could happen because the hibernate session will get flushed only when it reaches the closing cftransaction, is that correct?

I have noticed that because of another issue wich I will ask a in another question. But basically, I have to disable triggers on a specific table before running the ORM statement or it will result in a StaleStateException.

When having the ORM operation between DISABLE TRIGGER and ENABLE TRIGGER statements within the same cftransaction, it did not solve the issue, because ENABLE TRIGGER ran before the ORM query. However, when using the following, it works:

<cfquery datasource="test">
    DISABLE TRIGGER ALL ON Test;
</cfquery>

<cftransaction>
    <!--- ORM CODE --->
    <!--- Other cfquery --->
</cftransaction>

<cfquery datasource="test">
    ENABLE TRIGGER ALL ON Test;
</cfquery>

I also tried to use ormFlush() right after the ORM statements, but it did not worked. Is there a way to mix raw queries and ORM statements within the same cftransaction and having the statement to run sequentially? Maybe I am misunderstanding the issue completely, but I am quite puzzled right now.

Upvotes: 2

Views: 223

Answers (1)

Jan Br&#252;nemann
Jan Br&#252;nemann

Reputation: 229

Try using ORMExecuteQuery.

ORMExecuteQuery("update SomeEntity set SomeProperty = 'test' where id = :id",
  {"id"="someId"});`

These get executed right away and skip the ORM session.

Upvotes: 0

Related Questions