今天为了处理批量数据操作写了个简单的NHibernate helper类,支持同一事务的批量数据处理. using System; using System.Threading; using System.Collections; using System.Collections.Specialized; using Nullables; using Nullables.NHibernate; using NHibernate; using NHibernate.Cfg; { /// 简单的NHibernate Helper类。支持同一事务内的批量数据处理。 /// </summary> { private NHibernate.Cfg.Configuration _cfg = null; private ISessionFactory _sessionFactory = null; /// 用于保存线程中的数据库会话。 /// </summary> private HybridDictionary _sessionMap; #endregion private static NHHelper _theInstance = null; { this._cfg = new NHibernate.Cfg.Configuration(); this._cfg.AddAssembly( "Entities.Assembly" ); this._sessionFactory = this._cfg.BuildSessionFactory(); this._sessionMap = new HybridDictionary(); } { { { NHHelper._theInstance = new NHHelper(); } return NHHelper._theInstance; } } #endregion { ISession session; { { session = (ISession)this._sessionMap[Thread.CurrentThread]; { if( session.Transaction == null || session.Transaction.WasCommitted || { session.BeginTransaction(); { throw new Exception("当前不支持嵌套事务!"); } } { session = this._sessionFactory.OpenSession(); session.BeginTransaction(); this._sessionMap[Thread.CurrentThread] = session; } } } { ISession session; { { session = (ISession)this._sessionMap[Thread.CurrentThread]; { if( session.Transaction == null || session.Transaction.WasCommitted || { throw new Exception("当前已打开的会话或没有未提交的事务!"); { { session.Transaction.Commit(); { session.Transaction.Rollback(); throw ex; { session.Close(); this._sessionMap.Remove( Thread.CurrentThread ); } } { throw new Exception("当前已打开的会话或没有未提交的事务!"); } { throw new Exception("当前已打开的会话或没有未提交的事务!"); } } } { ISession session; { { session = (ISession)this._sessionMap[Thread.CurrentThread]; { if( session.Transaction == null || session.Transaction.WasCommitted || { throw new Exception("当前已打开的会话或没有未提交的事务!"); { { session.Transaction.Rollback(); { throw ex; { session.Close(); this._sessionMap.Remove( Thread.CurrentThread ); } } { throw new Exception("当前已打开的会话或没有未提交的事务!"); } { throw new Exception("当前已打开的会话或没有未提交的事务!"); } } } #endregion { bool autoCommit = true; ISession session; { { session = (ISession)this._sessionMap[Thread.CurrentThread]; if( session.Transaction == null || session.Transaction.WasCommitted || { autoCommit = true; { autoCommit = false; } { session = this._sessionFactory.OpenSession(); session.BeginTransaction(); } } { session.Save( entity ); { session.Transaction.Commit(); session.Close(); } { { session.Transaction.Rollback(); { { session.Close(); } throw ex; } } { bool autoCommit = true; ISession session; { { session = (ISession)this._sessionMap[Thread.CurrentThread]; if( session.Transaction == null || session.Transaction.WasCommitted || { autoCommit = true; { autoCommit = false; } { session = this._sessionFactory.OpenSession(); session.BeginTransaction(); } } { session.Update( entity, key ); { session.Transaction.Commit(); session.Close(); } { { session.Transaction.Rollback(); { { session.Close(); } throw ex; } } { bool autoCommit = true; ISession session; { { session = (ISession)this._sessionMap[Thread.CurrentThread]; if( session.Transaction == null || session.Transaction.WasCommitted || { autoCommit = true; { autoCommit = false; } { session = this._sessionFactory.OpenSession(); session.BeginTransaction(); } } { session.Delete( entity ); { session.Transaction.Commit(); session.Close(); } { { session.Transaction.Rollback(); { { session.Close(); } throw ex; } } { bool autoCommit = true; ISession session; { { session = (ISession)this._sessionMap[Thread.CurrentThread]; if( session.Transaction == null || session.Transaction.WasCommitted || { autoCommit = true; { autoCommit = false; } { session = this._sessionFactory.OpenSession(); session.BeginTransaction(); } } { session.SaveOrUpdate( entity ); { session.Transaction.Commit(); session.Close(); } { { session.Transaction.Rollback(); { { session.Close(); } throw ex; } } { this.BeginTransaction(); { { this.Save(entity); } this.CommitTransaction(); { this.RollbackTransaction(); throw ex; } } { this.BeginTransaction(); { { this.Save(entity); } this.CommitTransaction(); { this.RollbackTransaction(); throw ex; } } { object entity = null; bool closeSession = true; ISession session; { if( this._sessionMap[Thread.CurrentThread] != null && { session = (ISession)this._sessionMap[Thread.CurrentThread]; closeSession = false; { session = this._sessionFactory.OpenSession(); closeSession = true; } } { entity = session.Get( entityType, id ); { closeSession = true; throw ex; { if( closeSession ) session.Close(); } return entity; } #endregion } } 相关文章: 2021-10-14 2021-09-09 2021-07-25 2022-03-08 2022-12-23 2022-12-23 2022-12-23