2011-06-01 55 views
0

在父事务范围中,我有TransactionScopeOption of Required。在子事务处理范围中查找父事务范围的TransactionScopeOption

后来在内部事务范围中,我需要检查父事务范围的TransactionScopeOption。

这怎么可能..

Parent : using (TransactionScope transactionScopeParent = new TransactionScope(TransactionScopeOption.Required, transactionOptions)) 
{ 
.... 
method() 
} 

method() 
{ 
here i need to check the TransactionScopeOption of this transaction scope's parent one and then set this transaction scope to be having the same TransactionScopeOption. 

using (TransactionScope transactionScopeChild = new TransactionScope(TransactionScopeOption.Required, transactionOptions)) 
} 

这是可能的。

回答

0

这不是必需的。

如果“父”TransactionScopeRequired,则嵌套的一个已经注册了交易。

如果不是,嵌套的不会自动成为“必需的”。

唯一的例外是,如果您明确将嵌套TransactionScope设置为RequiresNew,那么将启动新的事务。

+0

请注意,我的用法是,内部事务将使其TransactionScope默认为'suppress',但是当存在父事务处理范围时,则需要将此“suppress”更改为与父级内联TransactionScope的。请确认 – Saravanan 2011-06-02 05:51:33

+0

@saravanan - 如果您禁止内部交易,它将不参与交易。也许你应该创建你的函数的两个版本 - 一个抑制交易,一个不行。 – Oded 2011-06-02 05:56:15

+0

我实际上必须为MySQL和SQL事务使用此功能。在SQL的情况下,我将允许子交易参与父交易。在MySQL的情况下,由于MySQL不支持分布式事务,所以它将处于被压缩的事务中。 – Saravanan 2011-06-03 04:02:04

0

从您的评论@俄德的回答来看,你似乎想

  • MS SQL连接参与System.Transactions.Transaction小号
  • MySQL连接不参与

要获得这种行为,你只需要追加

AutoEnlist = False;

MySQL connection string

如果AutoEnlist设置为true,这是默认的,在此范围内使用TransactionScope参与打开连接 ,其提交 当范围提交和回滚如果TransactionScope不提供 提交。但是,此功能被视为安全敏感,因此无法在中等信任环境中使用。