NHibernate ExecuteUpdate does not participate in current transaction?

240 views
Skip to first unread message

Brian Berns

unread,
May 15, 2012, 1:47:34 PM5/15/12
to nhu...@googlegroups.com

I have code that looks like this:

using (var session = this.sessionCreator.OpenSession())
using (var transaction = session.BeginTransaction())
{
    session
.SaveOrUpdate(anObject);
    session
.CreateSQLQuery(sql)
       
.ExecuteUpdate();
    transaction
.Commit();
}

Surprisingly, the SQL query executes before anObject is saved. Apparently, the ExecuteUpdatecommand does not participate in the current transaction. Is there any way to get the update to enlist in the transaction?

(Cross-posted from StackOverflow, where this had no responses.)

Oskar Berggren

unread,
May 15, 2012, 3:29:09 PM5/15/12
to nhu...@googlegroups.com
2012/5/15 Brian Berns <brian...@gmail.com>:
> I have code that looks like this:
>
> using (var session = this.sessionCreator.OpenSession())
> using (var transaction = session.BeginTransaction())
> {
>     session.SaveOrUpdate(anObject);
>     session.CreateSQLQuery(sql)
>         .ExecuteUpdate();
>     transaction.Commit();
> }
>
> Surprisingly, the SQL query executes before anObject is saved. Apparently,

It is not surprising. SaveOrUpdate() does NOT imply "execute sql now".


> the ExecuteUpdatecommand does not participate in the current transaction. Is
> there any way to get the update to enlist in the transaction?

I don't see how you can reach that conclusion based on the information
given. If you remove the call to Commit(), are the changes still
present in the database? I think it's just a matter of calling Flush()
on the session before ExecuteUpdate() if you need to guarantee that
any dirty state has been flushed to the DB before you SQL query. I
think NH can't do that automatically in this case.

/Oskar

Brian Berns

unread,
May 15, 2012, 4:09:02 PM5/15/12
to nhu...@googlegroups.com
That makes sense. I'll give it a try. Thanks.

-- Brian



--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


Reply all
Reply to author
Forward
0 new messages