泡泡饭

导航

.net中的4种事务总结[转载]

本文来自CSDN博客,http://blog.csdn.net/simonezhlx

 

1 SQL事务
    sql事务是使用SQL server自身的事务:在存储过程中直接使用Begin Tran,Rollback Tran,Commit Tran实现事务:
优点:执行效率最佳
限制:事务上下文仅在数据库中调用,难以实现复杂的业务逻辑。
Demo:(所有demo,都以SQL Server自带的Northwind数据的表Region为例)

带事务的存储过程
CREATE PROCEDURE dbo.SPTransaction
    (
    @UpdateID int,
    @UpdateValue nchar(50),
    @InsertID int,
    @InsertValue nchar(50)
    )
AS
begin Tran
Update Region  Set RegionDescription=@UpdateValue where RegionID=@UpdateID

insert into Region Values (@InsertID,@InsertValue)

declare @RegionError int
select @RegionError=@@error
if(@RegionError=0)
COMMIT Tran
else
ROLLBACK Tran
GO

执行带事务的存储过程
/**//// <summary>
        /// SQL事务:
        /// </summary>
        public void SQLTran()
        {
            SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123;");
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SPTransaction";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;
            conn.Open();
            SqlParameter[] paras= new SqlParameter[]{
                                        new SqlParameter ("@UpdateID",SqlDbType.Int,32),
                                        new SqlParameter ("@UpdateValue",SqlDbType .NChar,50),
                                        new SqlParameter ("@InsertID",SqlDbType.Int ,32),
                                        new SqlParameter ("@InsertValue",SqlDbType.NChar ,50)};
            paras[0].Value = "2";
            paras[1].Value = "Update Value1";
            paras[2].Value = "6";
            paras[3].Value = "Insert Value1";
            foreach (SqlParameter para in paras )
            {
                cmd.Parameters.Add(para);
            }
            cmd.ExecuteNonQuery();  
        }

 

 

2 ADO.net事务
   Ado.net事务可能是大家一般都用的
 优点:简单,效率和数据库事务差不多。
 缺点:事务不能跨数据库,只能在一个数据库连接上。如果是两个数据库上就不能使用该事务了。
Demo:

ADO.net事务
/**//// <summary>
        /// 一般的ADO.net 事务
        /// </summary>
        public void ADONetTran1()
        {
            SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123;");
            SqlCommand cmd = new SqlCommand();
            try
            {
                cmd.CommandText = "Update Region Set RegionDescription=@UpdateValue where RegionID=@UpdateID";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = conn;
                conn.Open();
                SqlParameter[] paras = new SqlParameter[]{
                                        new SqlParameter ("@UpdateID",SqlDbType.Int,32),
                                        new SqlParameter ("@UpdateValue",SqlDbType .NChar,50)};
                paras[0].Value = "2";
                paras[1].Value = "Update Value12";

                foreach (SqlParameter para in paras)
                {
                    cmd.Parameters.Add(para);
                }
                //开始事务
                cmd.Transaction = conn.BeginTransaction();
                cmd.ExecuteNonQuery();


                cmd.CommandText = "insert into Region values(@InsertID,@InsertValue)";
                cmd.CommandType = CommandType.Text;

                paras = new SqlParameter[]{
                                        new SqlParameter ("@InsertID",SqlDbType.Int ,32),
                                        new SqlParameter ("@InsertValue",SqlDbType.NChar ,50)};
                paras[0].Value = "7";
                paras[1].Value = "Insert Value";

                cmd.Parameters.Clear();
                foreach (SqlParameter para in paras)
                {
                    cmd.Parameters.Add(para);
                }
               
                cmd.ExecuteNonQuery();
                //提交事务
                cmd.Transaction.Commit();
            }
            catch
            {
                //回滚事务
                cmd.Transaction.Rollback();
                throw;
            }
            finally
            {
                conn.Close();
            }

        } 

 

 

3 TransactionScope事务
  TransactionScope事务类,它可以使代码块成为事务性代码。并自动提升为分布式事务
 优点:实现简单,同时能够自动提升为分布式事务
Demo:

TransactionScope事务
 /**//// <summary>
        /// TransactionScope事务:可自动提升事务为完全分布式事务的轻型(本地)事务。
        /// 使用时要保证MSDTC服务(控制分布事务)是开启的可以使用:net start msdtc命令开启服务;
        /// </summary>
        public void ADONetTran2()
        {
             SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123;");
             SqlCommand cmd = new SqlCommand();
            try
            {
             
                using (System.Transactions.TransactionScope ts = new TransactionScope())
                {
                   
                    cmd.CommandText = "Update Region Set RegionDescription=@UpdateValue where RegionID=@UpdateID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = conn;
                    conn.Open();
                    SqlParameter[] paras = new SqlParameter[]{
                                        new SqlParameter ("@UpdateID",SqlDbType.Int,32),
                                        new SqlParameter ("@UpdateValue",SqlDbType .NChar,50)};
                    paras[0].Value = "2";
                    paras[1].Value = "Update Value12";

                    foreach (SqlParameter para in paras)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.ExecuteNonQuery();


                    cmd.CommandText = "insert into Region values(@InsertID,@InsertValue)";
                    cmd.CommandType = CommandType.Text;

                    paras = new SqlParameter[]{
                                        new SqlParameter ("@InsertID",SqlDbType.Int ,32),
                                        new SqlParameter ("@InsertValue",SqlDbType.NChar ,50)};
                    paras[0].Value = "8";
                    paras[1].Value = "Insert Value";

                    cmd.Parameters.Clear();
                    foreach (SqlParameter para in paras)
                    {
                        cmd.Parameters.Add(para);
                    }

                    cmd.ExecuteNonQuery();
                    //提交事务
                    ts.Complete();
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                conn.Close();
            }

        }

 

 

 

4 COM+事务
  在分布式应用程序中,往往要同时操作多个数据库,使用数据库事务就不能满足业务的要求了。在COM+中,提供完整的事务处理服务。很方便处理多个数据库上的事务。
Demo:

COM+事务
/**//// <summary>
        /// COM+事务
        /// </summary>
        public void ComTran()
        {
            SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123;");
            SqlCommand cmd = new SqlCommand();
            ServiceConfig sc = new ServiceConfig();

            //指定事务类型
            sc.Transaction = TransactionOption.Required;
            //设置启动跟踪
            sc.TrackingEnabled = true;
            //创建一个上下文,该上下文的配置由作为 cfg 参数传递的 ServiceConfig 对象来指定。
            //随后,客户端和服务器端的策略均被触发,如同发生了一个方法调用。
            //接着,新的上下文被推至上下文堆栈,成为当前上下文
            ServiceDomain.Enter(sc);
            try
            {
                    cmd.CommandText = "Update Region Set RegionDescription=@UpdateValue where RegionID=@UpdateID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = conn;
                    conn.Open();
                    SqlParameter[] paras = new SqlParameter[]{
                                        new SqlParameter ("@UpdateID",SqlDbType.Int,32),
                                        new SqlParameter ("@UpdateValue",SqlDbType .NChar,50)};
                    paras[0].Value = "2";
                    paras[1].Value = "Update Value22";

                    foreach (SqlParameter para in paras)
                    {
                        cmd.Parameters.Add(para);
                    }
                    cmd.ExecuteNonQuery();


                    cmd.CommandText = "insert into Region values(@InsertID,@InsertValue)";
                   cmd.CommandType = CommandType.Text;

                    paras = new SqlParameter[]{
                                        new SqlParameter ("@InsertID",SqlDbType.Int ,32),
                                        new SqlParameter ("@InsertValue",SqlDbType.NChar ,50)};
                    paras[0].Value = "9";
                    paras[1].Value = "Insert Value";

                    cmd.Parameters.Clear();
                    foreach (SqlParameter para in paras)
                    {
                        cmd.Parameters.Add(para);
                    }

                    cmd.ExecuteNonQuery();

                    //提交事务
                    ContextUtil.SetComplete();
            }
            catch
            {
                //回滚事务
                ContextUtil.SetAbort();
                throw;
            }
            finally
            {
                conn.Close();
                //触发服务器端的策略,随后触发客户端的策略,如同一个方法调用正在返回。
                //然后,当前上下文被弹出上下文堆栈,调用 Enter 时正在运行的上下文成为当前的上下文。
                ServiceDomain.Leave();
            }

        }


 
在.net中还有些也能进行事务处理,如web Service中

需要特别补充的是:
如果你使用的是分布事务(TransactionScope事务和COM+事务),在默认情况下你是要重新配置安装SQL Server数据库服务器和访问数据库的客户端的.(如果没有配置运行会出现以下错误:该伙伴事务管理器已经禁止了它对远程/网络事务的支持。 (异常来自 HRESULT:0x8004D025)
)下面是MSDN上关于配置分布式事务的一段原话:
配置分布式事务
要启用分布式事务,可能需要通过网络启用 MS DTC,以便在使用应用了最新的 Service Pack 的较新操作系统(例如 Windows XP 或 Windows 2003)时使用分布式事务。如果启用了 Windows 防火墙(Windows XP Service Pack 2 的默认设置),必须允许 MS DTC 服务使用网络或打开 MS DTC 端口。
实际怎么配置呢,经过我的实际使用:大致如下:打开'控制面板'->'管理工具'->'组件服务',点开'组件服务'->'计算机'->'我的电脑',在'我的电脑'上右击属性,点'MSDTC',然后点'安全性配置'。作为数据库的服务器的配置如下:


而访问数据库的客户端的配置和服务器端的稍有些差别:


在设置完上面的还有使防火墙MS DTC 服务使用网络或打开 MS DTC 端口:运行netsh firewall set allowedprogram %windir%\system32\msdtc.exe MSDTC enable命令就可以了


 

 

 

posted on 2010-05-08 11:19  泡泡饭  阅读(275)  评论(0编辑  收藏  举报