我看过System.Transactions命名空间,并想知道,我真的可以使用这个命名空间创建RDMBS吗?
但是当我看到一些例子时,我不明白System.Transactions除了简单的try catch和获得成功/失败结果之外还能做什么呢?
这是MSDN网站上的例子,我知道它可能很简单,但我无法理解这个例子的好处,有人能告诉我在下面的例子中简单的try/catch和事务范围有什么区别吗?
如果我应该创建一个RDBMS (创建我自己的RDMBS),我知道我们必须将我们执行的操作的大量日志写入磁盘,最后我们在回滚的情况下撤消这些操作,但这里没有关于撤消任何事情的内容。
// 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 3rd party RDBMS 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();
try
{
// 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))
{
// 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))
{
// 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);
}
}
// The Complete method commits the transaction. If an exception has been thrown,
// Complete is not called and the transaction is rolled back.
scope.Complete();
}
}
catch (TransactionAbortedException ex)
{
writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);
}
catch (ApplicationException ex)
{
writer.WriteLine("ApplicationException Message: {0}", ex.Message);
}
// Display messages.
Console.WriteLine(writer.ToString());
return returnValue;
}在上面的例子中,我们承诺了什么?我猜SQL客户端库可以做所有的事情,对吗?这是否意味着System.IO.StringWriter将包含所有成功文本或所有失败文本?或者在TransactionScope的作用域之间有任何锁定?
发布于 2010-08-21 18:21:11
首先,TransactionScope与try/catch不同。TransactionScope由事务的名称作用域表示。作用域中的事务必须通过在作用域上调用Complete来显式提交。任何其他情况(包括在作用域中引发的异常)都会导致结束using块,该块处理作用域并隐式地回滚未完成的事务,但不会处理该异常。
在基本情况下,来自System.Transactions的事务的行为与数据库客户端事务的行为相同。System.Transactions提供了以下附加功能:
,促销也会起作用
发布于 2010-08-21 17:55:11
事务将为您执行必要的锁定。此外,还有一个隐式回滚,当事务在其作用域的末尾被处理时,如果它不是由Complete()提交的(如注释所建议的)。因此,如果出现异常,所有操作都会自动回滚,数据库中不会发生任何更改。例如,如果第二个查询失败,它还将放弃对第一个查询所做的更改。
但是,对于StringWriter,它仍将包含直到故障点的消息(例如
Rows to be affected by command1: {0}
ApplicationException Message: {0}可以在此代码后出现在您的日志中。
至于使用这个类创建RDBMS,我不太确定我是否理解了您的问题。如果您想实际创建一个关系型数据库管理系统,我会说您可能找错了地方。如果你的意思是你想通过事务访问RDBMS,我会说,这取决于你的需求,即。如果您需要事务来保证您的语句按顺序运行,并以全有或全无的方式运行,那么是的,事务是一个很好的起点。
https://stackoverflow.com/questions/3537134
复制相似问题