Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WorkflowPersistenceContext.cs « Activities « System « System.Activities « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d056669a4e9b3d6fbb9ed16d9268c577ba007dbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.Activities
{
    using System.Collections.Generic;
    using System.Transactions;
    using System.Runtime;

    class WorkflowPersistenceContext
    {
        CommittableTransaction contextOwnedTransaction;
        Transaction clonedTransaction;        

        public WorkflowPersistenceContext(bool transactionRequired, TimeSpan transactionTimeout)
            : this(transactionRequired, CloneAmbientTransaction(), transactionTimeout)
        {
        }

        public WorkflowPersistenceContext(bool transactionRequired, Transaction transactionToUse, TimeSpan transactionTimeout)
        {
            if (transactionToUse != null)
            {
                this.clonedTransaction = transactionToUse;
            }
            else if (transactionRequired)
            {
                this.contextOwnedTransaction = new CommittableTransaction(transactionTimeout);
                // Clone it so that we don't pass a CommittableTransaction to the participants
                this.clonedTransaction = this.contextOwnedTransaction.Clone();
            }
        }

        public Transaction PublicTransaction
        {
            get
            {
                return this.clonedTransaction;
            }
        }       

        public void Abort()
        {
            if (this.contextOwnedTransaction != null)
            {
                try
                {
                    this.contextOwnedTransaction.Rollback();
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }

                    // ---- these exceptions as we are already on the error path
                }
            }
        }

        public void Complete()
        {            
            if (this.contextOwnedTransaction != null)
            {
                this.contextOwnedTransaction.Commit();
            }
        }        

        // Returns true if end needs to be called
        // Note: this is side effecting even if it returns false
        public bool TryBeginComplete(AsyncCallback callback, object state, out IAsyncResult result)
        {
            // In the interest of allocating less objects we don't implement
            // the full async pattern here.  Instead, we've flattened it to
            // do the [....] part and then optionally delegate down to the inner
            // BeginCommit.            

            if (this.contextOwnedTransaction != null)
            {
                result = this.contextOwnedTransaction.BeginCommit(callback, state);
                return true;
            }
            else
            {
                result = null;
                return false;
            }
        }

        public void EndComplete(IAsyncResult result)
        {
            Fx.Assert(this.contextOwnedTransaction != null, "We must have a contextOwnedTransaction if we are calling End");

            this.contextOwnedTransaction.EndCommit(result);
        }

        // We might as well clone the ambient transaction so that PersistenceParticipants
        // can't cast to a CommittableTransaction.
        static Transaction CloneAmbientTransaction()
        {
            Transaction ambientTransaction = Transaction.Current;
            return ambientTransaction == null ? null : ambientTransaction.Clone();
        }
    }
}