forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dotnet/corefx#29697 from AfsanehR/PoolBlockingPeriod
Added PoolBlockingPeriod connection property Commit migrated from dotnet/corefx@f487a4c
- Loading branch information
Showing
17 changed files
with
526 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
...aries/System.Data.SqlClient/src/System/Data/Common/DbConnectionStringCommon.NetCoreApp.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Diagnostics; | ||
using System.Data.SqlClient; | ||
|
||
namespace System.Data.Common | ||
{ | ||
internal static partial class DbConnectionStringBuilderUtil | ||
{ | ||
#region <<PoolBlockingPeriod Utility>> | ||
internal static bool TryConvertToPoolBlockingPeriod(string value, out PoolBlockingPeriod result) | ||
{ | ||
Debug.Assert(Enum.GetNames(typeof(PoolBlockingPeriod)).Length == 3, "PoolBlockingPeriod enum has changed, update needed"); | ||
Debug.Assert(null != value, "TryConvertToPoolBlockingPeriod(null,...)"); | ||
|
||
if (StringComparer.OrdinalIgnoreCase.Equals(value, nameof(PoolBlockingPeriod.Auto))) | ||
{ | ||
result = PoolBlockingPeriod.Auto; | ||
return true; | ||
} | ||
else if (StringComparer.OrdinalIgnoreCase.Equals(value, nameof(PoolBlockingPeriod.AlwaysBlock))) | ||
{ | ||
result = PoolBlockingPeriod.AlwaysBlock; | ||
return true; | ||
} | ||
else if (StringComparer.OrdinalIgnoreCase.Equals(value, nameof(PoolBlockingPeriod.NeverBlock))) | ||
{ | ||
result = PoolBlockingPeriod.NeverBlock; | ||
return true; | ||
} | ||
else | ||
{ | ||
result = DbConnectionStringDefaults.PoolBlockingPeriod; | ||
return false; | ||
} | ||
} | ||
|
||
internal static bool IsValidPoolBlockingPeriodValue(PoolBlockingPeriod value) | ||
{ | ||
Debug.Assert(Enum.GetNames(typeof(PoolBlockingPeriod)).Length == 3, "PoolBlockingPeriod enum has changed, update needed"); | ||
return (uint)value <= (uint)PoolBlockingPeriod.NeverBlock; | ||
} | ||
|
||
internal static string PoolBlockingPeriodToString(PoolBlockingPeriod value) | ||
{ | ||
Debug.Assert(IsValidPoolBlockingPeriodValue(value)); | ||
|
||
switch (value) | ||
{ | ||
case PoolBlockingPeriod.AlwaysBlock: | ||
return nameof(PoolBlockingPeriod.AlwaysBlock); | ||
case PoolBlockingPeriod.NeverBlock: | ||
return nameof(PoolBlockingPeriod.NeverBlock); | ||
default: | ||
return nameof(PoolBlockingPeriod.Auto); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// This method attempts to convert the given value to a PoolBlockingPeriod enum. The algorithm is: | ||
/// * if the value is from type string, it will be matched against PoolBlockingPeriod enum names only, using ordinal, case-insensitive comparer | ||
/// * if the value is from type PoolBlockingPeriod, it will be used as is | ||
/// * if the value is from integral type (SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64), it will be converted to enum | ||
/// * if the value is another enum or any other type, it will be blocked with an appropriate ArgumentException | ||
/// | ||
/// in any case above, if the conerted value is out of valid range, the method raises ArgumentOutOfRangeException. | ||
/// </summary> | ||
/// <returns>PoolBlockingPeriod value in the valid range</returns> | ||
internal static PoolBlockingPeriod ConvertToPoolBlockingPeriod(string keyword, object value) | ||
{ | ||
Debug.Assert(null != value, "ConvertToPoolBlockingPeriod(null)"); | ||
string sValue = (value as string); | ||
PoolBlockingPeriod result; | ||
if (null != sValue) | ||
{ | ||
// We could use Enum.TryParse<PoolBlockingPeriod> here, but it accepts value combinations like | ||
// "ReadOnly, ReadWrite" which are unwelcome here | ||
// Also, Enum.TryParse is 100x slower than plain StringComparer.OrdinalIgnoreCase.Equals method. | ||
if (TryConvertToPoolBlockingPeriod(sValue, out result)) | ||
{ | ||
return result; | ||
} | ||
|
||
// try again after remove leading & trailing whitespaces. | ||
sValue = sValue.Trim(); | ||
if (TryConvertToPoolBlockingPeriod(sValue, out result)) | ||
{ | ||
return result; | ||
} | ||
|
||
// string values must be valid | ||
throw ADP.InvalidConnectionOptionValue(keyword); | ||
} | ||
else | ||
{ | ||
// the value is not string, try other options | ||
PoolBlockingPeriod eValue; | ||
|
||
if (value is PoolBlockingPeriod) | ||
{ | ||
// quick path for the most common case | ||
eValue = (PoolBlockingPeriod)value; | ||
} | ||
else if (value.GetType().IsEnum) | ||
{ | ||
// explicitly block scenarios in which user tries to use wrong enum types, like: | ||
// builder["PoolBlockingPeriod"] = EnvironmentVariableTarget.Process; | ||
// workaround: explicitly cast non-PoolBlockingPeriod enums to int | ||
throw ADP.ConvertFailed(value.GetType(), typeof(PoolBlockingPeriod), null); | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
// Enum.ToObject allows only integral and enum values (enums are blocked above), rasing ArgumentException for the rest | ||
eValue = (PoolBlockingPeriod)Enum.ToObject(typeof(PoolBlockingPeriod), value); | ||
} | ||
catch (ArgumentException e) | ||
{ | ||
// to be consistent with the messages we send in case of wrong type usage, replace | ||
// the error with our exception, and keep the original one as inner one for troubleshooting | ||
throw ADP.ConvertFailed(value.GetType(), typeof(PoolBlockingPeriod), e); | ||
} | ||
} | ||
|
||
// ensure value is in valid range | ||
if (IsValidPoolBlockingPeriodValue(eValue)) | ||
{ | ||
return eValue; | ||
} | ||
else | ||
{ | ||
throw ADP.InvalidEnumerationValue(typeof(ApplicationIntent), (int)eValue); | ||
} | ||
} | ||
} | ||
#endregion | ||
} | ||
|
||
internal static partial class DbConnectionStringDefaults | ||
{ | ||
internal const PoolBlockingPeriod PoolBlockingPeriod = System.Data.SqlClient.PoolBlockingPeriod.Auto; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...braries/System.Data.SqlClient/src/System/Data/ProviderBase/DbConnectionPool.NetCoreApp.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Diagnostics; | ||
using System.Data.Common; | ||
using System.Data.SqlClient; | ||
|
||
namespace System.Data.ProviderBase | ||
{ | ||
sealed internal partial class DbConnectionPool | ||
{ | ||
partial void CheckPoolBlockingPeriod(Exception e) | ||
{ | ||
if (!IsBlockingPeriodEnabled()) | ||
{ | ||
throw e; | ||
} | ||
} | ||
|
||
private bool IsBlockingPeriodEnabled() | ||
{ | ||
var poolGroupConnectionOptions = _connectionPoolGroup.ConnectionOptions as SqlConnectionString; | ||
if (poolGroupConnectionOptions == null) | ||
{ | ||
return true; | ||
} | ||
var policy = poolGroupConnectionOptions.PoolBlockingPeriod; | ||
|
||
switch (policy) | ||
{ | ||
case System.Data.SqlClient.PoolBlockingPeriod.Auto: | ||
{ | ||
return !ADP.IsAzureSqlServerEndpoint(poolGroupConnectionOptions.DataSource); | ||
} | ||
case System.Data.SqlClient.PoolBlockingPeriod.AlwaysBlock: | ||
{ | ||
return true; //Enabled | ||
} | ||
case System.Data.SqlClient.PoolBlockingPeriod.NeverBlock: | ||
{ | ||
return false; //Disabled | ||
} | ||
default: | ||
{ | ||
//we should never get into this path. | ||
Debug.Fail("Unknown PoolBlockingPeriod. Please specify explicit results in above switch case statement."); | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/PoolBlockingPeriod.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
|
||
namespace System.Data.SqlClient | ||
{ | ||
public enum PoolBlockingPeriod | ||
{ | ||
Auto = 0, // Blocking period OFF for Azure SQL servers, but ON for all other SQL servers. | ||
AlwaysBlock = 1, // Blocking period ON for all SQL servers including Azure SQL servers. | ||
NeverBlock = 2, // Blocking period OFF for all SQL servers including Azure SQL servers. | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...braries/System.Data.SqlClient/src/System/Data/SqlClient/SqlConnectionString.NetCoreApp.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Data.Common; | ||
|
||
namespace System.Data.SqlClient | ||
{ | ||
internal sealed partial class SqlConnectionString : DbConnectionOptions | ||
{ | ||
internal static partial class DEFAULT | ||
{ | ||
internal const PoolBlockingPeriod PoolBlockingPeriod = DbConnectionStringDefaults.PoolBlockingPeriod; | ||
} | ||
|
||
private readonly PoolBlockingPeriod _poolBlockingPeriod; | ||
|
||
internal PoolBlockingPeriod PoolBlockingPeriod { get { return _poolBlockingPeriod; } } | ||
|
||
internal System.Data.SqlClient.PoolBlockingPeriod ConvertValueToPoolBlockingPeriod() | ||
{ | ||
string value; | ||
if (!TryGetParsetableValue(KEY.PoolBlockingPeriod, out value)) | ||
{ | ||
return DEFAULT.PoolBlockingPeriod; | ||
} | ||
|
||
try | ||
{ | ||
return DbConnectionStringBuilderUtil.ConvertToPoolBlockingPeriod(KEY.PoolBlockingPeriod, value); | ||
} | ||
catch (Exception e) when (e is FormatException || e is OverflowException) | ||
{ | ||
throw ADP.InvalidConnectionOptionValue(KEY.PoolBlockingPeriod, e); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.