使用.net 2和ADO.NET。确定是否提交TransactionScope
有没有办法确定一个事务是否被提交?原因是我坚持使用我无法改变的遗留框架,并且可能或可能不是环境事务。有时环境事务已经被提交,导致下一次数据库调用抛出异常,我需要知道它是否是。
任何指针都会很棒!
感谢
约翰
使用.net 2和ADO.NET。确定是否提交TransactionScope
有没有办法确定一个事务是否被提交?原因是我坚持使用我无法改变的遗留框架,并且可能或可能不是环境事务。有时环境事务已经被提交,导致下一次数据库调用抛出异常,我需要知道它是否是。
任何指针都会很棒!
感谢
约翰
检查Transaction.Current.TransactionInformation.Status
。如果它不是TransactionStatus.Active
,则不应使用当前事务。
不言而喻,你应该检查Transaction.Current
为null
之前采取其状态。
我已经找到了最有效地/捕获此正确的最好方法是如下:
使用语句的TransactionScope内部,并调用范围之前/完成()。
//Register for the transaction completed event for the current transaction
Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(Current_TransactionCompleted);
然后创建事件处理函数如下:
/// <summary>
/// Handles the TransactionCompleted event of the Current control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Transactions.TransactionEventArgs"/> instance containing the event data.</param>
static void Current_TransactionCompleted(object sender, TransactionEventArgs e)
{
if (e.Transaction.TransactionInformation.Status == TransactionStatus.Committed)
{
/// Yay it's committed code goes here!
}
}
引述MSDN
“你可以为这个事件而不是使用挥发性入伍以获取交易结果的信息登记。传递给TransactionCompletedEventHandler委托的参数是一个Transaction实例,然后可以查询特定实例的TransactionInformation属性以获取TransactionInformation的实例,其状态属性包含具有Committed或Aborted值的事务状态。“
发布当前代码库的一些例子会有所帮助。 – Oded 2012-01-09 16:05:30