Fabio, My main environment is an ASP.NET application, using session per request pattern, with a bit of an abstraction on top of NH, so no, actual code is not a 100% match to the method I sketched up. The session is opened on first use (rather than explicitly) and is closed in an EndRequest handler. Page event handlers use TransactionScope, so the effective life cycle of an NH session looks more like this:
- new TransactionScope() - OpenSession - session.SaveOrUpdate - scope.Complete - session.Close This works, but I wanted to make sure I am not moving too far from the correct pattern. Based on Ayende's response, sounds like we should be OK. Right? Thank you! -Michael ________________________________ From: [email protected] [[email protected]] On Behalf Of Fabio Maulo [[email protected]] Sent: Thursday, February 18, 2010 3:21 PM To: [email protected] Subject: Re: [nhibernate-development] Re: AdoNetWithDistrubtedTransactionFactory Michael, Perhaps my questions does not sound as a support request but even so an answer will be appreciate and may help some user. 2010/2/18 Fabio Maulo <[email protected]<mailto:[email protected]>> questions: Are you managing the NH's session and trasaction at the same level of ambient transaction ? Where you are opening a NH's session and NH's transaction should you be aware of ambient transaction ? Where I'm managing the NH's session and NH's transaction I can't, and I wouldn't, be aware if it happen in an AmbientTransaction or not. Have you some place where the code in your app. look like that you sent us ? 2010/2/18 Michael Teper <[email protected]<mailto:[email protected]>> Fabio, Does that mean the correct pattern is: public void DoSomething() { using (var scope = new TransactionScope()) { var s = SessionFactory.OpenSession(); s.BeginTransaction(); var foo = s.Get<Foo>(); foo.Prop = "bar"; s.SaveOrUpdate(foo); s.CommitTransaction(); scope.Complete(); } } If so, why are BeginTransaction and CommitTransaction needed? They feel redundant... Thank you! -Michael ________________________________ From: [email protected]<mailto:[email protected]> [[email protected]<mailto:[email protected]>] On Behalf Of Fabio Maulo [[email protected]<mailto:[email protected]>] Sent: Thursday, February 18, 2010 6:53 AM To: [email protected]<mailto:[email protected]> Subject: Re: [nhibernate-development] Re: AdoNetWithDistrubtedTransactionFactory In order to use NH correctly you should: 1) throw away the session after an exception 2) use the NH's transaction Use NH's transaction mean: 1) open the session inside a transactionscope 2) after open the session begin a NH's transaction using session.BeginTransaction 2010/2/18 Sathish Naga <[email protected]<mailto:[email protected]>> The test is reflecting the way I use the NH session and transaction scope. When I run the test with the default transaction factory(AdoNetWithDistrubtedTransactionFactory), it fails on first session.Clear. When I override the Session Factory to use the AdoTransactionFactory, sample test didn't fail on first session.Clear(). I have the test as patch, if you want I can send/upload it. Thanks Sathish On Feb 17, 9:51 pm, Fabio Maulo <[email protected]<mailto:[email protected]>> wrote: > Where is the NH's transaction ? (ITransaction) > I mean... you said you have a issue > with AdoNetWithDistrubtedTransactionFactory but in your code you haven't use > it, so... where is the issue ? > > 2010/2/17 Sathish Naga <[email protected]<mailto:[email protected]>> > > > > > > > Hi, > > > I've a issue with NHibernate 2.1x and > > AdoNetWithDistrubtedTransactionFactory and SessionImpl. > > > To give you background of my Infrastructure, I have a NH session > > created per web request. > > > On Begin Request I create a Session and On End Request, I clear and > > dispose the NH session. > > > using (ISession s = sessions.OpenSession()) > > { > > s.FlushMode = FlushMode.Commit; > > > using (var tx = new > > TransactionScope(TransactionScopeOption.Required, > > new TimeSpan(0,0,0,0,10000))) > > > // > > Set timeout to 10 secs > > { > > try > > { > > s.Save(new Person { CreatedAt = DateTime.Now}); > > Thread.Sleep(20000); //Sleep for 20 secs to timeout > > the transaction > > s.Flush(); > > tx.Complete(); > > } > > catch (Exception ex) > > { > > //when this transaction fails, clear internal entity > > cache on > > session and move on > > s.Clear();// > > } > > } > > //This transaction expected to be successful > > using (var tx = new TransactionScope()) > > { > > try > > { > > s.Save(new Person { CreatedAt = DateTime.Now }); > > s.Flush(); > > tx.Complete(); > > } > > catch (Exception ex) > > { > > s.Clear(); > > } > > } > > } > > > As i reuse the same session for multiple transactions, when any of my > > transaction fails, I want to clear the session internal cache. In that > > case when the error is due to TransactionScope it fails on > > session.clear and brings whole application down. > > > Once JIRA is up, I'm going to add this defect. I have the test > > created, Looks like I cant attach the patch to this mail. > > -- > Fabio Maulo -- Fabio Maulo -- Fabio Maulo -- Fabio Maulo
