01-06-01 "Nhibernate (version 3.3.1.4000) in and out of the lake" business

Source: Internet
Author: User
Tags assert

Use of NHibernate transactions:

         Public voidAdd (Customer customer) {ISession session=_sessionmanager.getsession (); ITransaction Transaction = session.            BeginTransaction (); Try{session.                Save (customer); Session. Flush ();//clear first-level cache transaction.commit (); }            Catch(Exception) { transaction.            Rollback (); }            finally{session.            Close (); }        }

Test:

[TestMethod] [ExpectedException (typeof(NHibernate.Exceptions.GenericADOException))] Public voidtestaddtransation () {customerservice customerservice=NewCustomerService (); OrderService OrderService=NewOrderService (); Customer Customer1=NewCustomer () {FirstName="Test", LastName="TestAddTransation1", Age=Ten           }; Order Order1=NewOrder () {OrderDate= DateTime.Now.AddMinutes (1), Customer=Customer1}; Order Order2=NewOrder () {OrderDate= DateTime.Now.AddMinutes (2), Customer=Customer1}; Customer Customer2=NewCustomer () {FirstName=NULL,//database requirement cannot be null, now intentionally throwing an add exceptionLastName ="testaddtransation", Age=Ten           }; Order Order3=NewOrder () {OrderDate= DateTime.Now.AddMinutes (3), Customer=Customer2}; Order Order4=NewOrder () {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 and its Order1 and Order2 can be successfully inserted because they are not in the same business as Customer2Customerservice.add (CUSTOMER2);//Customer2 Insert failed, Order3 and ORDER4 cannot be inserted successfully. Assert.isnull (Customerservice.get (customer1.           CUSTOMERID)); Assert.isnull (Orderservice.find (order1.           ORDERID)); Assert.isnull (Orderservice.find (order2.       ORDERID)); }

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

About the addition of transactional operations:

Session. Flush ();//Clear first-level cache

Whether it will be saved to the database immediately

The conclusion is that the database will not be saved immediately, but wait until the call Transaction.commit () is actually saved to the database:

Graphic:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.