交易问题 [英] Transaction Issue

查看:109
本文介绍了交易问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我对.Net(2.0)中的事务编程有疑问.希望有人在这个问题上能为我提供帮助.

作为.Net框架,建议使用TransactionScope和using语句进行分布式事务.但是,我遇到的问题是在using语句中,因为它必须包含using语句内的所有连接.如果我有一个带有单个数据表更新的类挠痒痒的消息,并且在一个子例程中使用带有打开/关闭连接的子例程调用,则使用语句解决方案似乎无法接受,因为我打算根据需求实现事务方法.

因此,我研究了commitabletransaction和相关的事务方法,但是我发现它们很可能需要在连接中完成(如MSDN中的示例CommitableTransaction部分所示).

先谢谢了.
Sink中的程序员

解决方案

我正在使用事务范围进行所有事务处理,但确实没有问题.

示例

公共子Save()

尝试

使用ts作为新的transactionScope

myClass1.Save()

myClass2.Save()

ts.commit

结束使用

将ex捕获为例外

''到这里,您的交易将被中止,因为

结束尝试

结束子

您应该能够掌握要点,但是基本上,如果其中一个save方法(在每个命令中使用sqltransaction)抛出异常,则退出transactionscope而不调用commit,因此将其回滚. /p>

业务对象上的每个保存方法都通过databasewrapper类通过读取器中的insert/update方法保存其数据.基本上我想说的是,只要您的主要方法在using语句内,这些方法就可以调用任何数量的其他方法而不会导致线程在using语句内开始和结束时出现任何问题.只需确保将您在事务范围上的超时设置为合理的时间范围即可.

这有意义吗?


All,

I have a question regarding transaction programming in the .Net (2.0). Would like if any one can help me regarding the issue.

As the .Net framework recommended using TransactionScope with using statement for distributed transactions. However, the problem I encountered is in using statement, as it is required to contained all connections inside the using statement. If I have class tickles with a single datatable update which using a subroutine call with open/close connection inside one subroutine, the using statement solution looks like cannot acceptable as I am intending to implement the transaction method based on demand.

So I looks into the commitabletransaction and related transaction methods, however I found them most likely required need to completed with in the connection (As example showed in MSDN, CommitableTransaction section).

Thanks in advance.
A Programmer In Sink

解决方案

I'm using transaction scope for all my transaction handling and not really had a problem.

 

example

 

public sub Save()

 

try

 

Using ts as new transactionScope

 

myClass1.Save()

myClass2.Save()

ts.commit

end using

 

catch ex as exception

 ' Get here your transaction will be aborted because the

end try

 

End Sub

 

you should be able to get the gist but basically if one of the save methods (which use a sqltransaction in each command) throws an exception, the transactionscope is exited without calling the commit and therefore is rolled back.

 

Each save method on the Business Object saves its data through a insert/update method in a reader via a databasewrapper class.  Basically what I'm trying to say is that as long as your primary methods are within the using statement, those methods can call any number of other methods without causing any issues as the thread starts and ends within the using statement.  Just make sure that your timeout on the transaction scope is set to a reasonable time frame.

 

Does this make sense?


这篇关于交易问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆