views:

1407

answers:

2

Long title, I know but I searched all over and couldn't find that error message coming from that function call so I thought this might be more useful.

This is the code snippet:

string hql = " from LabRequest r where 1 = 1 ";
hql += " and 0 < (select count(rs) ";
hql += "            from r.Statuses rs ";
hql += "           where rs.StatusType.Description IN ('Assigned','Submitted')";
hql += "         ) ";
//Session.Clear();          
IQuery query = Session.CreateQuery(hql);
IQueryable<LabRequest> requests = query.List<LabRequest>().AsQueryable<LabRequest>();

This is a function (or most of it) in my Data Access Object in an MVC app I'm working on. It's for a search page and when the page runs this function gets called exactly like you see in the code and works.

Then, without changing anything, I refresh the page which goes through the same steps and calls this code, exactly as you see it, again. But the second time through it crashes on the query.List() portion of the last line with the error in the subject.

Session is defined in another DAO as:

session = NHibernateHelper.GetCurrentSession();

I know this is hard to analyze without the actual DB but I just wanted to see if anyone could maybe point me in the right direction, or maybe point out something obvious about NHibernate since I know basically nothing about it.

Edit: forgot to mention that when I uncomment the Session.Clear() it works fine, so was thinking the answer has something to do with that, and if it does how I should handle when to clear()?

Edit 2: This is part of the answer, but I call a very similar function prior to this one the second time around. What I can't figure out is why that one is affecting the one I posted. The 'query' variable is local, so it seems to be something with Session.CreateQuery. Anyone know what that would be?

Thanks, Jeff

A: 

While I'm not sure why exactly it seems the 'Statistics' property on the Session has data on it from the first query and I think this is what's causing the error because if I do a Session.Clear it removes the collections in the Statistics property.

As my current, and possibly temporary fix, I just created an extension method for the CreateQuery function that takes a bool asking whether to clear the Session and am just using this instead of the one provided.

If anyone else has any real answer to this please add it.

Jeff Keslinke
A: 

Relating to your "Edit 1" and "Edit 2" notes, yes, it has to do with the session that is shared (assuming you're using one of the standard methods of handling sessions in NHibernate).

Is there a good reason for using Session.Clear()? In general, Clear is only used after a flush, to make sure the Session cache doesn't get too big causing a performance hit. Are you using it that way, or for some business reason not mentioned in your question?

Mufasa
I'm using it because it's the only way that I can get it to work. In essensce that's what I'm asking is how should I be handling it instead?
Jeff Keslinke
Oh, I read it wrong--the use of Clear makes it work--not the opposite. My bad. I figure it has to do with how you're managing the Session lifetime. What method are you using? ThreadLocal, etc.?
Mufasa