views:

500

answers:

4

I have run into trouble using NHibernate when I am doing ajax calls using JQuery.

I get different exceptions regarding either the connection or reader being closed like:

"Invalid attempt to call Read when reader is closed".

Does anybody know how to fix this?

A: 

Check you're initializing your connection properly and not closing it from .NET before you try to read something.

It seems you're trying to get info while it's not opened yet or you already closed it.

Finally, have you tried to do some AJAX with your app before, using other frameworks or XMLHttpRequest object directly? If not, give it a try. If problem still persists, you can be sure the problem is on the server side.

Seb
The call works perfectly when im not calling using ajax - so the question is if someone knows why this is different when using ajax and what to do about it.Im not handeling my connections myself, and my ajax as suck works just fine - its a problem with NHibernate in an ajax context it seems.
From the error message it sounds like there has to be an attempt to read after the session is already closed, so check your assumptions in the code. The context for the page may be different in an AJAX call if parameteres (for example) are different. Some code and more info would be helpful. :)
Stuart Childs
A: 

Well the call is done with the same parameter, and all I can really show you is a basic Linq to NHibernate call to get an employee by username == name - where name is the param passed in.

So it is just:

return Session.Linq().Single(e => e == "John");

I have had a breakpoint on this line and it works perfectly, untill it is called from an action that is calld using JQuery.getJSON

I also have other calls that are the same kind, and they also seem to fail in this context.

Christian Nielsen
A: 
protected object GetJsonUserWrong(string name) {
    var user = default(User);
    using(var session = this.SessionFactory.Open()) {
        user = session.Linq<User>().Single(e => e.Name == name);
    }
    //doing the serialization while the session is closed might not work
    //if NHibernate is using proxy objects, this can fail.
    return SerializeToJson(user);
}

protected object GetJsonUserRight(string name) {
    using(var session = this.SessionFactory.Open()) {
        //must do the serialization while the session is open
        return SerializeToJson(session.Linq<user>().Single(e => e.Name == name));
    }
}
Justice
A: 

I am having same problem with asp.net mvc2 RTM. I didn't have this problem with asp.net MVC2 RC. This problem occurs on local machine, I didn't try it when it is deployed to a web server.

Ivan