zoukankan      html  css  js  c++  java
  • NHibernate系列文章十四:NHibernate事务

    摘要

    NHibernate实现事务机制非常简单,调用ISession.BeginTransaction()开启一个事务对象ITransaction,使用ITransaction.Commit()提交事务,使用ITransaction.Rollback()回滚事务。

    标准NHibernate事务代码:

     1        using (var session = SessionFactory.OpenSession())
     2         {
     3             using (var transaction = session.BeginTransaction())
     4             {
     5                 try
     6                 {
     7                     // some date operaton codes here
     8                     transaction.Commit();
     9                 }
    10                 catch (HibernateException e)
    11                 {
    12                     transaction.Rollback();  // or log exception
    13                 }
    14             }
    15         }

    也可以使用下面的代码:

     1         var session = SessionFactory.OpenSession();
     2         var transaction = session.BeginTransaction();
     3         try
     4         {
     5             try
     6             {
     7                 // some date operaton codes here
     8                 transaction.Commit();
     9             }
    10             catch (HibernateException e)
    11             {
    12                 transaction.Rollback();
    13                 throw e; // or log exception
    14             }
    15             finally
    16             {
    17                 transaction.Dispose();
    18             }
    19         }
    20         finally
    21         {
    22             session.Close();
    23         }

    上面两段落的代码是等效的。

  • 相关阅读:
    msp430入门编程25
    msp430入门编程24
    msp430入门编程23
    msp430入门编程22
    msp430入门编程21
    msp430入门编程20
    msp430入门编程16
    msp430入门编程15
    msp430入门编程14
    msp430入门编程13
  • 原文地址:https://www.cnblogs.com/uncle_danny/p/5648084.html
Copyright © 2011-2022 走看看