Fly with the wind-TerryDong

.NET on the way

导航

[原创]一个可以操作多数据库服务器的事务方法

Posted on 2008-02-03 18:16  Terry Dong  阅读(1476)  评论(7编辑  收藏  举报

方法特点:

支持多个位于不同数据库服务器的数据库的统一事务提交,如果任何一个操作失败,全部回滚

应用:可以应用于分布式数据库系统中,多sql的事务提交应用

 

    /// <summary>
        /// 多数据库服务器事务提交
        /// </summary>
        /// <param name="sqlStrings">key为connName,value为Sql语句</param>
        /// <returns></returns>
        public static bool ExecuteMultiTran(List<string[]> sqlStrings)
        {
            bool reval = true;

            SqlCommand cmd = new SqlCommand();
            SqlTransaction tran;
            SqlConnection conn;
            //事务对象名,事务对象的集合
            Dictionary<string, SqlTransaction> tranResult = new Dictionary<string, SqlTransaction>();

            //conn对象名,对象
            Dictionary<string, SqlConnection> connResult = new Dictionary<string, SqlConnection>();

            //当前是否执行成功
            bool isSuccess = true;

            //
            List<string> keys = new List<string>();

            //通过connName进行循环执行事务
            foreach (string[] sqls in sqlStrings)
            {
                string keyName = sqls[0];

                //如果keys中已经存在当前 keyname,说明改conn的已经执行完毕,跳到下一keyname执行
                if (!keys.Contains(keyName))
                {
                    keys.Add(keyName);

                    //提交当前conn的事务,如果失败,标记当前事务失败
                    try
                    {
                        CreateNewConnection(keyName);
                        conn = new SqlConnection(connectionString);
                        conn.Open();
                        cmd.Connection = conn;
                        tran = conn.BeginTransaction();
                        cmd.Transaction = tran;

                        //记录当前事务
                        tranResult.Add(keyName, tran);

                        //记录当前conn
                        connResult.Add(keyName, conn);

                        //读取当前conn的sql,执行
                        foreach (string[] sql in sqlStrings)
                        {
                            if (sql[0] == keyName)
                            {
                                cmd.CommandText = sql[1];
                                cmd.ExecuteNonQuery();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogUtility.WriteErrLog(ex);

                        isSuccess = false;
                    }

                    if (!isSuccess)
                    {
                        break;
                    }
                }
            }

            //如果当前事务失败,把执行过的所有事务对象rollBack
            if (!isSuccess)
            {
                foreach ( SqlTransaction sqlTran in tranResult.Values)
                {
                    sqlTran.Rollback();
                }
                reval = false;
            }
            else
            {
                foreach (SqlTransaction sqlTran in tranResult.Values)
                {
                    sqlTran.Commit();
                }
            }          
            //关闭conn
            foreach (SqlConnection value in connResult.Values)
            {
                if (value.State!= ConnectionState.Closed)
                {
                    value.Close();
                }
            }
            return reval;
        }