How to handle NHibernate session time using services?

In this question I asked about the duration of the NHibernate session. I use a desktop application, but with a separation between the client and the server, so the conclusion is that I will use one session for each server request, since all the magic of NHibernate happens on the server side.

Now my problem is how to handle this. I had problems before loading the reference data when the session was prematurely closed. The problem is that when debugging, I see the following in my reference classes - so the reference data is not yet loaded:

base {NHibernate.HibernateException} = {"Initialization [MyNamespace.Foo # 14] - failed to lazily initialize the role collection: MyNamespace.Foo.Bars, session or session closed"}

From what I understand, it does not load everything, even if I commit a transaction. So, I found out that I need to keep the session open for a while, but for how long?

My question is mainly if I handle all my life correctly, or what I have to change to be on the right track. Honestly, I donโ€™t see how this could be wrong, so I would really like it to call a function to provide reference data. I do not use lazy loading, so I thought they would be downloaded immediately.?

Current architecture: using the class "service behavior" that performs the transaction. This is IDisposable, therefore the service itself uses a using-sentence around it. NHibernateSessionFactory provides a static factory, which therefore will be reused.

// 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; } } 
+7
c # session nhibernate lazy-loading
source share
2 answers

Turns out I'm doing lazy loading all the time. 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 suggested that Not.LazyLoad() turned off lazy loading, but apparently not for reference objects. I added lazy loading by reference and this seems to fix the problem.

 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(); // <---------- } } 

Thank you for your time, and I will continue to be glad to see your opinion on whether this structure is reasonable.

+5
source share

If xml files are used for matching purposes, we can set lazy = false for the package.

-one
source share

All Articles