简单的NHibernate helper类,支持同一事务的批量数据处理
今天为了处理批量数据操作写了个简单的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;
lock( this._sessionMap.SyncRoot ){
if( this._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;
lock( this._sessionMap.SyncRoot ){
if( this._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;
lock( this._sessionMap.SyncRoot ){
if( this._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;
lock( this._sessionMap.SyncRoot ){
if( this._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;
lock( this._sessionMap.SyncRoot ){
if( this._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;
lock( this._sessionMap.SyncRoot ){
if( this._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;
lock( this._sessionMap.SyncRoot ){
if( this._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 {
foreach( object entity in entities ) {
this.Save(entity);
}
this.CommitTransaction();
} catch (Exception ex) {
this.RollbackTransaction();
throw ex;
}
}
public void Save( params object[] entities ) {
this.BeginTransaction();
try {
foreach( object 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;
lock( this._sessionMap.SyncRoot ){
if( this._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
}
}
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;
lock( this._sessionMap.SyncRoot ){
if( this._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;
lock( this._sessionMap.SyncRoot ){
if( this._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;
lock( this._sessionMap.SyncRoot ){
if( this._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;
lock( this._sessionMap.SyncRoot ){
if( this._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;
lock( this._sessionMap.SyncRoot ){
if( this._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;
lock( this._sessionMap.SyncRoot ){
if( this._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;
lock( this._sessionMap.SyncRoot ){
if( this._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 {
foreach( object entity in entities ) {
this.Save(entity);
}
this.CommitTransaction();
} catch (Exception ex) {
this.RollbackTransaction();
throw ex;
}
}
public void Save( params object[] entities ) {
this.BeginTransaction();
try {
foreach( object 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;
lock( this._sessionMap.SyncRoot ){
if( this._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
}
}