码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2014-06-07 05:27:18      阅读:446      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

 

Nhibernate事务的使用:

bubuko.com,布布扣
        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();
            }
        }

bubuko.com,布布扣

 

测试:

bubuko.com,布布扣
       [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));
       }
bubuko.com,布布扣

 

 

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

关于事务操作的添加:

  session.Flush();//清除一级缓存

是否会立即保存到数据库

结论是:不会立即保存的数据库,而是等到调用transaction.Commit();才真正保存到数据库:

图解:

bubuko.com,布布扣

 

bubuko.com,布布扣

 

01-06-01【Nhibernate (版本3.3.1.4000) 出入江湖】事务,布布扣,bubuko.com

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

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/easy5weikai/p/3755922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!