Skip to content

Get/Return pooled connections #3404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageVersion Include="System.Memory" Version="4.5.5" />
<PackageVersion Include="System.Data.Common" Version="4.3.0" />
<PackageVersion Include="System.Text.Encodings.Web" Version="8.0.0" />
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
</ItemGroup>
<!-- NetFx and NetCore project dependencies -->
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
<!-- Transitive dependencies that would otherwise bring in older, vulnerable versions. -->
<PackageReference Include="System.Text.Json" />
</ItemGroup>

<!-- netstandard dependencies -->
<ItemGroup Condition="'$(TargetGroup)' != 'netcoreapp'">
<PackageReference Include="System.Threading.Channels" />
</ItemGroup>

<Import Project="$(ToolsDir)targets\ResolveContract.targets" Condition="'$(OSGroup)' == 'AnyOS' AND '$(TargetGroup)' != 'netcoreapp'" />
<Import Project="$(ToolsDir)targets\NotSupported.targets" Condition="'$(OSGroup)' == 'AnyOS' AND '$(TargetGroup)' != 'netcoreapp'" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\ChannelDbConnectionPool.cs">
<Link>Microsoft\Data\SqlClient\ConnectionPool\ChannelDbConnectionPool.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\ConnectionPoolSlots.cs">
<Link>Microsoft\Data\SqlClient\ConnectionPool\ConnectionPoolSlots.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPoolAuthenticationContext.cs">
<Link>Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPoolAuthenticationContext.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<PackageReference Include="System.Security.Cryptography.Pkcs" />
<PackageReference Include="System.Text.Encodings.Web" />
<PackageReference Include="System.Text.Json" />
<PackageReference Include="System.Threading.Channels" />
</ItemGroup>
<Import Project="$(ToolsDir)targets\TrimDocsForIntelliSense.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\ChannelDbConnectionPool.cs">
<Link>Microsoft\Data\SqlClient\ConnectionPool\ChannelDbConnectionPool.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\ConnectionPoolSlots.cs">
<Link>Microsoft\Data\SqlClient\ConnectionPool\ConnectionPoolSlots.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPoolAuthenticationContext.cs">
<Link>Microsoft\Data\SqlClient\ConnectionPool\DbConnectionPoolAuthenticationContext.cs</Link>
</Compile>
Expand Down Expand Up @@ -974,6 +977,7 @@
<PackageReference Include="System.Security.Cryptography.Pkcs" />
<PackageReference Include="System.Text.Encodings.Web" />
<PackageReference Include="System.Text.Json" />
<PackageReference Include="System.Threading.Channels" />
</ItemGroup>
<Import Project="$(CommonSourceRoot)tools\targets\GenerateResourceStringsSource.targets" />
<Import Project="$(NetFxSource)tools\targets\GenerateThisAssemblyCs.targets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<Reference Include="System.Configuration.ConfigurationManager" />
<Reference Include="System.Transactions" />
<PackageReference Include="System.Data.Common" />
</ItemGroup>
<PackageReference Include="System.Threading.Channels" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'net462'">
<!-- References that only apply to netcore -->
<PackageReference Include="System.Configuration.ConfigurationManager" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ internal abstract class DbConnectionInternal
/// </summary>
private bool _cannotBePooled;

/// <summary>
/// When the connection was created.
/// </summary>
private DateTime _createTime;

/// <summary>
/// [usage must be thread-safe] the transaction that we're enlisted in, either manually or automatically.
/// </summary>
Expand Down Expand Up @@ -93,10 +88,16 @@ internal DbConnectionInternal(ConnectionState state, bool hidePassword, bool all
AllowSetConnectionString = allowSetConnectionString;
ShouldHidePassword = hidePassword;
State = state;
CreateTime = DateTime.UtcNow;
}

#region Properties

/// <summary>
/// When the connection was created.
/// </summary>
internal DateTime CreateTime { get; init; }

internal bool AllowSetConnectionString { get; }

internal bool CanBePooled => !IsConnectionDoomed && !_cannotBePooled && !_owningObject.TryGetTarget(out _);
Expand Down Expand Up @@ -531,7 +532,7 @@ internal void DeactivateConnection()
// If we're not already doomed, check the connection's lifetime and
// doom it if it's lifetime has elapsed.
DateTime now = DateTime.UtcNow;
if (now.Ticks - _createTime.Ticks > Pool.LoadBalanceTimeout.Ticks)
if (now.Ticks - CreateTime.Ticks > Pool.LoadBalanceTimeout.Ticks)
{
DoNotPoolThisConnection();
}
Expand Down Expand Up @@ -701,7 +702,6 @@ internal void MakeNonPooledObject(DbConnection owningObject)
/// <param name="connectionPool"></param>
internal void MakePooledConnection(IDbConnectionPool connectionPool)
{
_createTime = DateTime.UtcNow;
Pool = connectionPool;
}

Expand Down
Loading
Loading