-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTM.SendMessageToAnyService.StoredProcedure.sql
More file actions
273 lines (219 loc) · 14 KB
/
RTM.SendMessageToAnyService.StoredProcedure.sql
File metadata and controls
273 lines (219 loc) · 14 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/****************************************************************************************************
Developed by DataView, LLC. All rights reserved.
Licensed under GNU LESSER GENERAL PUBLIC LICENSE Version 3
Real Time Messenging Software for SQL Server
***************************************************************************************************/
CREATE PROCEDURE [RTM].[SendMessageToAnyService] (
@FromService sysname
,@ToService sysname
,@Contract sysname
,@MessageType sysname
,@MessageBody XML )
AS
BEGIN
--Find the starting state of XACT_ABORT
DECLARE @XACT_ABORT BIT = 0;
IF ( ( 16384 & @@OPTIONS ) = 16384 )
SET @XACT_ABORT = 1;
--turn XACT_ABORT off to prevent transactions from rolling back automatically
SET XACT_ABORT OFF;
DECLARE @ch UNIQUEIDENTIFIER;
DECLARE @details NVARCHAR(255)
DECLARE @GetCHSuccess BIT = 1;
DECLARE @TransactionCount INT;
SET @TransactionCount = @@TRANCOUNT;
BEGIN TRY
IF @TransactionCount = 0
BEGIN TRANSACTION;
ELSE
SAVE TRANSACTION [SavePoint];
--obtain the latest open conversation handle
EXECUTE [RTM].[GetConversationHandleBySPID]
@InitiatorService = @FromService
,@TargetService = @ToService
,@Contract = @Contract
,@ch = @ch OUTPUT;
IF ( @ch IS NULL )
BEGIN
SET @GetCHSuccess = 0;
BEGIN DIALOG CONVERSATION @ch
FROM SERVICE @FromService
TO SERVICE @ToService
ON CONTRACT @Contract
WITH ENCRYPTION = OFF;
END;
--If there wasn't an initial transaction, commit the new transaction
IF @TransactionCount = 0
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
DECLARE @ErrorNumber INT = Error_Number();
DECLARE @ErrorSource NVARCHAR(255) = Object_Name(@@PROCID);
DECLARE @ErrorLine INT = Error_Line();
DECLARE @ErrorMessage NVARCHAR(4000) = Error_Message();
DECLARE @ErrorSeverity INT = Error_Severity();
DECLARE @ErrorState INT = Error_State();
DECLARE @ErrorUser sysname = SUser_SName();
DECLARE @xml_error_details XML
SET @xml_error_details = (
SELECT
[FromService] = @FromService
,[ToService] = @ToService
,[Contract] = @Contract
,[Error_Number] = Error_Number()
,[Object_Name] = Object_Name(@@PROCID)
,[Error_Line] = Error_Line()
,[Error_Message] = Error_Message()
,[Error_Severity] = Error_Severity()
,[Error_State] = Error_State()
,[GetDate] = GetDate()
,[SUser_SName] = SUser_SName()
,[SPID] = @@SPID
,[TranCount] = @@TRANCOUNT
,[initialTranCount] = @TransactionCount
FOR
XML PATH
)
-- IF @@TRANCOUNT > 0
BEGIN
IF ( Xact_State() ) = -1
BEGIN
IF @TransactionCount = 0
BEGIN
ROLLBACK TRANSACTION;
SET @details = N'The transaction is in an uncommittable state. Rolling back transaction.'
END
ELSE
BEGIN
ROLLBACK TRANSACTION [SavePoint]
SET @details = N'The transaction is in an uncommittable state. Rolling back transaction to Save Point.'
END
EXEC [RTM].[AddEvent]
@Level = N'WARNING'
,@Source = N'[RTM].[SendMessageToAnyService]'
,@Details = @details
,@IntResult = 0
,@SessionId = NULL
,@Message_Body = @xml_error_details;
END;
IF ( Xact_State() ) = 1 AND
@TransactionCount = 0
BEGIN
ROLLBACK TRANSACTION;
EXEC [RTM].[AddEvent]
@Level = N'WARNING'
,@Source = N'[RTM].[SendMessageToAnyService]'
,@Details = N'Rolling back current transaction.'
,@IntResult = 0
,@SessionId = NULL
,@Message_Body = @xml_error_details;
END;
IF ( Xact_State() ) = 1 AND
@TransactionCount > 0
BEGIN
ROLLBACK TRANSACTION [SavePoint];
EXEC [RTM].[AddEvent]
@Level = N'WARNING'
,@Source = N'[RTM].[SendMessageToAnyService]'
,@Details = N'Transaction rolled back to the Save Point'
,@IntResult = 0
,@SessionId = NULL
,@Message_Body = @xml_error_details;
END;
END
INSERT INTO [RTM].[Process_M204_ErrorLog]
( [Error_Number]
,[Error_Source]
,[Error_Line]
,[Error_Message]
,[Error_Severity]
,[Error_State]
,[Error_Date]
,[Error_User_System]
,[Message_Body]
,[Source_Process] )
SELECT
@ErrorNumber
,@ErrorSource
,@ErrorLine
,@ErrorMessage
,@ErrorSeverity
,@ErrorState
,GetDate()
,@ErrorUser
,@MessageBody
,'RTM';
THROW;
END CATCH
--manage send as it's own try block
BEGIN TRY
SEND ON CONVERSATION @ch MESSAGE TYPE @MessageType (@MessageBody);
IF @GetCHSuccess = 0
BEGIN
END CONVERSATION @ch;
END;
END TRY
BEGIN CATCH
SET @ErrorNumber = Error_Number();
SET @ErrorSource = Object_Name(@@PROCID);
SET @ErrorLine = Error_Line();
SET @ErrorMessage = Error_Message();
SET @ErrorSeverity = Error_Severity();
SET @ErrorState = Error_State();
SET @ErrorUser = SUser_SName();
SET @xml_error_details = (
SELECT
[FromService] = @FromService
,[ToService] = @ToService
,[Contract] = @Contract
,[Error_Number] = Error_Number()
,[Object_Name] = Object_Name(@@PROCID)
,[Error_Line] = Error_Line()
,[Error_Message] = Error_Message()
,[Error_Severity] = Error_Severity()
,[Error_State] = Error_State()
,[GetDate] = GetDate()
,[SUser_SName] = SUser_SName()
,[SPID] = @@SPID
,[TranCount] = @@TRANCOUNT
,[initialTranCount] = @TransactionCount
FOR
XML PATH
)
EXEC [RTM].[AddEvent]
@Level = N'WARNING'
,@Source = N'[RTM].[SendMessageToAnyService]'
,@Details = N'Send on conversation error'
,@IntResult = 0
,@SessionId = NULL
,@Message_Body = @xml_error_details;
INSERT INTO [RTM].[Process_M204_ErrorLog]
( [Error_Number]
,[Error_Source]
,[Error_Line]
,[Error_Message]
,[Error_Severity]
,[Error_State]
,[Error_Date]
,[Error_User_System]
,[Message_Body]
,[Source_Process] )
SELECT
@ErrorNumber
,@ErrorSource
,@ErrorLine
,@ErrorMessage
,@ErrorSeverity
,@ErrorState
,GetDate()
,@ErrorUser
,@MessageBody
,'RTM';
THROW;
END CATCH
IF @XACT_ABORT = 1
BEGIN
SET XACT_ABORT ON;
END
END;
GO