Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Activities / System / Activities / WorkflowPersistenceContext.cs
1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //-----------------------------------------------------------------------------
4
5 namespace System.Activities
6 {
7     using System.Collections.Generic;
8     using System.Transactions;
9     using System.Runtime;
10
11     class WorkflowPersistenceContext
12     {
13         CommittableTransaction contextOwnedTransaction;
14         Transaction clonedTransaction;        
15
16         public WorkflowPersistenceContext(bool transactionRequired, TimeSpan transactionTimeout)
17             : this(transactionRequired, CloneAmbientTransaction(), transactionTimeout)
18         {
19         }
20
21         public WorkflowPersistenceContext(bool transactionRequired, Transaction transactionToUse, TimeSpan transactionTimeout)
22         {
23             if (transactionToUse != null)
24             {
25                 this.clonedTransaction = transactionToUse;
26             }
27             else if (transactionRequired)
28             {
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();
32             }
33         }
34
35         public Transaction PublicTransaction
36         {
37             get
38             {
39                 return this.clonedTransaction;
40             }
41         }       
42
43         public void Abort()
44         {
45             if (this.contextOwnedTransaction != null)
46             {
47                 try
48                 {
49                     this.contextOwnedTransaction.Rollback();
50                 }
51                 catch (Exception e)
52                 {
53                     if (Fx.IsFatal(e))
54                     {
55                         throw;
56                     }
57
58                     // ---- these exceptions as we are already on the error path
59                 }
60             }
61         }
62
63         public void Complete()
64         {            
65             if (this.contextOwnedTransaction != null)
66             {
67                 this.contextOwnedTransaction.Commit();
68             }
69         }        
70
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)
74         {
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
78             // BeginCommit.            
79
80             if (this.contextOwnedTransaction != null)
81             {
82                 result = this.contextOwnedTransaction.BeginCommit(callback, state);
83                 return true;
84             }
85             else
86             {
87                 result = null;
88                 return false;
89             }
90         }
91
92         public void EndComplete(IAsyncResult result)
93         {
94             Fx.Assert(this.contextOwnedTransaction != null, "We must have a contextOwnedTransaction if we are calling End");
95
96             this.contextOwnedTransaction.EndCommit(result);
97         }
98
99         // We might as well clone the ambient transaction so that PersistenceParticipants
100         // can't cast to a CommittableTransaction.
101         static Transaction CloneAmbientTransaction()
102         {
103             Transaction ambientTransaction = Transaction.Current;
104             return ambientTransaction == null ? null : ambientTransaction.Clone();
105         }
106     }
107 }