Hi Hoang, The NHibernate session tracks the current active (not committed or rolled back) transaction.
If you call ISession.Transaction when no transaction has yet been created yet during the life time of the session, the session will create a new transaction object at that point in time, but won't begin it yet. When you call ISession.BeginTransaction, the session will see if their is already a transaction object that has been created before, but not yet completed. If so, it will return this transaction object. If not, it will create a new transaction object, begin it and store a reference to this new object. On transaction completion, the transaction object notifies the session to which it belongs that it has completed, on which the session will release its reference to the transaction object. Any following call to ISession.Transaction or ISession.BeginTransaction will then cause the creation of a new transaction object. NHibernate does not support reuse of transaction objects for more than one transaction (this behaviour may be different from Hibernate, which does seem to support reuse of transaction objects). Regards, Gerke. On 23 mrt, 07:59, Hoang Tang <[email protected]> wrote: > Recently I started to dig into how to improve the handling of transaction > handling in my code > > I notice that there are two way to start a transaction... > > ITransaction beginTransaction = Session.BeginTransaction(); > Or I can do Session.Transaction.Begin(); > > The interesting is that > > [Test] > public void NHibernateTransact1() > { > ITransaction beginTransaction = Session.BeginTransaction(); > ITransaction beginTransaction1 = Session.BeginTransaction(); > Assert.Equal(beginTransaction, beginTransaction1); // so if I > call begin transactiontwice.. it return the same transaction > > Assert.Equal(beginTransaction, Session.Transaction); // it also > return the same thing as Session.Transaction > } > > but if I do something like > > [Test] > public void NHibernateTransact2() > { > ITransaction beginTransaction = Session.BeginTransaction(); > ITransaction beginTransaction1 = Session.BeginTransaction(); > Assert.Equal(beginTransaction, beginTransaction1); > > Assert.Equal(beginTransaction, Session.Transaction); > beginTransaction.Rollback(); > > Assert.NotEqual(beginTransaction, Session.Transaction); //<--- > after the rollback they are not the same anymore? > > } > > for both test the Session is a brand new session from > SessionFactory.OpenSession(); > > So I am curious of what is the different between the two... and when should > I use one vs the other? > > Any help on this would be greatly appreciated > > Thanks, > > Hoang -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
