IEnlistmentNotification接口

IEnlistmentNotification 接口

.NET Framework 4

描述资源管理器为了在登记参与时为事务管理器提供两阶段提交通知回调而应该实现的接口。

public interface IEnlistmentNotification;


   
   
 名称说明
公共方法Commit通知登记的对象事务正在提交。
公共方法InDoubt通知登记的对象事务的状态不确定。
公共方法Prepare通知登记的对象事务正在为提交做准备。
公共方法Rollback通知登记的对象事务正在回滚(中止)。

static void Main(string[] args)
		{
			try
			{
				using (TransactionScope scope = new TransactionScope())
				{
				
					//Create an enlistment object
					myEnlistmentClass myElistment = new myEnlistmentClass();

					//Enlist on the current transaction with the enlistment object
					Transaction.Current.EnlistVolatile(myElistment, EnlistmentOptions.None);

					//Perform transactional work here.

					//Call complete on the TransactionScope based on console input
	                    			ConsoleKeyInfo c;
					while(true)
	                    			{
						Console.Write("Complete the transaction scope? [Y|N] ");
						c = Console.ReadKey();
						Console.WriteLine();
				
		                        			if ((c.KeyChar == 'Y') || (c.KeyChar == 'y'))
						{
							scope.Complete();
							break;
						}
						else if ((c.KeyChar == 'N') || (c.KeyChar == 'n'))
						{
							break;
						}
					}
				}
			}
			catch (System.Transactions.TransactionException ex)
			{
				Console.WriteLine(ex);
			}
			catch
			{
				Console.WriteLine("Cannot complete transaction");
				throw;
			}
		}

		class myEnlistmentClass : IEnlistmentNotification
		{
			public void Prepare(PreparingEnlistment preparingEnlistment)
			{
				Console.WriteLine("Prepare notification received");

				//Perform transactional work

				//If work finished correctly, reply prepared
				preparingEnlistment.Prepared();

				// otherwise, do a ForceRollback
				preparingEnlistment.ForceRollback();
			}

			public void Commit(Enlistment enlistment)
			{
				Console.WriteLine("Commit notification received");

				//Do any work necessary when commit notification is received

				//Declare done on the enlistment
				enlistment.Done();
			}

			public void Rollback(Enlistment enlistment)
			{
				Console.WriteLine("Rollback notification received");

				//Do any work necessary when rollback notification is received

				//Declare done on the enlistment
				enlistment.Done();
			}

			public void InDoubt(Enlistment enlistment)
			{
				Console.WriteLine("In doubt notification received");

				//Do any work necessary when indout notification is received
				
				//Declare done on the enlistment
				enlistment.Done();
			}
		}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>