自定义TransactionScope

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Web;
using System.Threading;

namespace Customized.Framework.Transaction
{
    public class CustomizedTransactionScope : IDisposable
    {
        /// <summary>
        
/// 标志位,标志用户是不是提交了事务
        
/// </summary>
        private bool isCompleted = false;
        /// <summary>
        
/// 存放已加入的事务的Connection的
        
/// </summary>
        private Dictionary<String, ConnAndTran> transactionPool = new Dictionary<string, ConnAndTran>();

        /// <summary>
        
/// 用来存放WinFrom程序中的事务
        
/// </summary>
        [ThreadStatic]
        private static CustomizedTransactionScope currentScope;

        /// <summary>
        
/// 取得当前事务
        
/// </summary>
        public static CustomizedTransactionScope Current
        {
            get
            {
                //如果这不是一个Web项目
                if (HttpContext.Current == null)
                {
                    return currentScope;
                }
                else
                {
                    //Web项目的话,就把事务标志放到HttpContext中
                    HttpContext context = HttpContext.Current;
                    
                    return context.Items["CurrentTransactionScope"as CustomizedTransactionScope;
                }
            }
            private set
            {
                
                if (HttpContext.Current == null)
                {
                    currentScope = value;
                }
                else
                {
                    HttpContext context = HttpContext.Current;
                  

                    if (context.Items.Contains("CurrentTransactionScope"))
                        context.Items["CurrentTransactionScope"] = value;
                    else
                        context.Items.Add("CurrentTransactionScope", value);
                   
                }
            }
        }

        private Guid scopeID = Guid.NewGuid();
        /// <summary>
        
/// 事务ID
        
/// </summary>
        public Guid ScopeID
        {
            get
            {
                return scopeID;
            }
        }

        /// <summary>
        
/// 构造方法
        
/// </summary>
        public CustomizedTransactionScope()
        {
            //如果当前没有起动事务,就记下此标志
            if (Current == null)
            {
                Current = this;
            }
        }

        /// <summary>
        
/// 调用此方法,将会在代码段结束后提交事务
        
/// </summary>
        public void Complete()
        {
            //记录用户的提示
            isCompleted = true;
        }

        /// <summary>
        
/// 加入当前事务
        
/// </summary>
        
/// <param name="database"></param>
        
/// <returns></returns>
        public DbTransaction JoinTransaction(Database database)
        {
            
            if (transactionPool.ContainsKey(database.ConnectionStringWithoutCredentials))
            {
                return transactionPool[database.ConnectionStringWithoutCredentials].Transaction;
            }
            else
            {

                DbConnection dbconnection = database.CreateConnection();
                dbconnection.Open();
                DbTransaction dbTransaction = dbconnection.BeginTransaction();

                ConnAndTran Pair = new ConnAndTran();

                Pair.Connection = dbconnection;
                Pair.Transaction = dbTransaction;

                transactionPool.Add(database.ConnectionStringWithoutCredentials, Pair);
                return Pair.Transaction;

            }
        }

        public override bool Equals(object obj)
        {
            if (obj is CustomizedTransactionScope)
            {
                CustomizedTransactionScope scope = obj as CustomizedTransactionScope;

                return (scope.scopeID == this.scopeID);
            }

            return false;

        }

        public override int GetHashCode()
        {
            return scopeID.GetHashCode();
        }

        #region IDisposable 成员
        /// <summary>
        
/// 销毁资源
        
/// </summary>
        public void Dispose()
        {

            if (Current == null)
                return;

            if (Current.scopeID != this.scopeID)
                return;

            foreach (String connString in transactionPool.Keys)
            {
                try
                {
                    //如果用户提交了事务
                    if (isCompleted)
                        transactionPool[connString].Transaction.Commit();
                    else
                        transactionPool[connString].Transaction.Rollback();
                }
                finally
                {
                    //关闭所有的连接
                    DbConnection conn = transactionPool[connString].Connection;

                    if (conn != null && conn.State != System.Data.ConnectionState.Closed)
                        transactionPool[connString].Connection.Close();

                    transactionPool[connString].Transaction.Dispose();
                        
                }
            }
            //去掉事务标志
            RemoveTransaction();

        }

        #endregion

        private void RemoveTransaction()
        {
            Current = null;
        }
    }

    /// <summary>
    
/// 放事务和连接类
    
/// </summary>
    class ConnAndTran
    {
        public DbConnection Connection;
        public DbTransaction Transaction;
    }
}
posted @ 2012-10-25 23:52  chunchill  阅读(660)  评论(0编辑  收藏  举报