读书笔记:《亮剑 .Net》——System.Transactions 事务处理
2009-07-24 14:49
531 查看
使用System.Transactiions不需要考虑是简单事务还是分布式事务。
在SQL Server 2005下.Net会创建一个Local Transaction,性能非常高。但是如果是SQL Server 2000,则会自动激发一个分布式事务,在性能上会受一些损失
using (TransactionScope ts = new TransactionScope())//使整个代码块成为事务性代码
{
#region 在这里编写需要具备Transaction的代码
string msg = "";
string conString = "data source=127.0.0.1;database=codematic;user id=sa;password=";
SqlConnection myConnection = new SqlConnection(conString);
myConnection.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
try
{
myCommand.CommandText = "update P_Product set Name='电脑2' where Id=52";
myCommand.ExecuteNonQuery();
myCommand.CommandText = "update P_Product set Name='电脑3' where Id=53";
myCommand.ExecuteNonQuery();
msg = "成功!";
}
catch (Exception ex)
{
msg = "失败:" + ex.Message;
}
finally
{
myConnection.Close();
}
#endregion
ts.Complete();
return msg;
}
嵌套的事务处理
void RootMethod()
{
using (TransactionScope scope = new TransactionScope())
{
//操作代码
SonMethod();
scope.Complete();
}
}
void SonMethod()
{
using (TransactionScope scope = new TransactionScope())
{
//操作代码
scope.Complete();
}
}
事务范围
如果想保留代码部分执行的操作,并在失败的情况下不希望终止环境事务使用TransactionScopeOption.Suppress
void MethodSuppress()
{
using (TransactionScope scope1 = new TransactionScope())
{
try
{
//开始一个非事务范围
using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.Suppress))
{
//不受事务控制代码
}
//从这里开始又回归事务处理
}
catch
{ }
}
}
在SQL Server 2005下.Net会创建一个Local Transaction,性能非常高。但是如果是SQL Server 2000,则会自动激发一个分布式事务,在性能上会受一些损失
using (TransactionScope ts = new TransactionScope())//使整个代码块成为事务性代码
{
#region 在这里编写需要具备Transaction的代码
string msg = "";
string conString = "data source=127.0.0.1;database=codematic;user id=sa;password=";
SqlConnection myConnection = new SqlConnection(conString);
myConnection.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
try
{
myCommand.CommandText = "update P_Product set Name='电脑2' where Id=52";
myCommand.ExecuteNonQuery();
myCommand.CommandText = "update P_Product set Name='电脑3' where Id=53";
myCommand.ExecuteNonQuery();
msg = "成功!";
}
catch (Exception ex)
{
msg = "失败:" + ex.Message;
}
finally
{
myConnection.Close();
}
#endregion
ts.Complete();
return msg;
}
嵌套的事务处理
void RootMethod()
{
using (TransactionScope scope = new TransactionScope())
{
//操作代码
SonMethod();
scope.Complete();
}
}
void SonMethod()
{
using (TransactionScope scope = new TransactionScope())
{
//操作代码
scope.Complete();
}
}
事务范围
如果想保留代码部分执行的操作,并在失败的情况下不希望终止环境事务使用TransactionScopeOption.Suppress
void MethodSuppress()
{
using (TransactionScope scope1 = new TransactionScope())
{
try
{
//开始一个非事务范围
using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.Suppress))
{
//不受事务控制代码
}
//从这里开始又回归事务处理
}
catch
{ }
}
}
相关文章推荐
- System.Transactions 事务处理
- .NET开发中的事务处理大比拼 之 System.Transactions
- System.Transactions 事务处理
- .NET开发中的事务处理大比拼 之 System.Transactions
- 谈谈分布式事务之三: System.Transactions事务详解[上篇]
- How To Join XLA_AE_HEADERS and RCV_TRANSACTIONS(子分类账到事务处理追溯)
- [第4篇]System.Transactions事务之事务(Transaction)和可提交事务(CommittableTransaction)
- System.Transactions事务超时设置
- 使用事务处理SQL数据时,遇到Exception:The transaction manager has disabled its support for remote/network transactions
- System.Transaction处理数据库事务时 MSDTC不可用的解决办法。
- 接收事务处理类型 rcv_transactions
- 事务控制(System.Transactions)用法
- NET中的System.Transactions(分布式事务)
- How To Join XLA_AE_HEADERS and RCV_TRANSACTIONS(子分类账到事务处理追溯)
- 谈谈分布式事务之三: System.Transactions事务详解[下篇]
- C#事务处理(三)之Transactions事务
- System.Transactions 事务超时属性
- System.Transactions; 事务
- 读书笔记:《亮剑 .Net》——企业级服务COM+事务
- 读书笔记--SQL必知必会20--管理事务处理