forked from danbarua/Npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlResourceManager.cs
More file actions
177 lines (152 loc) · 6.01 KB
/
NpgsqlResourceManager.cs
File metadata and controls
177 lines (152 loc) · 6.01 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// NpgsqlResourceManager.cs
//
// Author:
// Josh Cooley <jbnpgsql@tuxinthebox.net>
//
// Copyright (C) 2007, The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
using System;
using System.Collections.Generic;
using System.Transactions;
namespace Npgsql
{
internal interface INpgsqlResourceManager
{
void Enlist(INpgsqlTransactionCallbacks transactionCallbacks, byte[] txToken);
byte[] Promote(INpgsqlTransactionCallbacks transactionCallbacks);
void CommitWork(string txName);
void RollbackWork(string txName);
}
internal class NpgsqlResourceManager : MarshalByRefObject, INpgsqlResourceManager
{
private readonly Dictionary<string, CommittableTransaction> _transactions = new Dictionary<string, CommittableTransaction>();
#region INpgsqlTransactionManager Members
public byte[] Promote(INpgsqlTransactionCallbacks callbacks)
{
CommittableTransaction tx = new CommittableTransaction();
DurableResourceManager rm = new DurableResourceManager(this, callbacks, tx);
byte[] token = TransactionInterop.GetTransmitterPropagationToken(tx);
_transactions.Add(rm.TxName, tx);
rm.Enlist(tx);
return token;
}
public void Enlist(INpgsqlTransactionCallbacks callbacks, byte[] txToken)
{
DurableResourceManager rm = new DurableResourceManager(this, callbacks);
rm.Enlist(txToken);
}
public void CommitWork(string txName)
{
CommittableTransaction tx;
if (_transactions.TryGetValue(txName, out tx))
{
tx.Commit();
_transactions.Remove(txName);
}
}
public void RollbackWork(string txName)
{
CommittableTransaction tx;
if (_transactions.TryGetValue(txName, out tx))
{
_transactions.Remove(txName);
// you can fail to commit,
// but you're not getting out
// of a rollback. Remove from list first.
tx.Rollback();
}
}
#endregion
private class DurableResourceManager : ISinglePhaseNotification
{
private CommittableTransaction _tx;
private NpgsqlResourceManager _rm;
private readonly INpgsqlTransactionCallbacks _callbacks;
private string _txName;
public DurableResourceManager(NpgsqlResourceManager rm, INpgsqlTransactionCallbacks callbacks)
: this(rm, callbacks, null)
{
}
public DurableResourceManager(NpgsqlResourceManager rm, INpgsqlTransactionCallbacks callbacks,
CommittableTransaction tx)
{
_rm = rm;
_tx = tx;
_callbacks = callbacks;
}
public string TxName
{
get
{
// delay initialize since callback methods may be expensive
if (_txName == null)
{
_txName = _callbacks.GetName();
}
return _txName;
}
}
#region IEnlistmentNotification Members
public void Commit(Enlistment enlistment)
{
_callbacks.CommitTransaction();
// TODO: remove record of prepared
enlistment.Done();
_callbacks.Dispose();
}
public void InDoubt(Enlistment enlistment)
{
// not going to happen when enlisted durably
throw new NotImplementedException();
}
public void Prepare(PreparingEnlistment preparingEnlistment)
{
_callbacks.PrepareTransaction();
// TODO: record prepared
preparingEnlistment.Prepared();
}
public void Rollback(Enlistment enlistment)
{
_callbacks.RollbackTransaction();
// TODO: remove record of prepared
enlistment.Done();
_callbacks.Dispose();
}
#endregion
#region ISinglePhaseNotification Members
public void SinglePhaseCommit(SinglePhaseEnlistment singlePhaseEnlistment)
{
_callbacks.CommitTransaction();
singlePhaseEnlistment.Committed();
_callbacks.Dispose();
}
#endregion
private static readonly Guid rmGuid = new Guid("9e1b6d2d-8cdb-40ce-ac37-edfe5f880716");
public Transaction Enlist(byte[] token)
{
return Enlist(TransactionInterop.GetTransactionFromTransmitterPropagationToken(token));
}
public Transaction Enlist(Transaction tx)
{
tx.EnlistDurable(rmGuid, this, EnlistmentOptions.None);
return tx;
}
}
}
}