3

I'm using NHibernate behind my ASP.NET MVC application and I've come across a frustrating problem when trying to save an object via an AJAX call. I am getting the usual:

Failed to lazily initialize a collection of role: [type] no session or session was closed

The problem is, as far as I can tell the session is not closed. I'm using an HttpModule to handle my sessions per the session per request pattern with NHibernate set to use the web current_session_context_class. Here's the HttpModule:

public class NHHttpModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.EndRequest += ApplicationEndRequest;
        context.BeginRequest += ApplicationBeginRequest;
    }

    public void ApplicationBeginRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Bind(SessionFactory.GetNewSession());
    }

    public void ApplicationEndRequest(object sender, EventArgs e)
    {
        var currentSession = CurrentSessionContext.Unbind(SessionFactory.GetSessionFactory());
        currentSession.Close();
        currentSession.Dispose();
    }

    public void Dispose()
    {
        // Do nothing
    }
}

My SessionFactory is pretty standard, too:

public static class SessionFactory
{
    private static ISessionFactory _sessionFactory;

    private static void Init()
    {
        _sessionFactory = Fluently.Configure() //Lots of other stuff here for NH config
            .BuildSessionFactory();
    }

    public static ISessionFactory GetSessionFactory()
    {
        if (_sessionFactory == null)
            Init();

        return _sessionFactory;
    }

    public static ISession GetNewSession()
    {
        return GetSessionFactory().OpenSession();
    }

    public static ISession GetCurrentSession()
    {
        return GetSessionFactory().GetCurrentSession();
    }
}

I'm using a unit of work for transactions, which is why I don't open a transaction at the BeginRequest. Even so, I've tried that with no change in results.

I'm trying to save a Comment object to a User object via an AJAX post. Here is the controller code:

    [HttpPost, ValidateInput(false)]
    public ActionResult CreateCommentAsync(CommentCreateViewModel model)
    {
        if (!model.UserId.HasValue)
            return Content("false");

        var svc = DependencyResolver.Current.GetService<IPartnerUserService>();
        var user = svc.FindBy(model.UserId.Value, UserContext.Current.ActiveUser);

        // I put this in here as a test -- it throws the no-session error, too.            
        var count = user.Comments.Count();

        var comment = new Comment();
        comment.CommentText = model.CommentText;
        comment.DateCreated = DateTime.UtcNow;
        comment.CreatedBy = UserContext.Current.ActiveUser;

        // This is the original source of the error
        user.Comments.Add(comment);
        svc.Save(user, UserContext.Current.ActiveUser);

        return Content("true");
    }

I have debugged the application and confirmed that a session is created at the beginning of the request, and, most confusing, the SessionFactory.GetCurrentSession().IsOpen is true, even when I hit a breakpoint for the errors listed above.

Furthermore, the Comments list is populated when I render a view that displays a list of comments. I can't figure out why it's failing when I add it.

As if that weren't enough, every once in a while, with no changes to the code, I don't get the error and can successfully add a comment. I'm pulling my hair out...any ideas? This is certainly a session management issue, but I've gone over everything I can find online and by all accounts the way I'm doing session management is ok. Any ideas?

UPDATE: I've tried a few additional tests, most notably whether the current session has the user object I'm trying to manipulate. It does. When I test like this:

if (!SessionFactory.GetCurrentSession().Contains(user))
    SessionFactory.GetCurrentSession().Refresh(user);

I get a result of true on the condition.

A commenter requested the code on the service, but for that particular call it doesn't touch a session, it just verifies that the requesting user has permissions then sets up the detached criteria. The repository is then called within that service, and here's that code:

public IEnumerable<T> FindBy(DetachedCriteria detachedCriteria) //Infrastructure.Querying.Query query)
{
    return detachedCriteria.GetExecutableCriteria(SessionFactory.GetCurrentSession()).Future<T>();
}

The reason I don't think this code is the problem is that it's exactly the same code called for the details view. I don't have any lazy loading errors when I display the comments, and I do it the same way - I use the service to load the user object then do a foreach iteration through the list. I've NEVER had a problem doing that.

In case this was some sort of issue with the AJAX call, I also changed it to a full postback, but still got the same error.

I can't for the life of me figure out what's going on here.

6
  • Please post what "svc.FindBy(model.UserId.Value, UserContext.Current.ActiveUser);" actually do. The inner logic. You would get this error if the user was selected from another session, on a previous request. Commented Jun 1, 2011 at 8:09
  • Added the relevant code. Commented Jun 1, 2011 at 14:27
  • The method I asked about takes 2 arguments, you posted a medthod that take 1 DetachedCriteria. You probably have a specific service for IPartnerUserService that has another method signature. You pasted the generic one. Commented Jun 1, 2011 at 15:24
  • I noticed the Async on the method name. Have you tested this with a plain controller instead of an async controller? Do you have anything that could be running on another thread and occasionally closing or disposing your session? Commented Jun 1, 2011 at 15:36
  • Oh, I see what you mean now, but you return a IPersonUser, and I want to know how that works. Also, what is CurrentSessionContext? What does Bind do? Commented Jun 1, 2011 at 15:54

3 Answers 3

1

I finally discovered the reason for this error, but only by dumb luck. I'll post the resolution in case it helps someone, but I can't really offer any explanation why and will probably post a new question to see if someone can clear the air.

You'll note in my code I was calling my service like this:

var user = svc.FindBy(model.UserId.Value, UserContext.Current.ActiveUser);

That UserContext object is a static class with a session-stored Current instance that contains a property which is the current user record NHibernate object. Because of how NHibernate proxies properties, that UserContext.ActiveUser property had to do something like this whenever it was called:

if (!SessionFactory.GetCurrentSession().Contains(ActiveUser))
    SessionFactory.GetCurrentSession().Refresh(ActiveUser);

For some reason, it was this refresh process that was screwing things up. When I explicitly retrieved the active user instead of using the UserContext class, everything worked fine.

I've since changed how I retrieve the active user so that it's not using a session-stored instance and it's working fine. I wish I knew exactly why, but I don't!

Sign up to request clarification or add additional context in comments.

Comments

0

It is possible that there is a problem with the collection mapping. Are you sure mapping is correct and entities are properly binded?

Check one-to-many and many-to-one and inverse attribute.

updated

have you tried to attach "user" entity with session.Lock(..)?

I can suggest you to try to save single user entity or single comment entity to test if the problem comes from comment collection.

4 Comments

Yes, I'm sure the mapping is correct. Not only can I access the list of Comments in a Details view, but I can intermittently add a comment without an error. I'm pretty sure this is a session issue.
The problem is that my user object is created in the MVC app and therefore doesn't have direct knowledge of the session. I don't have a repository set up for the comment since it's not an aggregate root. I'd like to see if I can fix the session management before I go changing how my entities are related, but I'll keep that in mind as a last resort.
so comments are not mapped in the repository as a user collection? Is this the problem?
They are mapped and they can be saved 1) intermittently in the web application and 2) all the time in a console application with current_session_context_class set to call. It's not the mapping, it's session management, I'm almost positive.
0

This exception is thrown also when you want to do something with session in the view.
Check this thread

4 Comments

Thanks, that's one of the threads I checked. My problem is different - lazy loading is failing in the same context. So I'm not loading the entity and disposing of the session then trying to get at the lazy loaded properties. I'm loading and immediately trying to lazy load, when debugging shows the session is still active.
Maybe you should try to get all comments to create proxy sth like session.createcriteria<Comment>(); before trying to add new one?
If that's the case then I lose all the benefit of a rich domain model with NH. I would rather find the core problem than go implementing workarounds.
I am sorry but I don't have another idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.