Skip to content

Commit 9994be8

Browse files
committed
Moves Sql Server helper to its project
1 parent 2eb47f6 commit 9994be8

5 files changed

Lines changed: 135 additions & 116 deletions

File tree

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/Connection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private void OpenWithCheck(string checkQueryString)
136136
connectionChecked = true;
137137
}
138138
catch (Exception exception) {
139-
if (SqlHelper.ShouldRetryOn(exception)) {
139+
if (InternalHelpers.ShouldRetryOn(exception)) {
140140
if (restoreTriggered)
141141
throw;
142142

@@ -173,7 +173,7 @@ private async Task OpenWithCheckAsync(string checkQueryString, CancellationToken
173173
connectionChecked = true;
174174
}
175175
catch (Exception exception) {
176-
if (SqlHelper.ShouldRetryOn(exception)) {
176+
if (InternalHelpers.ShouldRetryOn(exception)) {
177177
if (restoreTriggered) {
178178
throw;
179179
}

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/DriverFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Xtensive.Orm;
1515
using Xtensive.Sql.Info;
1616
using SqlServerConnection = System.Data.SqlClient.SqlConnection;
17+
using JetBrains.Annotations;
1718

1819
namespace Xtensive.Sql.Drivers.SqlServer
1920
{
@@ -175,7 +176,7 @@ private void EnsureConnectionIsAlive(ref SqlServerConnection connection, string
175176
}
176177
}
177178
catch (Exception exception) {
178-
if (SqlHelper.ShouldRetryOn(exception)) {
179+
if (InternalHelpers.ShouldRetryOn(exception)) {
179180
if (!TryReconnect(ref connection, query))
180181
throw;
181182
}
@@ -202,8 +203,7 @@ private static bool TryReconnect(ref SqlServerConnection connection, string quer
202203
}
203204
return true;
204205
}
205-
catch (Exception)
206-
{
206+
catch (Exception) {
207207
return false;
208208
}
209209
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright (C) 2020 Xtensive LLC.
2+
// All rights reserved.
3+
// For conditions of distribution and use, see license.
4+
// Created by: Denis Krjuchkov
5+
// Created: 2020.04.10
6+
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Data.SqlClient;
10+
using Xtensive.Core;
11+
12+
namespace Xtensive.Sql.Drivers.SqlServer
13+
{
14+
internal static class InternalHelpers
15+
{
16+
/// <summary>
17+
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
18+
/// directly from your code. This API may change or be removed in future releases.
19+
/// </summary>
20+
public static bool ShouldRetryOn(Exception ex)
21+
{
22+
ArgumentValidator.EnsureArgumentNotNull(ex, "ex");
23+
SqlException sqlException = ex as SqlException;
24+
if (sqlException != null) {
25+
foreach (SqlError err in sqlException.Errors) {
26+
switch (err.Number) {
27+
// SQL Error Code: 49920
28+
// Cannot process request. Too many operations in progress for subscription "%ld".
29+
// The service is busy processing multiple requests for this subscription.
30+
// Requests are currently blocked for resource optimization. Query sys.dm_operation_status for operation status.
31+
// Wait until pending requests are complete or delete one of your pending requests and retry your request later.
32+
case 49920:
33+
// SQL Error Code: 49919
34+
// Cannot process create or update request. Too many create or update operations in progress for subscription "%ld".
35+
// The service is busy processing multiple create or update requests for your subscription or server.
36+
// Requests are currently blocked for resource optimization. Query sys.dm_operation_status for pending operations.
37+
// Wait till pending create or update requests are complete or delete one of your pending requests and
38+
// retry your request later.
39+
case 49919:
40+
// SQL Error Code: 49918
41+
// Cannot process request. Not enough resources to process request.
42+
// The service is currently busy.Please retry the request later.
43+
case 49918:
44+
// SQL Error Code: 41839
45+
// Transaction exceeded the maximum number of commit dependencies.
46+
case 41839:
47+
// SQL Error Code: 41325
48+
// The current transaction failed to commit due to a serializable validation failure.
49+
case 41325:
50+
// SQL Error Code: 41305
51+
// The current transaction failed to commit due to a repeatable read validation failure.
52+
case 41305:
53+
// SQL Error Code: 41302
54+
// The current transaction attempted to update a record that has been updated since the transaction started.
55+
case 41302:
56+
// SQL Error Code: 41301
57+
// Dependency failure: a dependency was taken on another transaction that later failed to commit.
58+
case 41301:
59+
// SQL Error Code: 40613
60+
// Database XXXX on server YYYY is not currently available. Please retry the connection later.
61+
// If the problem persists, contact customer support, and provide them the session tracing ID of ZZZZZ.
62+
case 40613:
63+
// SQL Error Code: 40501
64+
// The service is currently busy. Retry the request after 10 seconds. Code: (reason code to be decoded).
65+
case 40501:
66+
// SQL Error Code: 40197
67+
// The service has encountered an error processing your request. Please try again.
68+
case 40197:
69+
// SQL Error Code: 10929
70+
// Resource ID: %d. The %s minimum guarantee is %d, maximum limit is %d and the current usage for the database is %d.
71+
// However, the server is currently too busy to support requests greater than %d for this database.
72+
// For more information, see http://go.microsoft.com/fwlink/?LinkId=267637. Otherwise, please try again.
73+
case 10929:
74+
// SQL Error Code: 10928
75+
// Resource ID: %d. The %s limit for the database is %d and has been reached. For more information,
76+
// see http://go.microsoft.com/fwlink/?LinkId=267637.
77+
case 10928:
78+
// SQL Error Code: 10060
79+
// A network-related or instance-specific error occurred while establishing a connection to SQL Server.
80+
// The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server
81+
// is configured to allow remote connections. (provider: TCP Provider, error: 0 - A connection attempt failed
82+
// because the connected party did not properly respond after a period of time, or established connection failed
83+
// because connected host has failed to respond.)"}
84+
case 10060:
85+
// SQL Error Code: 10054
86+
// A transport-level error has occurred when sending the request to the server.
87+
// (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)
88+
case 10054:
89+
// SQL Error Code: 10053
90+
// A transport-level error has occurred when receiving results from the server.
91+
// An established connection was aborted by the software in your host machine.
92+
case 10053:
93+
// SQL Error Code: 1205
94+
// Deadlock
95+
case 1205:
96+
// SQL Error Code: 233
97+
// The client was unable to establish a connection because of an error during connection initialization process before login.
98+
// Possible causes include the following: the client tried to connect to an unsupported version of SQL Server;
99+
// the server was too busy to accept new connections; or there was a resource limitation (insufficient memory or maximum
100+
// allowed connections) on the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by
101+
// the remote host.)
102+
case 233:
103+
// SQL Error Code: 121
104+
// The semaphore timeout period has expired
105+
case 121:
106+
// SQL Error Code: 64
107+
// A connection was successfully established with the server, but then an error occurred during the login process.
108+
// (provider: TCP Provider, error: 0 - The specified network name is no longer available.)
109+
case 64:
110+
// DBNETLIB Error Code: 20
111+
// The instance of SQL Server you attempted to connect to does not support encryption.
112+
case 20:
113+
return true;
114+
// This exception can be thrown even if the operation completed succesfully, so it's safer to let the application fail.
115+
// DBNETLIB Error Code: -2
116+
// Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated.
117+
//case -2:
118+
}
119+
}
120+
121+
return false;
122+
}
123+
124+
return ex is TimeoutException;
125+
}
126+
}
127+
}

Orm/Xtensive.Orm.SqlServer/Xtensive.Orm.SqlServer.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1717
<WarningLevel>2</WarningLevel>
1818
</PropertyGroup>
19+
<ItemGroup>
20+
<PackageReference Include="System.Data.SqlClient" Version="4.8.0" />
21+
</ItemGroup>
1922

2023
<ItemGroup>
2124
<ProjectReference Include="..\Xtensive.Orm\Xtensive.Orm.csproj" />

Orm/Xtensive.Orm/Sql/SqlHelper.cs

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -378,116 +378,5 @@ public static NotSupportedException NotSupported(ServerFeatures feature)
378378
{
379379
return NotSupported(feature.ToString());
380380
}
381-
382-
/// <summary>
383-
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
384-
/// directly from your code. This API may change or be removed in future releases.
385-
/// </summary>
386-
public static bool ShouldRetryOn([NotNull] Exception ex)
387-
{
388-
SqlException sqlException = ex as SqlException;
389-
if (sqlException!=null) {
390-
foreach (SqlError err in sqlException.Errors) {
391-
switch (err.Number) {
392-
// SQL Error Code: 49920
393-
// Cannot process request. Too many operations in progress for subscription "%ld".
394-
// The service is busy processing multiple requests for this subscription.
395-
// Requests are currently blocked for resource optimization. Query sys.dm_operation_status for operation status.
396-
// Wait until pending requests are complete or delete one of your pending requests and retry your request later.
397-
case 49920:
398-
// SQL Error Code: 49919
399-
// Cannot process create or update request. Too many create or update operations in progress for subscription "%ld".
400-
// The service is busy processing multiple create or update requests for your subscription or server.
401-
// Requests are currently blocked for resource optimization. Query sys.dm_operation_status for pending operations.
402-
// Wait till pending create or update requests are complete or delete one of your pending requests and
403-
// retry your request later.
404-
case 49919:
405-
// SQL Error Code: 49918
406-
// Cannot process request. Not enough resources to process request.
407-
// The service is currently busy.Please retry the request later.
408-
case 49918:
409-
// SQL Error Code: 41839
410-
// Transaction exceeded the maximum number of commit dependencies.
411-
case 41839:
412-
// SQL Error Code: 41325
413-
// The current transaction failed to commit due to a serializable validation failure.
414-
case 41325:
415-
// SQL Error Code: 41305
416-
// The current transaction failed to commit due to a repeatable read validation failure.
417-
case 41305:
418-
// SQL Error Code: 41302
419-
// The current transaction attempted to update a record that has been updated since the transaction started.
420-
case 41302:
421-
// SQL Error Code: 41301
422-
// Dependency failure: a dependency was taken on another transaction that later failed to commit.
423-
case 41301:
424-
// SQL Error Code: 40613
425-
// Database XXXX on server YYYY is not currently available. Please retry the connection later.
426-
// If the problem persists, contact customer support, and provide them the session tracing ID of ZZZZZ.
427-
case 40613:
428-
// SQL Error Code: 40501
429-
// The service is currently busy. Retry the request after 10 seconds. Code: (reason code to be decoded).
430-
case 40501:
431-
// SQL Error Code: 40197
432-
// The service has encountered an error processing your request. Please try again.
433-
case 40197:
434-
// SQL Error Code: 10929
435-
// Resource ID: %d. The %s minimum guarantee is %d, maximum limit is %d and the current usage for the database is %d.
436-
// However, the server is currently too busy to support requests greater than %d for this database.
437-
// For more information, see http://go.microsoft.com/fwlink/?LinkId=267637. Otherwise, please try again.
438-
case 10929:
439-
// SQL Error Code: 10928
440-
// Resource ID: %d. The %s limit for the database is %d and has been reached. For more information,
441-
// see http://go.microsoft.com/fwlink/?LinkId=267637.
442-
case 10928:
443-
// SQL Error Code: 10060
444-
// A network-related or instance-specific error occurred while establishing a connection to SQL Server.
445-
// The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server
446-
// is configured to allow remote connections. (provider: TCP Provider, error: 0 - A connection attempt failed
447-
// because the connected party did not properly respond after a period of time, or established connection failed
448-
// because connected host has failed to respond.)"}
449-
case 10060:
450-
// SQL Error Code: 10054
451-
// A transport-level error has occurred when sending the request to the server.
452-
// (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)
453-
case 10054:
454-
// SQL Error Code: 10053
455-
// A transport-level error has occurred when receiving results from the server.
456-
// An established connection was aborted by the software in your host machine.
457-
case 10053:
458-
// SQL Error Code: 1205
459-
// Deadlock
460-
case 1205:
461-
// SQL Error Code: 233
462-
// The client was unable to establish a connection because of an error during connection initialization process before login.
463-
// Possible causes include the following: the client tried to connect to an unsupported version of SQL Server;
464-
// the server was too busy to accept new connections; or there was a resource limitation (insufficient memory or maximum
465-
// allowed connections) on the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by
466-
// the remote host.)
467-
case 233:
468-
// SQL Error Code: 121
469-
// The semaphore timeout period has expired
470-
case 121:
471-
// SQL Error Code: 64
472-
// A connection was successfully established with the server, but then an error occurred during the login process.
473-
// (provider: TCP Provider, error: 0 - The specified network name is no longer available.)
474-
case 64:
475-
// DBNETLIB Error Code: 20
476-
// The instance of SQL Server you attempted to connect to does not support encryption.
477-
case 20:
478-
return true;
479-
// This exception can be thrown even if the operation completed succesfully, so it's safer to let the application fail.
480-
// DBNETLIB Error Code: -2
481-
// Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated.
482-
//case -2:
483-
}
484-
}
485-
486-
return false;
487-
}
488-
489-
return ex is TimeoutException;
490-
}
491-
492381
}
493382
}

0 commit comments

Comments
 (0)