-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTM.GetConversationHandleBySPID.StoredProcedure.sql
295 lines (237 loc) · 17 KB
/
RTM.GetConversationHandleBySPID.StoredProcedure.sql
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/****************************************************************************************************
Developed by DataView, LLC. All rights reserved.
Licensed under GNU LESSER GENERAL PUBLIC LICENSE Version 3
Real Time Messenging Software for SQL Server
***************************************************************************************************/
-- =========================================================================================================
/*
Developed by DataView, LLC
Versioned 4/8/2016 (KjM):
--Add a check for intial transaction count, handle transaction commit/rollback in catch
Versioned 4/12/2016 (BPC):
--Check for transaction count when XACT_STATE() = -1. If transaction count = 0, rollback all, ELSE rollback to
--savepoint transaction
Versioned 7/6/2016 (BPC):
--only using conversations not within N minutes (@Expiration_Minutes_delta) of expiration
Versioned 8/18/2016 (BPC/KjM):
--reduced calls to [RTM].[SPID_CONVERSATION] to 2 from 3. Pull the top ch from [RTM].[SPID_CONVERSATION], if
--it doesn't exist create it and return it. Everything done with (NOLOCK) to increase potential performance.
--Risk of dirty read reduce by expiration_delta variable and conversation cleanup procedure changes made previously.
*/
-- ==========================================================================================================
CREATE PROCEDURE [RTM].[GetConversationHandleBySPID] (
@InitiatorService VARCHAR(250)
,@TargetService VARCHAR(250)
,@Contract VARCHAR(250)
,@ch UNIQUEIDENTIFIER OUTPUT )
AS
BEGIN
--set lifespan in seconds (24 hours x 60 minutes x 60 seconds) X 4 days
DECLARE @lifetime INT = ( 24 * 60 * 60 ) * 4;
DECLARE @SessionID UNIQUEIDENTIFIER = NewId();
DECLARE @RandomExecutionRate INT = 20000
DECLARE @details NVARCHAR(255)
DECLARE @TransactionCount INT;
DECLARE @Expiration_Minutes_delta INT = 30
SET @TransactionCount = @@TRANCOUNT;
DECLARE @Expiration_delta DATETIME = DateAdd(MINUTE, @Expiration_Minutes_delta, GetUtcDate())
BEGIN TRY
IF @TransactionCount = 0
BEGIN TRANSACTION;
ELSE
SAVE TRANSACTION [SavePoint];
IF (
SELECT (Round ((Rand () * (@RandomExecutionRate - 1)), 0) + 1)
) = 1
BEGIN TRY
BEGIN
EXEC [RTM].[Maintain_SPID_CONVERSATION]
@SessionID;
END;
END TRY
BEGIN CATCH
DECLARE @xml XML
SET @xml = (
SELECT
[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
,[Xact_State] = Xact_State()
FOR
XML PATH
)
IF Error_Number() = 1222
EXEC [RTM].[AddEvent]
@Level = N'WARNING'
,@Source = N'[RTM].[GetConversationHandleBySPID]'
,@Details = N'Timeout on [RTM].[Maintain_SPID_CONVERSATION]'
,@IntResult = 0
,@SessionId = @SessionID
,@Message_Body = @xml;
ELSE
EXEC [RTM].[AddEvent]
@Level = N'WARNING'
,@Source = N'[RTM].[GetConversationHandleBySPID]'
,@Details = N'Error on [RTM].[Maintain_SPID_CONVERSATION] FROM [RTM].[SPID_CONVERSATION]'
,@IntResult = 0
,@SessionId = @SessionID
,@Message_Body = @xml;
END CATCH;
--just to be sure this is null before trying to populate it
SET @ch = NULL
--Find a ch that is valid for at least N expiration minutes and return as @ch
SELECT TOP 1
@ch = [ce].[conversation_handle]
FROM
[RTM].[SPID_CONVERSATION] [sc] WITH ( NOLOCK )
JOIN [sys].[conversation_endpoints] [ce] WITH ( NOLOCK ) ON [sc].[CONVERSATION_HANDLE] = [ce].[conversation_handle] AND
[ce].[state] IN ( 'CO', 'SO' )
WHERE
[sc].[SPID] = @@SPID AND
[sc].[INITIATOR_SERVICE] = @InitiatorService AND
[sc].[TARGET_SERVICE] = @TargetService AND
[sc].[CONTRACT] = @Contract AND
[ce].[lifetime] > @Expiration_delta
--if we don't find a ch, make one
IF @ch IS NULL
BEGIN
BEGIN DIALOG CONVERSATION @ch
FROM SERVICE @InitiatorService
TO SERVICE @TargetService
ON CONTRACT @Contract
WITH ENCRYPTION = OFF, LIFETIME = @lifetime;
INSERT [RTM].[SPID_CONVERSATION]
( [SPID]
,[INITIATOR_SERVICE]
,[TARGET_SERVICE]
,[CONTRACT]
,[CONVERSATION_HANDLE]
,[Modify_TS]
,[Modify_ID] )
SELECT
@@SPID
,@InitiatorService
,@TargetService
,@Contract
,@ch
,GetDate()
,SUser_Name();
IF (
SELECT (Round ((Rand () * (10 - 1)), 0) + 1)
) = 1
BEGIN
BEGIN TRY
EXECUTE [RTM].[Cleanup_Unusable_Conversations]
@SessionID;
END TRY
BEGIN CATCH
EXEC [RTM].[AddEvent]
@Level = N'WARNING'
,@Source = N'[RTM].[GetConversationHandleBySPID]'
,@Details = N'Deadlock on [RTM].[Cleanup_Unusable_Conversations]'
,@IntResult = 0
,@SessionId = @SessionID
,@Message_Body = NULL;
END CATCH;
END;
END;
--If there wasn't an initial transaction, commit the new transaction
IF @TransactionCount = 0
COMMIT TRANSACTION;
RETURN
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
[InitiatorService] = @InitiatorService
,[TargetService] = @TargetService
,[Contract] = @Contract
,[TransactionCount] = @TransactionCount
FOR
XML PATH
)
EXEC [RTM].[AddEvent]
@Level = N'WARNING'
,@Source = N'[RTM].[GetConversationHandleBySPID]'
,@Details = @details
,@IntResult = 0
,@SessionId = @SessionID
,@Message_Body = @xml_error_details;
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
END;
IF ( Xact_State() ) = 1 AND
@TransactionCount = 0
BEGIN
ROLLBACK TRANSACTION;
EXEC [RTM].[AddEvent]
@Level = N'WARNING'
,@Source = N'[RTM].[GetConversationHandleBySPID]'
,@Details = N'Rolling back current transaction.'
,@IntResult = 0
,@SessionId = @SessionID
,@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].[GetConversationHandleBySPID]'
,@Details = N'Transaction rolled back to the Save Point'
,@IntResult = 0
,@SessionId = @SessionID
,@Message_Body = @xml_error_details;
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
,@xml_error_details
,'RTM';
SET @ch = NULL;
END CATCH;
END;
GO