简单的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#region local variables
        
private NHibernate.Cfg.Configuration _cfg = null;
        
private ISessionFactory _sessionFactory = null;
        
/**//// <summary>
        
/// 用于保存线程中的数据库会话。
        
/// </summary>

        private HybridDictionary _sessionMap; 
        
#endregion


        
singleton pattern#region singleton pattern
        
private static NHHelper _theInstance = null;
        
private NHHelper() {
            
this._cfg = new NHibernate.Cfg.Configuration();
            
this._cfg.AddAssembly( "Entities.Assembly" );
            
this._sessionFactory = this._cfg.BuildSessionFactory();
            
this._sessionMap = new HybridDictionary();
        }


        
public static NHHelper Instance {
            
get {
                
if( NHHelper._theInstance == null ) {
                    NHHelper._theInstance 
= new NHHelper();
                }

                
return NHHelper._theInstance;
            }

        }


        
#endregion


        
transaction relax#region transaction relax
        
public void BeginTransaction() {
            ISession session;

            
lockthis._sessionMap.SyncRoot ){
                
ifthis._sessionMap[Thread.CurrentThread] != null ) {
                    session 
= (ISession)this._sessionMap[Thread.CurrentThread];
                    
if( session.IsOpen ) {
                        
if( session.Transaction == null || session.Transaction.WasCommitted || 
                            session.Transaction.WasRolledBack ) 
{
                            session.BeginTransaction();
                        }
 else {
                            
throw new Exception("当前不支持嵌套事务!");
                        }

                    }

                }
 else {
                    session 
= this._sessionFactory.OpenSession();
                    session.BeginTransaction();
                    
this._sessionMap[Thread.CurrentThread] = session;
                }

            }

        }


        
        
public void CommitTransaction() {
            ISession session;

            
lockthis._sessionMap.SyncRoot ){
                
ifthis._sessionMap[Thread.CurrentThread] != null ) {
                    session 
= (ISession)this._sessionMap[Thread.CurrentThread];
                    
if( session.IsOpen ) {
                        
if( session.Transaction == null || session.Transaction.WasCommitted || 
                            session.Transaction.WasRolledBack ) 
{
                            
throw new Exception("当前已打开的会话或没有未提交的事务!");
                        }
 else {
                            
try {
                                session.Transaction.Commit();
                            }
 catch(Exception ex) {
                                session.Transaction.Rollback();
                                
throw ex;
                            }
 finally {
                                session.Close();
                                
this._sessionMap.Remove( Thread.CurrentThread );
                            }

                        }

                    }
 else {
                        
throw new Exception("当前已打开的会话或没有未提交的事务!");
                    }

                }
 else {
                    
throw new Exception("当前已打开的会话或没有未提交的事务!");
                }

            }

        }


        
        
public void RollbackTransaction() {
            ISession session;

            
lockthis._sessionMap.SyncRoot ){
                
ifthis._sessionMap[Thread.CurrentThread] != null ) {
                    session 
= (ISession)this._sessionMap[Thread.CurrentThread];
                    
if( session.IsOpen ) {
                        
if( session.Transaction == null || session.Transaction.WasCommitted || 
                            session.Transaction.WasRolledBack ) 
{
                            
throw new Exception("当前已打开的会话或没有未提交的事务!");
                        }
 else {
                            
try {
                                session.Transaction.Rollback();
                            }
 catch(Exception ex) {
                                
throw ex;
                            }
 finally {
                                session.Close();
                                
this._sessionMap.Remove( Thread.CurrentThread );
                            }

                        }

                    }
 else {
                        
throw new Exception("当前已打开的会话或没有未提交的事务!");
                    }

                }
 else {
                    
throw new Exception("当前已打开的会话或没有未提交的事务!");
                }

            }

        }

        
        
        
#endregion

        
        
public methods - CRUD#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 );     // 增加或更新列表里的所有对象,对象类型不用相同
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值