关于多数据库操做的事物实现

ixyxk 2007-08-11 07:44:15
谁有这方面的资料,实现分布式事物的.
...全文
193 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
godgreat 2007-08-13
  • 打赏
  • 举报
回复
使用可提交事务实现显式事务

CommittableTransaction 类为应用程序提供了一个使用事务的显式方法,此方法与隐式使用 TransactionScope 类的方法正好相反。此方法十分适合于想要跨多个函数调用或多个线程调用使用相同事务的应用程序。与 TransactionScope 类不同,为了提交或中止事务,应用程序编写者需要明确调用 Commit 和 Rollback 方法。

CommittableTransaction 类的概述
CommittableTransaction 类是从 Transaction 类派生的,因此提供后者所具有的全部功能。具体的用途就是 Transaction 类上的 Rollback 方法,该类还可用于回滚 CommittableTransaction 对象。

Transaction 类与 CommittableTransaction 类相似,但不提供 Commit 方法。这使您可以将事务对象(或其克隆)传递到其他方法(可能在其他线程上),同时仍控制何时提交事务。调用的代码能够对事务进行登记和投票,但只有 CommittableTransaction 对象的创建者才能够提交事务。

在使用 CommittableTransaction 类时,您应该注意以下事项:

创建 CommittableTransaction 事务并不设置环境事务。您需要具体设置和重置环境事务,以确保资源管理器根据需要基于正确的事务上下文操作。设置当前环境事务的方法是在全局 Transaction 对象上设置静态 Current 属性。

CommittableTransaction 对象不能重复使用。一旦提交或回滚了 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;

Gangzai1983 2007-08-13
  • 打赏
  • 举报
回复
up
qb81195235 2007-08-13
  • 打赏
  • 举报
回复
学习
jacobzhang 2007-08-13
  • 打赏
  • 举报
回复
关注。
czf1943 2007-08-11
  • 打赏
  • 举报
回复
up
Jinglecat 2007-08-11
  • 打赏
  • 举报
回复
使用可提交事务实现显式事务
http://msdn2.microsoft.com/zh-cn/library/ms172146(VS.80).aspx
Jinglecat 2007-08-11
  • 打赏
  • 举报
回复
使用事务范围实现隐式事务
http://msdn2.microsoft.com/zh-cn/library/ms172152(VS.80).aspx
Jinglecat 2007-08-11
  • 打赏
  • 举报
回复
// MSDN DEMO

// This function takes arguments for 2 connection strings and commands to create a transaction
// involving two SQL Servers. It returns a value > 0 if the transaction is committed, 0 if the
// transaction is rolled back. To test this code, you can connect to two different databases
// on the same server by altering the connection string, or to another RDBMS such as Oracle
// by altering the code in the connection2 code block.
static public int CreateTransactionScope(
string connectString1, string connectString2,
string commandText1, string commandText2)
{
// Initialize the return value to zero and create a StringWriter to display results.
int returnValue = 0;
System.IO.StringWriter writer = new System.IO.StringWriter();

// Create the TransactionScope to execute the commands, guaranteeing
// that both commands can commit or roll back as a single unit of work.
using (TransactionScope scope = new TransactionScope())
{
using (SqlConnection connection1 = new SqlConnection(connectString1))
{
try
{
// Opening the connection automatically enlists it in the
// TransactionScope as a lightweight transaction.
connection1.Open();

// Create the SqlCommand object and execute the first command.
SqlCommand command1 = new SqlCommand(commandText1, connection1);
returnValue = command1.ExecuteNonQuery();
writer.WriteLine("Rows to be affected by command1: {0}", returnValue);

// If you get here, this means that command1 succeeded. By nesting
// the using block for connection2 inside that of connection1, you
// conserve server and network resources as connection2 is opened
// only when there is a chance that the transaction can commit.
using (SqlConnection connection2 = new SqlConnection(connectString2))
try
{
// The transaction is escalated to a full distributed
// transaction when connection2 is opened.
connection2.Open();

// Execute the second command in the second database.
returnValue = 0;
SqlCommand command2 = new SqlCommand(commandText2, connection2);
returnValue = command2.ExecuteNonQuery();
writer.WriteLine("Rows to be affected by command2: {0}", returnValue);
}
catch (Exception ex)
{
// Display information that command2 failed.
writer.WriteLine("returnValue for command2: {0}", returnValue);
writer.WriteLine("Exception Message2: {0}", ex.Message);
}
}
catch (Exception ex)
{
// Display information that command1 failed.
writer.WriteLine("returnValue for command1: {0}", returnValue);
writer.WriteLine("Exception Message1: {0}", ex.Message);
}
}

// The Complete method commits the transaction. If an exception has been thrown,
// Complete is not called and the transaction is rolled back.
scope.Complete();
}

// The returnValue is greater than 0 if the transaction committed.
if (returnValue > 0)
{
writer.WriteLine("Transaction was committed.");
}
else
{
// You could write additional business logic here, for example, you can notify the caller
// by throwing a TransactionAbortedException, or logging the failure.
writer.WriteLine("Transaction rolled back.");
}

// Display messages.
Console.WriteLine(writer.ToString());

return returnValue;
}

zynj03315 2007-08-11
  • 打赏
  • 举报
回复
up
zhqs1000 2007-08-11
  • 打赏
  • 举报
回复
up
baoker 2007-08-11
  • 打赏
  • 举报
回复
没有 up

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧