01-06-01【Nhibernate (版本3.3.1.4000) 出入江湖】事務

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

 

Nhibernate事務的使用:

        public void Add(Customer customer)        {            ISession session = _sessionManager.GetSession();            ITransaction transaction = session.BeginTransaction();            try            {                session.Save(customer);                session.Flush();//清除一級緩衝                                transaction.Commit();            }            catch (Exception)            {                transaction.Rollback();            }            finally            {                session.Close();            }        }

 

測試:

       [TestMethod]       [ExpectedException(typeof(NHibernate.Exceptions.GenericADOException))]       public void TestAddTransation()       {           CustomerService customerService = new CustomerService();           OrderService orderService = new OrderService();           Customer customer1 = new Customer()           {                FirstName = "Test",               LastName = "TestAddTransation1",               Age = 10           };           Order order1 = new Order()           {               OrderDate = DateTime.Now.AddMinutes(1),               Customer = customer1           };           Order order2 = new Order()           {               OrderDate = DateTime.Now.AddMinutes(2),               Customer = customer1           };           Customer customer2= new Customer()           {               FirstName = null,  //資料庫要求不能為null,現在故意引發添加異常               LastName = "TestAddTransation",               Age = 10           };           Order order3 = new Order()           {               OrderDate = DateTime.Now.AddMinutes(3),               Customer = customer2           };           Order order4 = new Order()           {               OrderDate = DateTime.Now.AddMinutes(4),               Customer = customer2           };           customer1.Orders.Add(order1);           customer1.Orders.Add(order2);           customer2.Orders.Add(order3);           customer2.Orders.Add(order4);           customerService.Add(customer1); //customer1及其Order1與Order2能成功插入,因為他們與Customer2不在同一個事務           customerService.Add(customer2); //Customer2插入失敗,Order3和Order4也不能成功插入。           Assert.IsNull(customerService.Get(customer1.CustomerId));           Assert.IsNull(orderService.Find(order1.OrderId));           Assert.IsNull(orderService.Find(order2.OrderId));       }

 

 

-----------------------------------------------

關於事務操作的添加:

  session.Flush();//清除一級緩衝

是否會立即儲存到資料庫

結論是:不會立即儲存的資料庫,而是等到調用transaction.Commit();才真正儲存到資料庫:

圖解:

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.