Repository navigation
Expand file tree
/
Copy pathMessageQueueTransaction.cs
More file actions
198 lines (179 loc) · 7.19 KB
/
Copy pathMessageQueueTransaction.cs
File metadata and controls
198 lines (179 loc) · 7.19 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//------------------------------------------------------------------------------
// <copyright file="MessageQueueTransaction.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Messaging
{
using System.Threading;
using System.Diagnostics;
using System.Messaging.Interop;
/// <include file='doc\MessageQueueTransaction.uex' path='docs/doc[@for="MessageQueueTransaction"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public class MessageQueueTransaction : IDisposable
{
private ITransaction internalTransaction;
private MessageQueueTransactionStatus transactionStatus;
private bool disposed;
/// <include file='doc\MessageQueueTransaction.uex' path='docs/doc[@for="MessageQueueTransaction.MessageQueueTransaction"]/*' />
/// <devdoc>
/// <para>
/// Creates a new Message Queuing internal transaction context.
/// </para>
/// </devdoc>
public MessageQueueTransaction()
{
this.transactionStatus = MessageQueueTransactionStatus.Initialized;
}
internal ITransaction InnerTransaction
{
get
{
return this.internalTransaction;
}
}
/// <include file='doc\MessageQueueTransaction.uex' path='docs/doc[@for="MessageQueueTransaction.Status"]/*' />
/// <devdoc>
/// <para>
/// The status of the transaction that this object represents.
/// </para>
/// </devdoc>
public MessageQueueTransactionStatus Status
{
get
{
return this.transactionStatus;
}
}
/// <include file='doc\MessageQueueTransaction.uex' path='docs/doc[@for="MessageQueueTransaction.Abort"]/*' />
/// <devdoc>
/// <para>
/// Rolls back the pending internal transaction.
/// </para>
/// </devdoc>
public void Abort()
{
lock (this)
{
if (this.internalTransaction == null)
throw new InvalidOperationException(Res.GetString(Res.TransactionNotStarted));
else
{
this.AbortInternalTransaction();
}
}
}
/// <include file='doc\MessageQueueTransaction.uex' path='docs/doc[@for="MessageQueueTransaction.AbortInternalTransaction"]/*' />
/// <internalonly/>
private void AbortInternalTransaction()
{
int status = this.internalTransaction.Abort(0, 0, 0);
if (MessageQueue.IsFatalError(status))
throw new MessageQueueException(status);
this.internalTransaction = null;
this.transactionStatus = MessageQueueTransactionStatus.Aborted;
}
/// <include file='doc\MessageQueueTransaction.uex' path='docs/doc[@for="MessageQueueTransaction.Begin"]/*' />
/// <devdoc>
/// <para>
/// Begins a new Message Queuing internal transaction context.
/// </para>
/// </devdoc>
public void Begin()
{
//Won't allow begining a new transaction after the object has been disposed.
if (this.disposed)
throw new ObjectDisposedException(GetType().Name);
lock (this)
{
if (internalTransaction != null)
throw new InvalidOperationException(Res.GetString(Res.TransactionStarted));
else
{
int status = SafeNativeMethods.MQBeginTransaction(out this.internalTransaction);
if (MessageQueue.IsFatalError(status))
{
this.internalTransaction = null;
throw new MessageQueueException(status);
}
this.transactionStatus = MessageQueueTransactionStatus.Pending;
}
}
}
/// <include file='doc\MessageQueueTransaction.uex' path='docs/doc[@for="MessageQueueTransaction.BeginQueueOperation"]/*' />
/// <internalonly/>
internal ITransaction BeginQueueOperation()
{
#pragma warning disable 0618
//@TODO: This overload of Monitor.Enter is obsolete. Please change this to use Monitor.Enter(ref bool), and remove the pragmas -- ericeil
Monitor.Enter(this);
#pragma warning restore 0618
return this.internalTransaction;
}
/// <include file='doc\MessageQueueTransaction.uex' path='docs/doc[@for="MessageQueueTransaction.Commit"]/*' />
/// <devdoc>
/// <para>
/// Commits a pending internal transaction.
/// </para>
/// </devdoc>
public void Commit()
{
lock (this)
{
if (this.internalTransaction == null)
throw new InvalidOperationException(Res.GetString(Res.TransactionNotStarted));
else
{
int status = this.internalTransaction.Commit(0, 0, 0);
if (MessageQueue.IsFatalError(status))
throw new MessageQueueException(status);
this.internalTransaction = null;
this.transactionStatus = MessageQueueTransactionStatus.Committed;
}
}
}
/// <include file='doc\MessageQueueTransaction.uex' path='docs/doc[@for="MessageQueueTransaction.Dispose"]/*' />
/// <devdoc>
/// <para>
/// Disposes this transaction instance, if it is in a
/// pending status, the transaction will be aborted.
/// </para>
/// </devdoc>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <include file='doc\MessageQueueTransaction.uex' path='docs/doc[@for="MessageQueueTransaction.Dispose1"]/*' />
/// <devdoc>
/// <para>
/// </para>
/// </devdoc>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
lock (this)
{
if (internalTransaction != null)
this.AbortInternalTransaction();
}
}
this.disposed = true;
}
/// <include file='doc\MessageQueueTransaction.uex' path='docs/doc[@for="MessageQueueTransaction.Finalize"]/*' />
/// <internalonly/>
~MessageQueueTransaction()
{
Dispose(false);
}
/// <include file='doc\MessageQueueTransaction.uex' path='docs/doc[@for="MessageQueueTransaction.EndQueueOperation"]/*' />
/// <internalonly/>
internal void EndQueueOperation()
{
Monitor.Exit(this);
}
}
}