zoukankan      html  css  js  c++  java
  • 简单的NHibernate helper类,支持同一事务的批量数据处理

    今天为了处理批量数据操作写了个简单的NHibernate helper类,支持同一事务的批量数据处理.

    转载自:http://www.cnblogs.com/rayman/archive/2005/03/27/126702.aspx

    using System;
    using System.Threading;
    using System.Collections;
    using System.Collections.Specialized;

    using Nullables;
    using Nullables.NHibernate;
    using NHibernate;
    using NHibernate.Cfg;


    namespace NHibernate.Utils {
        
    /// <summary>
        
    /// 简单的NHibernate Helper类。支持同一事务内的批量数据处理。
        
    /// </summary>

        public class NHHelper {

            
    local variables

            
    singleton pattern

            
    transaction relax
            
            
    #region public methods - CRUD
            
    public void Add( object entity ) {
                
    bool autoCommit = true;

                ISession session;
                
                
    lockthis._sessionMap.SyncRoot ){
                    
    ifthis._sessionMap[Thread.CurrentThread] != null ) {
                        session 
    = (ISession)this._sessionMap[Thread.CurrentThread];
                        
    if(  session.Transaction == null || session.Transaction.WasCommitted || 
                            session.Transaction.WasRolledBack ) 
    {
                            autoCommit 
    = true;
                        }
     else {
                            autoCommit 
    = false;
                        }

                    }
     else {
                        session 
    = this._sessionFactory.OpenSession();
                        session.BeginTransaction();
                    }

                }


                
    try {
                    session.Save( entity );
                    
    if( autoCommit ) {
                        session.Transaction.Commit();
                        session.Close();
                    }

                }
     catch (Exception ex) {
                    
    try {
                        session.Transaction.Rollback();
                    }
     catch {
                    }
     finally {
                        session.Close();
                    }

                    
    throw ex;
                }

            }



            
    public void Update( object entity, object key ) {
                
    bool autoCommit = true;

                ISession session;
                
                
    lockthis._sessionMap.SyncRoot ){
                    
    ifthis._sessionMap[Thread.CurrentThread] != null ) {
                        session 
    = (ISession)this._sessionMap[Thread.CurrentThread];
                        
    if(  session.Transaction == null || session.Transaction.WasCommitted || 
                            session.Transaction.WasRolledBack ) 
    {
                            autoCommit 
    = true;
                        }
     else {
                            autoCommit 
    = false;
                        }

                    }
     else {
                        session 
    = this._sessionFactory.OpenSession();
                        session.BeginTransaction();
                    }

                }


                
    try {
                    session.Update( entity, key );
                    
    if( autoCommit ) {
                        session.Transaction.Commit();
                        session.Close();
                    }

                }
     catch (Exception ex) {
                    
    try {
                        session.Transaction.Rollback();
                    }
     catch {
                    }
     finally {
                        session.Close();
                    }

                    
    throw ex;
                }
            
            }



            
    public void Delete( object entity ) {
                
    bool autoCommit = true;

                ISession session;
                
                
    lockthis._sessionMap.SyncRoot ){
                    
    ifthis._sessionMap[Thread.CurrentThread] != null ) {
                        session 
    = (ISession)this._sessionMap[Thread.CurrentThread];
                        
    if(  session.Transaction == null || session.Transaction.WasCommitted || 
                            session.Transaction.WasRolledBack ) 
    {
                            autoCommit 
    = true;
                        }
     else {
                            autoCommit 
    = false;
                        }

                    }
     else {
                        session 
    = this._sessionFactory.OpenSession();
                        session.BeginTransaction();
                    }

                }


                
    try {
                    session.Delete( entity );
                    
    if( autoCommit ) {
                        session.Transaction.Commit();
                        session.Close();
                    }

                }
     catch (Exception ex) {
                    
    try {
                        session.Transaction.Rollback();
                    }
     catch {
                    }
     finally {
                        session.Close();
                    }

                    
    throw ex;
                }
            
            }



            
    public void Save( object entity ) {
                
    bool autoCommit = true;

                ISession session;
                
                
    lockthis._sessionMap.SyncRoot ){
                    
    ifthis._sessionMap[Thread.CurrentThread] != null ) {
                        session 
    = (ISession)this._sessionMap[Thread.CurrentThread];
                        
    if(  session.Transaction == null || session.Transaction.WasCommitted || 
                            session.Transaction.WasRolledBack ) 
    {
                            autoCommit 
    = true;
                        }
     else {
                            autoCommit 
    = false;
                        }

                    }
     else {
                        session 
    = this._sessionFactory.OpenSession();
                        session.BeginTransaction();
                    }

                }


                
    try {
                    session.SaveOrUpdate( entity );
                    
    if( autoCommit ) {
                        session.Transaction.Commit();
                        session.Close();
                    }

                }
     catch (Exception ex) {
                    
    try {
                        session.Transaction.Rollback();
                    }
     catch {
                    }
     finally {
                        session.Close();
                    }

                    
    throw ex;
                }
            
            }



            
    public void Save( IList entities ) {
                
    this.BeginTransaction();

                
    try {
                    
    foreachobject entity in entities ) {
                        
    this.Save(entity);
                    }

                    
                    
    this.CommitTransaction();
                }
     catch (Exception ex) {
                    
    this.RollbackTransaction();
                    
    throw ex;
                }

            }



            
    public void Save( params object[] entities ) {
                
    this.BeginTransaction();

                
    try {
                    
    foreachobject entity in entities ) {
                        
    this.Save(entity);
                    }

                    
                    
    this.CommitTransaction();
                }
     catch (Exception ex) {
                    
    this.RollbackTransaction();
                    
    throw ex;
                }

            }

            
            
            
    public object Get( Type entityType, object id ) {
                
    object entity = null;
                
                
    bool closeSession = true;
                ISession session;
                
                
    lockthis._sessionMap.SyncRoot ){
                    
    ifthis._sessionMap[Thread.CurrentThread] != null && 
                        ((ISession)
    this._sessionMap[Thread.CurrentThread]).IsOpen ) {
                        session 
    = (ISession)this._sessionMap[Thread.CurrentThread];
                        closeSession 
    = false;
                    }
     else {
                        session 
    = this._sessionFactory.OpenSession();
                        closeSession 
    = true;
                    }

                }


                
    try {
                    entity 
    = session.Get( entityType, id );
                }
     catch(Exception ex)  {
                    closeSession 
    = true;
                    
    throw ex;
                }
     finally {
                    
    if( closeSession ) session.Close();
                }

                
                
    return entity;
            }


            
    #endregion

        }

    }

     
                Entity entity;
                IList entityList;

                
    //
                NHHelper.Instance.Save( entity1 );        
                NHHelper.Instance.Get( entity.GetType(), id );    
    //获得一个对象实例

                NHHelper.Instance.BeginTransaction();    
    //启动事务
                NHHelper.Instance.Add( entity );    //增加一个对象
                NHHelper.Instance.Update( entity, entity.ID );    //更新一个对象
                NHHelper.Instance.Delete( entity );    //删除一个对象
                NHHelper.Instance.Save( entity );    //增加或更新一个对象
                NHHelper.Instance.CommitTransaction();    //提交事务
                
                NHHelper.Instance.Save( entity2, entity3, entity4, entity5 );    
    //增加或更新多个对象,对象类型不用相同
                NHHelper.Instance.Save( entityList );    //增加或更新列表里的所有对象,对象类型不用相同
  • 相关阅读:
    11. Container With Most Water
    9. Palindrome Number
    375. 猜数字大小 II leetcode java
    leetcode 72 编辑距离 JAVA
    73. 矩阵置零 leetcode JAVA
    快速排序 JAVA实现
    63. 不同路径 II leetcode JAVA
    重写(override)与重载(overload)
    62 不同路径 leetcode JAVA
    leetcode 56 合并区间 JAVA
  • 原文地址:https://www.cnblogs.com/wangchuang/p/2489185.html
Copyright © 2011-2022 走看看