Leonardo Ferrari Leonardo Ferrari - 2 months ago 25
C# Question

Dialect/Driver - Every SELECT I perform, add with(nolock)

I need to know a way to implement in my system, a Driver or Dialect which, whenever I perform a SELECT in Nhibernate, the SELECT adds the with(nolock) with it.
I need that to be in C# and NHibernate, not directly in the DB !

Hope you can understand !

Thanks !

Answer

It is possible to modify the sql using an Interceptor and overriding the OnPrepareStatement method, something like this:

public class AddNoLockHintsInterceptor : EmptyInterceptor
{
    public override SqlString OnPrepareStatement(SqlString sql)
    {
        // Modify the sql to add hints

        return sql;
    }
}

And here is a way to register the interceptor with NHibernate:

var session = SessionFactory.OpenSession(new AddNoLockHintsInterceptor());