haf / Castle.Facilities.NHibernate

The NHibernate Facility supercedes NHibernateIntegration and integrates with the new Transaction support. Please read http://stackoverflow.com/questions/4010265/how-to-let-nhibernate-retry-deadlocked-transactions-when-using-session-per-reque before using, it will help you.
https://github.com/haf/Castle.Facilities.NHibernate/wiki
Apache License 2.0
15 stars 23 forks source link

SessionPerWebRequest never commits #12

Closed PandaWood closed 9 years ago

PandaWood commented 11 years ago

Hi,

SessionPerWebRequest doesn't seem to be generating any transactions. While calling Session.Save() generally ends up persisting something, calls to Delete() and updating objects fail to do anything. No errors. No deleting. Nothing.

My setup/config is almost identical to that in the per_web_request_soec,cs in the code base (as below)

_container.Register(Component.For<INHibernateInstaller>().ImplementedBy<NHibernateInstaller>())
          .AddFacility<AutoTxFacility>()
          .AddFacility<NHibernateFacility>(fac => fac.DefaultLifeStyle = DefaultSessionLifeStyleOption.SessionPerWebRequest);

However, I've just found basic read/writes fail to commit.

When I call my injected sessionManager.OpenSession().Delete(obj) on a correct and valid object, it fails to delete the record. No DB command is issued.

If I change to the code to call a Session.CreateQuery("delete bla where bla") it works! So the setup is basically right, it's as if the SessionPerWebRequest just isn't working properly.

Any config that's not mentioned anywhere in the wiki that I could have missed?

PandaWood commented 11 years ago

Oh bugger, I didn't do like this var lifestyle = new PerWebRequestLifestyleModule(); lifestyle.Init();

Which seems very likely to be my problem. Though I did already have this setup in my web config for use in Windsor, in general. <add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />

Is it possible that this web.config registration doesn't cut it for NHibernateFacility?

PandaWood commented 11 years ago

No, sorry. Adding the PerWebRequestLifestyleModule, explicitly in code, didn't fix the problem that when I call Session.Delete(obj), within a controller method, nothing happens. No transaction is committed.

PandaWood commented 11 years ago

If I call container.ResolveScope().Service.GetSessionImplementation().TransactionInProgress in my controller method, it returns false and TransactionContext is null. I just don't see a simple reason why it wouldn't be doing transactions If I take out the SessionPerWebTransaction, and so I'm left without any [Transaction] attributes, it shows the sort of error message I would expect "No transaction in context when trying to instantiate model 'NHibernate.ISession'" So it's obviously fundamentally setup correctly before the problem that Session.Delete(obj) just flat out doesn't work.

PandaWood commented 4 years ago

BTW I never got this to work over several days of effort. Then later my experienced colleague tried and failed after several days of effort as well. We were already using the previous version of NHibernateIntegration without issues - which makes this so much more perplexing. But we were never able to upgrade to this - as far as we are concerned, it just doesn't work, Sorry.