-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathDriverFactory.cs
More file actions
578 lines (505 loc) · 21.9 KB
/
DriverFactory.cs
File metadata and controls
578 lines (505 loc) · 21.9 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
// Copyright (C) 2009-2021 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Denis Krjuchkov
// Created: 2009.06.23
using System;
using System.Collections.Generic;
using System.Data.Common;
using Microsoft.Data.SqlClient;
using System.Threading;
using System.Threading.Tasks;
using Xtensive.Core;
using Xtensive.Orm;
using Xtensive.Sql.Info;
using Xtensive.SqlServer.Resources;
using SqlServerConnection = Microsoft.Data.SqlClient.SqlConnection;
namespace Xtensive.Sql.Drivers.SqlServer
{
/// <summary>
/// A <see cref="SqlDriver"/> factory for Microsoft SQL Server.
/// </summary>
public class DriverFactory : SqlDriverFactory
{
private const string CheckConnectionQuery = "SELECT TOP(0) 0;";
private const string PoolingOffCommand = "pooling = false";
private const string DatabaseAndSchemaQuery = "SELECT DB_NAME(), COALESCE(SCHEMA_NAME(), 'dbo')";
private const string LangIdQuery = "SELECT @@LANGID";
private const string MessagesQuery = @"Declare @MSGLANGID int;
Select @MSGLANGID = msglangid FROM [master].[sys].[syslanguages] lang
WHERE lang.langid = @@LANGID;
SELECT msg.error , msg.description
FROM [master].[sys].[sysmessages] msg
WHERE msg.msglangid = @MSGLANGID AND msg.error IN ( 2627, 2601, 515, 547 )";
private const string VersionQuery = "SELECT @@VERSION";
private const string ForcedAzureVersion = "12.0.0.0";
private static ErrorMessageParser CreateMessageParser(SqlServerConnection connection)
{
bool isEnglish;
using (var command = connection.CreateCommand()) {
command.CommandText = LangIdQuery;
var langId = (short) command.ExecuteScalar();
isEnglish = langId == 0 || langId == 23;
}
var templates = new Dictionary<int, string>();
using (var command = connection.CreateCommand()) {
command.CommandText = MessagesQuery;
using (var reader = command.ExecuteReader()) {
while (reader.Read()) {
ReadMessageTemplate(reader, templates);
}
}
}
return new ErrorMessageParser(templates, isEnglish);
}
private static async Task<ErrorMessageParser> CreateMessageParserAsync(
SqlServerConnection connection, CancellationToken token)
{
bool isEnglish;
var command = connection.CreateCommand();
await using (command.ConfigureAwaitFalse()) {
command.CommandText = LangIdQuery;
isEnglish = (await command.ExecuteScalarAsync(token).ConfigureAwaitFalse()).ToString()=="0";
}
var templates = new Dictionary<int, string>();
command = connection.CreateCommand();
await using (command.ConfigureAwaitFalse()) {
command.CommandText = MessagesQuery;
var reader = await command.ExecuteReaderAsync(token).ConfigureAwaitFalse();
await using (reader.ConfigureAwaitFalse()) {
while (await reader.ReadAsync(token).ConfigureAwaitFalse()) {
ReadMessageTemplate(reader, templates);
}
}
}
return new ErrorMessageParser(templates, isEnglish);
}
private static void ReadMessageTemplate(SqlDataReader reader, Dictionary<int, string> templates) =>
templates.Add(reader.GetInt32(0), reader.GetString(1));
private static bool IsAzure(SqlServerConnection connection)
{
using var command = connection.CreateCommand();
command.CommandText = VersionQuery;
return ((string) command.ExecuteScalar()).IndexOf("Azure", StringComparison.Ordinal) >= 0;
}
private static async Task<bool> IsAzureAsync(SqlServerConnection connection, CancellationToken token)
{
var command = connection.CreateCommand();
await using (command.ConfigureAwaitFalse()) {
command.CommandText = VersionQuery;
return ((string) await command.ExecuteScalarAsync(token).ConfigureAwaitFalse())
.IndexOf("Azure", StringComparison.Ordinal) >= 0;
}
}
/// <inheritdoc/>
protected override string BuildConnectionString(UrlInfo url)
{
SqlHelper.ValidateConnectionUrl(url);
var builder = new SqlConnectionStringBuilder();
builder.Encrypt = url.Secure;
// host, port, database
if (url.Port==0) {
builder.DataSource = url.Host;
}
else {
builder.DataSource = url.Host + "," + url.Port;
}
builder.InitialCatalog = url.Resource ?? string.Empty;
// user, password
if (!string.IsNullOrEmpty(url.User)) {
builder.UserID = url.User;
builder.Password = url.Password;
}
else {
builder.IntegratedSecurity = true;
builder.PersistSecurityInfo = false;
}
// custom options
foreach (var param in url.Params) {
builder[param.Key] = param.Value;
}
return builder.ToString();
}
/// <inheritdoc/>
protected override SqlDriver CreateDriver(string connectionString, SqlDriverConfiguration configuration)
{
var isPooingOn = !IsPoolingOff(connectionString);
configuration.EnsureConnectionIsAlive &= isPooingOn;
using var connection = CreateAndOpenConnection(connectionString, configuration);
var isEnsureAlive = configuration.EnsureConnectionIsAlive;
var forcedServerVersion = configuration.ForcedServerVersion;
var isForcedVersion = !string.IsNullOrEmpty(forcedServerVersion);
var isForcedAzure = isForcedVersion && forcedServerVersion.Equals("azure", StringComparison.OrdinalIgnoreCase);
var isAzure = isForcedAzure || (!isForcedVersion && IsAzure(connection));
var parser = isAzure ? new ErrorMessageParser() : CreateMessageParser(connection);
var versionString = isForcedVersion
? isForcedAzure ? ForcedAzureVersion : forcedServerVersion
: connection.ServerVersion ?? string.Empty;
var version = new Version(versionString);
var defaultSchema = GetDefaultSchema(connection);
return CreateDriverInstance(connectionString, isAzure, version, defaultSchema, parser, isEnsureAlive);
}
/// <inheritdoc/>
protected override async Task<SqlDriver> CreateDriverAsync(
string connectionString, SqlDriverConfiguration configuration, CancellationToken token)
{
var isPooingOn = !IsPoolingOff(connectionString);
configuration.EnsureConnectionIsAlive &= isPooingOn;
var connection = await CreateAndOpenConnectionAsync(connectionString, configuration, token).ConfigureAwaitFalse();
await using (connection.ConfigureAwaitFalse()) {
var isEnsureAlive = configuration.EnsureConnectionIsAlive;
var forcedServerVersion = configuration.ForcedServerVersion;
var isForcedVersion = !string.IsNullOrEmpty(forcedServerVersion);
var isForcedAzure = isForcedVersion && forcedServerVersion.Equals("azure", StringComparison.OrdinalIgnoreCase);
var isAzure = isForcedAzure
|| (!isForcedVersion && await IsAzureAsync(connection, token).ConfigureAwaitFalse());
var parser = isAzure
? new ErrorMessageParser()
: await CreateMessageParserAsync(connection, token).ConfigureAwaitFalse();
var versionString = isForcedVersion
? isForcedAzure ? "10.0.0.0" : forcedServerVersion
: connection.ServerVersion ?? string.Empty;
var version = new Version(versionString);
var defaultSchema = await GetDefaultSchemaAsync(connection, token: token).ConfigureAwaitFalse();
return CreateDriverInstance(connectionString, isAzure, version, defaultSchema, parser, isEnsureAlive);
}
}
private static SqlDriver CreateDriverInstance(string connectionString, bool isAzure, Version version,
DefaultSchemaInfo defaultSchema, ErrorMessageParser parser, bool isEnsureAlive)
{
var builder = new SqlConnectionStringBuilder(connectionString);
var coreServerInfo = new CoreServerInfo {
ServerVersion = version,
ConnectionString = connectionString,
MultipleActiveResultSets = builder.MultipleActiveResultSets,
DatabaseName = defaultSchema.Database,
DefaultSchemaName = defaultSchema.Schema,
};
if (isAzure) {
return new Azure.Driver(coreServerInfo, parser, isEnsureAlive);
}
if (version.Major < 9) {
throw new NotSupportedException(Strings.ExSqlServerBelow2005IsNotSupported);
}
return version.Major switch {
9 => new v09.Driver(coreServerInfo, parser, isEnsureAlive),
10 => new v10.Driver(coreServerInfo, parser, isEnsureAlive),
11 => new v11.Driver(coreServerInfo, parser, isEnsureAlive),
12 => new v12.Driver(coreServerInfo, parser, isEnsureAlive),
13 => new v13.Driver(coreServerInfo, parser, isEnsureAlive),
_ => new v13.Driver(coreServerInfo, parser, isEnsureAlive)
};
}
/// <inheritdoc/>
protected override DefaultSchemaInfo ReadDefaultSchema(DbConnection connection, DbTransaction transaction) =>
SqlHelper.ReadDatabaseAndSchema(DatabaseAndSchemaQuery, connection, transaction);
/// <inheritdoc/>
protected override Task<DefaultSchemaInfo> ReadDefaultSchemaAsync(
DbConnection connection, DbTransaction transaction, CancellationToken token) =>
SqlHelper.ReadDatabaseAndSchemaAsync(DatabaseAndSchemaQuery, connection, transaction, token);
private static SqlServerConnection CreateAndOpenConnection(
string connectionString, SqlDriverConfiguration configuration)
{
var connection = new SqlServerConnection(connectionString);
var initScript = configuration.ConnectionInitializationSql;
if (!configuration.EnsureConnectionIsAlive) {
if (configuration.DbConnectionAccessors.Count == 0)
OpenConnectionFast(connection, initScript, false).GetAwaiter().GetResult();
else
OpenConnectionWithNotification(connection, configuration, false).GetAwaiter().GetResult();
return connection;
}
var testQuery = string.IsNullOrEmpty(initScript)
? CheckConnectionQuery
: initScript;
if (configuration.DbConnectionAccessors.Count == 0)
return EnsureConnectionIsAliveFast(connection, testQuery, false).GetAwaiter().GetResult();
else
return EnsureConnectionIsAliveWithNotification(connection, testQuery, configuration.DbConnectionAccessors, false)
.GetAwaiter().GetResult();
}
private static async Task<SqlServerConnection> CreateAndOpenConnectionAsync(
string connectionString, SqlDriverConfiguration configuration, CancellationToken token)
{
var connection = new SqlServerConnection(connectionString);
var initScript = configuration.ConnectionInitializationSql;
if (!configuration.EnsureConnectionIsAlive) {
if (configuration.DbConnectionAccessors.Count == 0)
await OpenConnectionFast(connection, initScript, true, token).ConfigureAwaitFalse();
else
await OpenConnectionWithNotification(connection, configuration, true, token).ConfigureAwaitFalse();
return connection;
}
var testQuery = string.IsNullOrEmpty(initScript)
? CheckConnectionQuery
: initScript;
if (configuration.DbConnectionAccessors.Count == 0)
return await EnsureConnectionIsAliveFast(connection, testQuery, true, token).ConfigureAwaitFalse();
else
return await EnsureConnectionIsAliveWithNotification(connection, testQuery, configuration.DbConnectionAccessors, true, token)
.ConfigureAwaitFalse();
}
private static async ValueTask OpenConnectionFast(SqlServerConnection connection,
string sqlScript, bool isAsync, CancellationToken token = default)
{
if (!isAsync) {
connection.Open();
SqlHelper.ExecuteInitializationSql(connection, sqlScript);
}
else {
await connection.OpenAsync(token).ConfigureAwaitFalse();
await SqlHelper.ExecuteInitializationSqlAsync(connection, sqlScript, token).ConfigureAwaitFalse();
}
}
private static async ValueTask OpenConnectionWithNotification(SqlServerConnection connection,
SqlDriverConfiguration configuration, bool isAsync, CancellationToken token = default)
{
var accessors = configuration.DbConnectionAccessors;
var initSql = configuration.ConnectionInitializationSql;
if (!isAsync) {
SqlHelper.NotifyConnectionOpening(accessors, connection);
try {
connection.Open();
if (!string.IsNullOrEmpty(initSql)) {
SqlHelper.NotifyConnectionInitializing(accessors, connection, initSql);
SqlHelper.ExecuteInitializationSql(connection, initSql);
}
SqlHelper.NotifyConnectionOpened(accessors, connection);
}
catch (Exception ex) {
SqlHelper.NotifyConnectionOpeningFailed(accessors, connection, ex);
throw;
}
}
else {
await SqlHelper.NotifyConnectionOpeningAsync(accessors, connection, false, token);
try {
await connection.OpenAsync(token);
if (!string.IsNullOrEmpty(initSql)) {
await SqlHelper.NotifyConnectionInitializingAsync(accessors, connection, initSql, false, token);
await SqlHelper.ExecuteInitializationSqlAsync(connection, initSql, token);
}
await SqlHelper.NotifyConnectionOpenedAsync(accessors, connection, false, token);
}
catch (Exception ex) {
await SqlHelper.NotifyConnectionOpeningFailedAsync(accessors, connection, ex, false, token);
throw;
}
}
}
private static async ValueTask<SqlServerConnection> EnsureConnectionIsAliveFast(SqlServerConnection connection,
string query, bool isAsync, CancellationToken token = default)
{
if (!isAsync) {
try {
connection.Open();
using (var command = connection.CreateCommand()) {
command.CommandText = query;
_ = command.ExecuteNonQuery();
}
return connection;
}
catch (Exception exception) {
try {
connection.Close();
connection.Dispose();
}
catch {
// ignored
}
if (InternalHelpers.ShouldRetryOn(exception)) {
var (isReconnected, newConnection) =
TryReconnectFast(connection.ConnectionString, query, isAsync).GetAwaiter().GetResult();
if (isReconnected)
return newConnection;
}
throw;
}
}
else {
try {
await connection.OpenAsync(token).ConfigureAwaitFalse();
var command = connection.CreateCommand();
await using (command.ConfigureAwaitFalse()) {
command.CommandText = query;
_ = await command.ExecuteNonQueryAsync(token).ConfigureAwaitFalse();
}
return connection;
}
catch (Exception exception) {
try {
await connection.CloseAsync().ConfigureAwaitFalse();
await connection.DisposeAsync().ConfigureAwaitFalse();
}
catch {
// ignored
}
if (InternalHelpers.ShouldRetryOn(exception)) {
var (isReconnected, newConnection) =
await TryReconnectFast(connection.ConnectionString, query, isAsync, token).ConfigureAwaitFalse();
if (isReconnected) {
return newConnection;
}
}
throw;
}
}
}
private static async ValueTask<SqlServerConnection> EnsureConnectionIsAliveWithNotification(SqlServerConnection connection,
string query, IReadOnlyCollection<IDbConnectionAccessor> connectionAccessos, bool isAsync, CancellationToken token = default)
{
if (!isAsync) {
SqlHelper.NotifyConnectionOpening(connectionAccessos, connection);
try {
connection.Open();
SqlHelper.NotifyConnectionInitializing(connectionAccessos, connection, query);
using (var command = connection.CreateCommand()) {
command.CommandText = query;
_ = command.ExecuteNonQuery();
}
SqlHelper.NotifyConnectionOpened(connectionAccessos, connection);
return connection;
}
catch (Exception exception) {
var retryToConnect = InternalHelpers.ShouldRetryOn(exception);
if (!retryToConnect)
SqlHelper.NotifyConnectionOpeningFailed(connectionAccessos, connection, exception);
try {
connection.Close();
connection.Dispose();
}
catch {
// ignored
}
if (retryToConnect) {
var (isReconnected, newConnection) = TryReconnectWithNotification(connection.ConnectionString, query, connectionAccessos, isAsync)
.GetAwaiter().GetResult();
if (isReconnected) {
return newConnection;
}
}
throw;
}
}
else {
await SqlHelper.NotifyConnectionOpeningAsync(connectionAccessos, connection, false, token).ConfigureAwaitFalse();
try {
await connection.OpenAsync(token).ConfigureAwaitFalse();
await SqlHelper.NotifyConnectionInitializingAsync(connectionAccessos, connection, query, false, token).ConfigureAwaitFalse();
var command = connection.CreateCommand();
await using (command.ConfigureAwaitFalse()) {
command.CommandText = query;
_ = await command.ExecuteNonQueryAsync(token).ConfigureAwaitFalse();
}
await SqlHelper.NotifyConnectionOpenedAsync(connectionAccessos, connection, false, token).ConfigureAwaitFalse();
return connection;
}
catch (Exception exception) {
var retryToConnect = InternalHelpers.ShouldRetryOn(exception);
if (!retryToConnect) {
await SqlHelper.NotifyConnectionOpeningFailedAsync(connectionAccessos, connection, exception, false, token).ConfigureAwaitFalse();
}
var connectionString = connection.ConnectionString;
try {
await connection.CloseAsync().ConfigureAwaitFalse();
await connection.DisposeAsync().ConfigureAwaitFalse();
}
catch {
// ignored
}
if (retryToConnect) {
var (isReconnected, newConnection) =
await TryReconnectWithNotification(connectionString, query, connectionAccessos, isAsync, token).ConfigureAwaitFalse();
if (isReconnected) {
return newConnection;
}
}
throw;
}
}
}
private static async Task<(bool isReconnected, SqlServerConnection connection)> TryReconnectFast(
string connectionString, string query, bool isAsync, CancellationToken token = default)
{
var connection = new SqlServerConnection(connectionString);
if (!isAsync) {
try {
connection.Open();
using (var command = connection.CreateCommand()) {
command.CommandText = query;
_ = command.ExecuteNonQuery();
}
return (true, connection);
}
catch {
connection.Dispose();
return (false, null);
}
}
else {
try {
await connection.OpenAsync(token).ConfigureAwaitFalse();
var command = connection.CreateCommand();
await using (command.ConfigureAwaitFalse()) {
command.CommandText = query;
_ = await command.ExecuteNonQueryAsync(token).ConfigureAwaitFalse();
}
return (true, connection);
}
catch {
await connection.DisposeAsync();
return (false, null);
}
}
}
private static async Task<(bool isReconnected, SqlServerConnection connection)> TryReconnectWithNotification(
string connectionString, string query, IReadOnlyCollection<IDbConnectionAccessor> connectionAccessors,
bool isAsync, CancellationToken token = default)
{
var connection = new SqlServerConnection(connectionString);
if (!isAsync) {
SqlHelper.NotifyConnectionOpening(connectionAccessors, connection, true);
try {
connection.Open();
SqlHelper.NotifyConnectionInitializing(connectionAccessors, connection, query, true);
using (var command = connection.CreateCommand()) {
command.CommandText = query;
_ = command.ExecuteNonQuery();
}
SqlHelper.NotifyConnectionOpened(connectionAccessors, connection, true);
return (true, connection);
}
catch (Exception exception) {
SqlHelper.NotifyConnectionOpeningFailed(connectionAccessors, connection, exception, true);
connection.Dispose();
return (false, null);
}
}
else {
await SqlHelper.NotifyConnectionOpeningAsync(connectionAccessors, connection, true, token).ConfigureAwaitFalse();
try {
await connection.OpenAsync(token).ConfigureAwaitFalse();
await SqlHelper.NotifyConnectionInitializingAsync(connectionAccessors, connection, query, true, token).ConfigureAwaitFalse();
var command = connection.CreateCommand();
await using (command.ConfigureAwaitFalse()) {
command.CommandText = query;
_ = await command.ExecuteNonQueryAsync(token).ConfigureAwaitFalse();
}
await SqlHelper.NotifyConnectionOpenedAsync(connectionAccessors, connection, true, token).ConfigureAwaitFalse();
return (true, connection);
}
catch (Exception exception) {
await SqlHelper.NotifyConnectionOpeningFailedAsync(connectionAccessors, connection, exception, true, token).ConfigureAwaitFalse();
await connection.DisposeAsync();
return (false, null);
}
}
}
private static bool IsPoolingOff(string connectionString)
{
var lowerCaseString = connectionString.ToLower();
return lowerCaseString.Contains(PoolingOffCommand) ||
lowerCaseString.Contains(PoolingOffCommand.Replace(" ", ""));
}
}
}