最近在了解MSMQ,里面提到的分布是事务,然后涉及到了COM+,而是网上找资料,看到一篇在关于创建COM+组件的文章:
http://www.codeproject.com/Articles/3845/Creating-COM-Objects-using-EnterpriseServices-in-N
花了很长时间把这篇文章啃完了,例子也做好了;
结构大概是这样的,大概有三个组件:
MyBusiness.Administration——这个是起始事务,demo主要调用这个组件方法启动;
MyBusiness.Personnel——被 admin 组件调用,操作数据库A;
MyBusiness.Orders——被 admin 组件调用,操作数据库B。
其中数据库A、B表里面都包含字段 ContextID 、ActivityID ,文中提到当调用MyBusiness.Administration组件方法没有报错,两个子组件在MyBusiness.Administration组件中被调用到,所以数据库A、B都会写入数据,并且两个数据库中表字段 ContextID 会得到相同的值。
但是我测试的结果是ContextID 不同,但是ActivityID 确实相同的,我想问问有经验的大侠问题出哪儿了???是他讲错了还是我程序的问题???
谢谢!
怎么会一个人都没有呢???
确定写数据库这个环节没问题?
你好,这是我的两个子事务类处理内容。
OrdersMaintenance.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Data.SqlClient; 5 using System.EnterpriseServices; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 10 namespace MyBusiness.Orders 11 { 12 [Transaction(TransactionOption.Required)] 13 [ObjectPooling(true, 5, 10)] 14 public class OrdersMaintenance : ServicedComponent 15 { 16 public OrdersMaintenance() 17 { 18 19 } 20 [AutoComplete(true)] 21 public void SetupUser(string name, int jobType, bool makeFail) 22 { 23 string sConnection = "server=.;database=MyOrdersDB;uid=sa;pwd=000000;"; 24 SqlConnection cnn = new SqlConnection(sConnection); 25 cnn.Open(); 26 27 DataSet ds = new DataSet(); 28 DataRow dr; 29 SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblOrderUser", cnn); 30 SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(da); 31 da.InsertCommand = objCommandBuilder.GetInsertCommand(); 32 33 da.MissingSchemaAction = MissingSchemaAction.AddWithKey; 34 35 da.Fill(ds, "tblOrderUser"); 36 37 dr = ds.Tables["tblOrderUser"].NewRow(); 38 39 dr["sName"] = name; 40 dr["nJobType"] = jobType; 41 dr["sTransactionActivityID"] = ContextUtil.ActivityId; 42 dr["sTransactionContextID"] = ContextUtil.ContextId; 43 44 ds.Tables["tblOrderUser"].Rows.Add(dr); 45 46 da.Update(ds, "tblOrderUser"); 47 48 cnn.Close(); 49 50 if (makeFail) 51 { 52 // Oh no!!! Its all gone horibly wrong. 53 throw new Exception("User requested Exception in PayrollMaintenance.AddEmployee"); 54 } 55 } 56 } 57 }
PayrollMaintenance.cs:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.EnterpriseServices; 7 using System.Data.SqlClient; 8 using System.Data; 9 10 [assembly: ApplicationName("MyBusiness.Personnel")] 11 [assembly: ApplicationActivation(ActivationOption.Library)] 12 namespace MyBusiness.Personnel 13 { 14 [Transaction(TransactionOption.Required)] 15 [ObjectPooling(true, 5, 10)] 16 public class PayrollMaintenance : ServicedComponent 17 { 18 public PayrollMaintenance() 19 { 20 21 } 22 public void AddEmployee(string name, string address, int jobType, bool makeFail) 23 { 24 25 string sConnection = "server=.;database=MyPersonnelDB;uid=sa;pwd=000000;"; 26 SqlConnection cnn = new SqlConnection(sConnection); 27 cnn.Open(); 28 29 DataSet ds = new DataSet(); 30 SqlDataAdapter adap = new SqlDataAdapter("SELECT * FROM tblEmployees", sConnection); 31 32 SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(adap); 33 34 adap.InsertCommand = objCommandBuilder.GetInsertCommand(); 35 36 adap.MissingSchemaAction = MissingSchemaAction.AddWithKey; 37 adap.Fill(ds, "tblEmployees"); 38 39 DataRow dr = ds.Tables["tblEmployees"].NewRow(); 40 dr["sName"] = name; 41 dr["sAddress"] = address; 42 dr["nJobType"] = jobType; 43 dr["sTransactionActivityID"] = ContextUtil.ActivityId; 44 dr["sTransactionContextID"] = ContextUtil.ContextId; 45 46 ds.Tables["tblEmployees"].Rows.Add(dr); 47 adap.Update(ds, "tblEmployees"); 48 cnn.Close(); 49 50 if (makeFail) 51 { 52 throw new Exception("User requested Exception in PayrollMaintenance.AddEmployee"); 53 } 54 } 55 } 56 }