博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单的NHibernate helper类,支持同一事务的批量数据处理
阅读量:4687 次
发布时间:2019-06-09

本文共 7558 字,大约阅读时间需要 25 分钟。

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

转载自:

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 );    
//
增加或更新列表里的所有对象,对象类型不用相同

转载于:https://www.cnblogs.com/wangchuang/archive/2012/05/07/2489185.html

你可能感兴趣的文章
python——网络编程
查看>>
Spark的39个机器学习库
查看>>
Electron学习笔记(一)
查看>>
Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
查看>>
配置NRPE的通讯
查看>>
VS2005编译VTK5.10.1
查看>>
shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言
查看>>
总结上海永辉云商高级前端职位面试题集
查看>>
匹配两个空格之间的字符。。。
查看>>
CSS 文字溢出 变成省略号 ...
查看>>
Spring事务
查看>>
java编程基础(三)流程控制语句
查看>>
让数据库跑的更快的7个MySQL优化建议
查看>>
jquery 取id模糊查询
查看>>
解决在vue中,自用mask模态框出来后,下层的元素依旧可以滑动的问题
查看>>
修改node节点名称
查看>>
Java 文件下载
查看>>
图论——读书笔记 (深度优先搜索)
查看>>
PAT(B) 1014 福尔摩斯的约会(Java)
查看>>
PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
查看>>