Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 529221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:04:08+00:00 2026-05-13T09:04:08+00:00

In this question I asked about NHibernate session lifetime. I’m using a desktop application,

  • 0

In this question I asked about NHibernate session lifetime. I’m using a desktop application, but with client/server separation, so the conclusion is that I will use one session per server request, as the server side is where all the NHibernate magic happens.

My problem now is how to handle it. I’ve had problems before with loading of referenced data when the session is prematurely closed. The problem is that I see the following on my referenced classes when debugging – hence the referenced data isn’t loaded yet:

base {NHibernate.HibernateException} = {“Initializing[MyNamespace.Foo#14]-failed to lazily initialize a collection of role: MyNamespace.Foo.Bars, no session or session was closed”}

From what I understand it doesn’t load all even though I commit the transaction. So I’ve learned that I need to keep my session open for a while, but how long?

My question is basically if I’m handling the lifetime properly, or what I should change to be on the right track. Honestly I can’t see how this can be wrong, so what I’d really like is a function call to ensure that the referenced data is fetched. I’m not using lazy loading, so I thought they would be loaded immediately..?

Current architecture: Using a “service behavior” class that does the transaction. This is IDisposable, so the service itself it using a using-clause around it. The NHibernateSessionFactory provides a static factory which hence will be resused.

// This is the service - the function called "directly" through my WCF service. 
public IList<Foo> SearchForFoo(string searchString)
{
    using (var serviceBehavior = new FooServiceBehavior(new NhibernateSessionFactory()))
    {
        return serviceBehavior.SearchForFoo(searchString);    
    }                        
}

public class FooServiceBehavior : IDisposable
{
    private readonly ISession _session;

    public FooServiceBehavior(INhibernateSessionFactory sessionFactory)
    {
        _session = sessionFactory.OpenSession();                
    }

    public void Dispose()
    {
        _session.Dispose();
    }

    public IList<Foo> SearchForFoo(string searchString)
    {           
        using (var tx = _session.BeginTransaction())
        {
            var result = _session.CreateQuery("from Foo where Name=:name").SetString("name", searchString).List<Name>();
            tx.Commit();
            return result; 
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:04:09+00:00Added an answer on May 13, 2026 at 9:04 am

    It turns out I’m doing lazy loading after all. I had the following mapping:

    public class FooMapping : ClassMap<Foo>
    {
        public FooMapping()
        {
            Not.LazyLoad();
            Id(c => c.Id).GeneratedBy.HiLo("1");
            Map(c => c.Name).Not.Nullable().Length(100);
            HasMany(x => x.Bars).Cascade.All();
        }
    }
    

    I assumed the Not.LazyLoad() disabled lazy loading, but apparently not for referenced objects. I added lazy loading on the reference, and this seems to have fixed the issue.

    public class FooMapping : ClassMap<Foo>
    {
        public FooMapping()
        {
            Not.LazyLoad();
            Id(c => c.Id).GeneratedBy.HiLo("1");
            Map(c => c.Name).Not.Nullable().Length(100);
            HasMany(x => x.Bars).Not.LazyLoad(); // <----------
        }
    }
    

    Thanks for your time, and I’ll still be glad to see your opinions to whether my given structure is reasonable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: I originally asked this question about an hour ago but only recently realized
This question has been asked in a C++ context but I'm curious about Java.
I recently asked a question about using Fluent NHibernate with .NET 4 - I
I previously asked this question about how to make versioned serializaion using boost::serialization::access I
I have asked this question about using the a Linq method that returns one
In this question , I asked about using queue's and threads in C#. I
I know this question has been asked about a dozen times, but this one
This question follows on from a question I asked yesterday about why using the
Someone asked this question about a year ago but I was unable to turn
I just asked this question about using a regular expression to allow numbers between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.