Transaction 確認。
ちょっと話題に出た+忘れかけてたので再確認してみた。
参考
トランザクション スコープを使用した暗黙的なトランザクションの実装
とか。
static void Main( string[] args ) { TranTest test = new TranTest(); test.TestExecute(); Console.WriteLine( "---------------------------------" ); test.TestExecute2(); } class TranTest { public void TestExecute() { try { Console.WriteLine( Transaction.Current == null ? "null" : "not null" ); using (TransactionScope scope1 = new TransactionScope()) { Transaction ct = Transaction.Current; Transaction.Current.TransactionCompleted += CurrentTransactionCompleted; Console.WriteLine( "scope1 DistributedIdentifier " ); Console.WriteLine( " {0} {1:HH:mm:ss.fff} {2}" , Transaction.Current.TransactionInformation.LocalIdentifier , Transaction.Current.TransactionInformation.CreationTime.ToLocalTime() , ct.Equals( Transaction.Current ) ); Thread.Sleep( 100 ); using (TransactionScope scope2 = new TransactionScope( TransactionScopeOption.Required ) ) { Console.WriteLine( "scope2 DistributedIdentifier " ); Console.WriteLine( " {0} {1:HH:mm:ss.fff} {2}" , Transaction.Current.TransactionInformation.LocalIdentifier , Transaction.Current.TransactionInformation.CreationTime.ToLocalTime() , ct.Equals( Transaction.Current ) ); scope2.Complete(); Console.WriteLine( "scope2.Complete" ); } using (TransactionScope scope3 = new TransactionScope( TransactionScopeOption.RequiresNew ) ) { Console.WriteLine( "scope3 DistributedIdentifier " ); Console.WriteLine( " {0} {1:HH:mm:ss.fff} {2}" , Transaction.Current.TransactionInformation.LocalIdentifier , Transaction.Current.TransactionInformation.CreationTime.ToLocalTime() , ct.Equals( Transaction.Current ) ); Transaction.Current.TransactionCompleted += CtTransactionCompleted; scope3.Complete(); } using (TransactionScope scope4 = new TransactionScope( TransactionScopeOption.Suppress ) ) { Console.WriteLine( Transaction.Current == null ); } scope1.Complete(); Console.WriteLine( "scope1.Complete" ); } } catch (TransactionAbortedException) { Console.WriteLine( "Catch TransactionAbortedException" ); } } public void TestExecute2() { using (TransactionScope scope1 = new TransactionScope()) { using (TransactionScope scope3 = new TransactionScope( TransactionScopeOption.RequiresNew ) ) { Console.WriteLine( "scope3 DistributedIdentifier " ); Console.WriteLine( " {0} {1:HH:mm:ss.fff} {2}" , Transaction.Current.TransactionInformation.LocalIdentifier , Transaction.Current.TransactionInformation.CreationTime.ToLocalTime() , Transaction.Current.Equals( Transaction.Current ) ); Transaction.Current.TransactionCompleted += CtTransactionCompleted; //scope3.Complete(); } scope1.Complete(); } } }
結果。
null scope1 DistributedIdentifier 65c3ecbc-644a-48e8-80e6-956bdb9ea815:1 16:24:23.272 True scope2 DistributedIdentifier 65c3ecbc-644a-48e8-80e6-956bdb9ea815:1 16:24:23.272 True scope2.Complete scope3 DistributedIdentifier 65c3ecbc-644a-48e8-80e6-956bdb9ea815:2 16:24:23.272 False CtTransactionCompleted Committed True scope1.Complete CurrentTransactionCompleted Committed --------------------------------- scope3 DistributedIdentifier 65c3ecbc-644a-48e8-80e6-956bdb9ea815:4 16:24:23.272 True CtTransactionCompleted Aborted
Transaction.Current.TransactionInformation.CreationTime がなんで全部一緒なのかはよくわかんなかった。