Nhibernate Multiple Database in One Transaction

2019-08-10 03:50发布

问题:

My program access to 2 databases and do some DB works. I want to do these works in one transaction and if one of them get any error, also another one should NOT be committed.

Here where I am right now, the simple code of a program.

using (var transactionScope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
{
            using (var session1 = NHibernateHelper.OpenSession1())
            {
                using (var session2 = NHibernateHelper.OpenSession2())
                {
                    using (var trnx1 = session1.BeginTransaction())
                    {
                        using (var trnx2 = session2.BeginTransaction())
                        {

                            session2.Save(bla bla);
                            session1.Update(bla bla);
                            trnx2.Commit();
                            trnx1.Commit();
                        }
                    }

                }
            }
            transactionScope.Complete();
}

I want to that if something wrong in trnx1, trnx2 should be rolled-back. Databases are in same Server.