1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //-----------------------------------------------------------------------------
5 namespace System.Activities
7 using System.Collections.Generic;
8 using System.Transactions;
11 class WorkflowPersistenceContext
13 CommittableTransaction contextOwnedTransaction;
14 Transaction clonedTransaction;
16 public WorkflowPersistenceContext(bool transactionRequired, TimeSpan transactionTimeout)
17 : this(transactionRequired, CloneAmbientTransaction(), transactionTimeout)
21 public WorkflowPersistenceContext(bool transactionRequired, Transaction transactionToUse, TimeSpan transactionTimeout)
23 if (transactionToUse != null)
25 this.clonedTransaction = transactionToUse;
27 else if (transactionRequired)
29 this.contextOwnedTransaction = new CommittableTransaction(transactionTimeout);
30 // Clone it so that we don't pass a CommittableTransaction to the participants
31 this.clonedTransaction = this.contextOwnedTransaction.Clone();
35 public Transaction PublicTransaction
39 return this.clonedTransaction;
45 if (this.contextOwnedTransaction != null)
49 this.contextOwnedTransaction.Rollback();
58 // ---- these exceptions as we are already on the error path
63 public void Complete()
65 if (this.contextOwnedTransaction != null)
67 this.contextOwnedTransaction.Commit();
71 // Returns true if end needs to be called
72 // Note: this is side effecting even if it returns false
73 public bool TryBeginComplete(AsyncCallback callback, object state, out IAsyncResult result)
75 // In the interest of allocating less objects we don't implement
76 // the full async pattern here. Instead, we've flattened it to
77 // do the sync part and then optionally delegate down to the inner
80 if (this.contextOwnedTransaction != null)
82 result = this.contextOwnedTransaction.BeginCommit(callback, state);
92 public void EndComplete(IAsyncResult result)
94 Fx.Assert(this.contextOwnedTransaction != null, "We must have a contextOwnedTransaction if we are calling End");
96 this.contextOwnedTransaction.EndCommit(result);
99 // We might as well clone the ambient transaction so that PersistenceParticipants
100 // can't cast to a CommittableTransaction.
101 static Transaction CloneAmbientTransaction()
103 Transaction ambientTransaction = Transaction.Current;
104 return ambientTransaction == null ? null : ambientTransaction.Clone();