创建可提交事务

下面的示例创建一个新的 CommittableTransaction 并提交它。

//Create a committable transaction
tx = new CommittableTransaction(); SqlConnection myConnection = new SqlConnection("server=(local)\\SQLExpress;Integrated Security=SSPI;database=northwind");
SqlCommand myCommand = new SqlCommand(); //Open the SQL connection
myConnection.Open(); //Give the transaction to SQL to enlist with
myConnection.EnlistTransaction(tx); myCommand.Connection = myConnection; // Restore database to near it's original condition so sample will work correctly.
myCommand.CommandText = "DELETE FROM Region WHERE (RegionID = 100) OR (RegionID = 101)";
myCommand.ExecuteNonQuery(); // Insert the first record.
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'MidWestern')";
myCommand.ExecuteNonQuery(); // Insert the second record.
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'MidEastern')";
myCommand.ExecuteNonQuery(); // Commit or rollback the transaction
while (true)
{
Console.Write("Commit or Rollback? [C|R] ");
ConsoleKeyInfo c = Console.ReadKey();
Console.WriteLine(); if ((c.KeyChar == 'C') || (c.KeyChar == 'c'))
{
tx.Commit();
break;
}
else if ((c.KeyChar == 'R') || (c.KeyChar == 'r'))
{
tx.Rollback();
break;
}
}
myConnection.Close();
tx = null;

异步提交

CommittableTransaction 类还提供用于异步提交事务的机制。事务提交可能会占用相当长的时间,因为它可能涉及多个数据库访问并可能有网络滞后时间。如果您想要在具有高吞吐量的应用程序中避免死锁,则可以使用异步提交尽可能快地完成事务工作,并且将提交操作作为后台任务执行。CommittableTransaction 类的 BeginCommitEndCommit 方法支持您这样做。

您可以调用 BeginCommit 以将延迟的提交分配给来自线程池的线程。您还可以调用 EndCommit,确定是否已实际提交事务。如果事务出于任何原因未能提交,则 EndCommit 引发事务异常。如果在调用 EndCommit 时尚未提交该事务,则在提交或中止该事务前阻塞调用方。

进行异步提交的最简单方法是提供在完成提交时要调用的回调方法。但是,您必须对用于进行该调用的原始 CommittableTransaction 对象调用 EndCommit 方法。要获取该对象,您可以终止回调方法的 IAsyncResult 参数,因为 CommittableTransaction 类实现 IAsyncResult 类。

下面的示例说明如何进行异步提交。

public void DoTransactionalWork()
{
Transaction oldAmbient = Transaction.Current;
CommittableTransaction committableTransaction = new CommittableTransaction();
Transaction.Current = committableTransaction; try
{
/* Perform transactional work here */
// No errors - commit transaction asynchronously
committableTransaction.BeginCommit(OnCommitted,null);
}
finally
{
//Restore the ambient transaction
Transaction.Current = oldAmbient;
}
}
void OnCommitted(IAsyncResult asyncResult)
{
CommittableTransaction committableTransaction;
committableTransaction = asyncResult as CommittableTransaction;
Debug.Assert(committableTransaction != null);
try
{
using(committableTransaction)
{
committableTransaction.EndCommit(asyncResult);
}
}
catch(TransactionException e)
{
//Handle the failure to commit
}
}

最新文章

  1. Maven2 根据项目生成模版项目,并使用该模板批量创建工程。
  2. /etc/sudoers文件损坏修复
  3. 跟我学Windows Azure 一 创建Windows Azure试用账号
  4. MY SQL 知识
  5. Jquery validate插件使用方法详解
  6. 161114、websocket实现心跳重连
  7. HDU4619+匈牙利
  8. 利用iptables来配置linux禁止所有端口登陆和开放指定端口
  9. jvm(13)-线程安全与锁优化(转)
  10. Datatable转换为Json 然后,Json数据导入 js 档
  11. Linux、GUN/Linux、GUN、GPL以及各个发行版本详细介绍
  12. screen 链接远程桌面
  13. Jquery实现的几款漂亮的时间轴
  14. Array和ArrayList的区别与联系
  15. Jenkins 在声明式 pipeline 中并行执行任务
  16. mybatis笔记01
  17. 点赞功能与redis
  18. WebDriver与文件系统
  19. 实验吧web解题记录
  20. IconMoon图标字体制作

热门文章

  1. iOS thirdKeyboard Develop (APP Extension)
  2. JRE JDK JVM是什么
  3. JS中setInterval与setTimeout的区别
  4. 移动安全初探:窃取微信聊天记录、Hacking Android with Metasploit
  5. 控制ClistCtrl的滚动的位置
  6. 码农带你区分String类型的"=="和equals()
  7. 黑马程序员——【Java高新技术】——案例:交通灯管理系统
  8. 360个人图书馆 轻松解除网页防复制 (转自老D)
  9. 软件测试第二次作业——Fault,Failure,Error辨析与设计测试用例
  10. Linux mint 18版本开启SSH服务