How to set Nhibernate LINQ Command Timeout using Session.Query
Asked Answered
P

1

7

Is anyone aware of a way to set the UnderlyingCriteria when using Session.Query?

I'm trying to set a more restrictive command timeout (or query timeout) for one specific query and I am trying to avoid adding that constraint on the connection or other querys in the session.

I've found in the old QueryOver functionality you could use something like this

// QueryOver returns a IQueryOver<T,T> an nHibernate class 
// with access to UnderlyingCriteria

var query = Session.QueryOver<Puppy>();
query.UnderlyingCriteria.SetTimeout(120); 

The problem with that is it's old, buggy, and just has a slew of functional issues.

Using Query returns an IQueryable<T>

 var query = (from c in Session.Query<Puppy>());

IQueryable is a MS class with no apparent access to command timeouts etc.

Another option would be to somehow set the sessions command timeout for all commands, at that point, then revert to the default, but I'm not seeing any public mechanism for doing this, beside setting the command timeout up front and leaving it so, like How to set timeout for NHibernate LINQ statement

Perak answered 2/1, 2014 at 19:32 Comment(0)
P
6

Never mind, found an example in Nhibernate's unit tests, they've added some extension methods to IQueryable.

var query = (from c in Session.Query<Puppy>()).Timeout(12);
Perak answered 2/1, 2014 at 19:45 Comment(3)
I find setting it globablly more helpful <property name="command_timeout">240</property>Ottillia
That's useful when you want to set the default for an entire application, but the goal behind the question was to change it for an individual command's timeout :)Perak
For recent code users (2024 is my current year) the equiv would be var query = (from c in Session.Query<Puppy>()).WithOptions(opt => opt.SetTimeout(12));Flotage

© 2022 - 2024 — McMap. All rights reserved.