Making NHibernate play nice with TransactionScope

Another day with NHibernate, one more thing learnt…Ok, so if you”re going to use NH and you want it to play nice with the TransactionScope class introduced by .NET 2.0 and you”re using a provider which is capable on enlisting in an existing transaction (ex.: you”re connecting to SQL Server), then the only thing you need to remember is to call session.Flush before committing the trnasaction (ie, before calling the Complete method over the TransactionScope instance you”re using).

This is really important if you”ve got an entity which holds a collection of components. Fortunately for me, I”ve ended up finding this post by Oren which helped me putting it to work. You see, besides being an NH rookie, I was under the impression that if the session is associated with a transaction, then committing the transaction would automatically call Flush over that session”s instance. This seems to be true when you use an NH ITransaction, but it seems like it simply doesn”t work when you have component collections and you”re using TransactionScopes.


~ by Luis Abreu on January 29, 2008.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: