How to turn off locking in Fluent NHibernate?

Go To StackoverFlow.com

4

I am running into the problem when I am importing data to a large table, the entire website goes down until the import is finished. this can take up to 3 hours for the insert/update process to finish. Also, if for some reason an exception is thrown the tables remain locked indefinitely until the application pool is restarted.

I looked into what was happening. Apparently the tables touched are locked until this process is finished. If this was direct SQL queries, I would add "with (NOLOCK)" to the query to solve the problem. However, I can't figure out how to tell Fluent NHibernate to add that to the queries it generates.

Also, is there a way when building the Session Factory that I can put in a setting to turn on "with (NOLOCK)" by default into all the queries?

Thanks in advance.

2012-04-05 15:05
by Michael D. Kirkpatrick
You need to set the lockmode.

http://stackoverflow.com/questions/1302746/how-do-add-nolock-with-nhibernat - rie819 2012-04-05 15:07

Are you using a stateless session?? This may help. http://davybrion.com/blog/2008/10/bulk-data-operations-with-nhibernates-stateless-sessions - CrazyCoderz 2012-04-06 13:08
Here is my solution by using SQL Interceptor. http://stackoverflow.com/questions/1302746/how-do-add-nolock-with-nhibernate/39518098#3951809 - Tola Ch. 2016-09-15 18:45


7

When you open your session or stateless session, begin a transaction with IsolationLevel.ReadUncomitted. This is the equivalent of specifying NOLOCK on all statements in that transaction.

using (var session = _sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction(IsolationLevel.ReadUncommitted))
{
    // do work

    transaction.Commit();
}
2012-09-26 10:21
by mattk
Matt, thanks for this solution. I am marking it as the solution since it does the trick as well. The solution I found was to add "SetLockMode(LockMode.None)" to my NHibernate query - Michael D. Kirkpatrick 2012-09-27 13:27
Ads