forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlDataSource.cs
More file actions
215 lines (179 loc) · 6.82 KB
/
NpgsqlDataSource.cs
File metadata and controls
215 lines (179 loc) · 6.82 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
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Transactions;
using Npgsql.Internal;
using Npgsql.Util;
namespace Npgsql;
/// <inheritdoc />
public abstract class NpgsqlDataSource : DbDataSource
{
/// <inheritdoc />
public override string ConnectionString { get; }
/// <summary>
/// Contains the connection string returned to the user from <see cref="NpgsqlConnection.ConnectionString"/>
/// after the connection has been opened. Does not contain the password unless Persist Security Info=true.
/// </summary>
internal NpgsqlConnectionStringBuilder Settings { get; }
// Note that while the dictionary is protected by locking, we assume that the lists it contains don't need to be
// (i.e. access to connectors of a specific transaction won't be concurrent)
private protected readonly Dictionary<Transaction, List<NpgsqlConnector>> _pendingEnlistedConnectors
= new();
internal abstract (int Total, int Idle, int Busy) Statistics { get; }
volatile bool _isDisposed;
internal NpgsqlDataSource(NpgsqlConnectionStringBuilder settings, string connectionString)
{
Settings = settings;
ConnectionString = settings.PersistSecurityInfo
? connectionString
: settings.ToStringWithoutPassword();
}
/// <summary>
/// Returns a new, unopened connection from this data source.
/// </summary>
public new NpgsqlConnection CreateConnection()
=> NpgsqlConnection.FromDataSource(this);
/// <summary>
/// Returns a new, opened connection from this data source.
/// </summary>
public new NpgsqlConnection OpenConnection()
{
var connection = CreateConnection();
try
{
connection.Open();
return connection;
}
catch
{
connection.Dispose();
throw;
}
}
/// <summary>
/// Returns a new, opened connection from this data source.
/// </summary>
/// <param name="cancellationToken">
/// An optional token to cancel the asynchronous operation. The default value is <see cref="CancellationToken.None"/>.
/// </param>
public new async ValueTask<NpgsqlConnection> OpenConnectionAsync(CancellationToken cancellationToken = default)
{
var connection = CreateConnection();
try
{
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
return connection;
}
catch
{
await connection.DisposeAsync().ConfigureAwait(false);
throw;
}
}
/// <inheritdoc />
protected override DbConnection CreateDbConnection()
=> CreateConnection();
/// <inheritdoc />
protected override DbCommand CreateDbCommand(string? commandText = null)
=> CreateCommand();
/// <inheritdoc />
protected override DbBatch CreateDbBatch()
=> CreateBatch();
/// <summary>
/// Creates a command ready for use against this <see cref="NpgsqlDataSource" />.
/// </summary>
/// <param name="commandText">An optional SQL for the command.</param>
public new NpgsqlCommand CreateCommand(string? commandText = null)
=> new NpgsqlDataSourceCommand(CreateConnection());
/// <summary>
/// Creates a batch ready for use against this <see cref="NpgsqlDataSource" />.
/// </summary>
public new NpgsqlBatch CreateBatch()
=> new NpgsqlDataSourceBatch(CreateConnection());
/// <summary>
/// Creates a new <see cref="NpgsqlDataSource" /> for the given <paramref name="connectionString" />.
/// </summary>
public static NpgsqlDataSource Create(string connectionString)
=> new NpgsqlDataSourceBuilder(connectionString).GetDataSource();
/// <summary>
/// Creates a new <see cref="NpgsqlDataSource" /> for the given <paramref name="connectionStringBuilder" />.
/// </summary>
public static NpgsqlDataSource Create(NpgsqlConnectionStringBuilder connectionStringBuilder)
=> Create(connectionStringBuilder.ToString());
internal abstract ValueTask<NpgsqlConnector> Get(
NpgsqlConnection conn, NpgsqlTimeout timeout, bool async, CancellationToken cancellationToken);
internal abstract bool TryGetIdleConnector([NotNullWhen(true)] out NpgsqlConnector? connector);
internal abstract ValueTask<NpgsqlConnector?> OpenNewConnector(
NpgsqlConnection conn, NpgsqlTimeout timeout, bool async, CancellationToken cancellationToken);
internal abstract void Return(NpgsqlConnector connector);
internal abstract void Clear();
internal abstract bool OwnsConnectors { get; }
#region Pending Enlisted Connections
internal virtual void AddPendingEnlistedConnector(NpgsqlConnector connector, Transaction transaction)
{
lock (_pendingEnlistedConnectors)
{
if (!_pendingEnlistedConnectors.TryGetValue(transaction, out var list))
list = _pendingEnlistedConnectors[transaction] = new List<NpgsqlConnector>();
list.Add(connector);
}
}
internal virtual bool TryRemovePendingEnlistedConnector(NpgsqlConnector connector, Transaction transaction)
{
lock (_pendingEnlistedConnectors)
{
if (!_pendingEnlistedConnectors.TryGetValue(transaction, out var list))
return false;
list.Remove(connector);
if (list.Count == 0)
_pendingEnlistedConnectors.Remove(transaction);
return true;
}
}
internal virtual bool TryRentEnlistedPending(Transaction transaction, NpgsqlConnection connection,
[NotNullWhen(true)] out NpgsqlConnector? connector)
{
lock (_pendingEnlistedConnectors)
{
if (!_pendingEnlistedConnectors.TryGetValue(transaction, out var list))
{
connector = null;
return false;
}
connector = list[list.Count - 1];
list.RemoveAt(list.Count - 1);
if (list.Count == 0)
_pendingEnlistedConnectors.Remove(transaction);
return true;
}
}
#endregion
#region Dispose
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
if (disposing)
{
_isDisposed = true;
Clear();
}
}
/// <inheritdoc />
protected override ValueTask DisposeAsyncCore()
{
// TODO: async Clear, #4499
Dispose(true);
return default;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private protected void CheckDisposed()
{
if (_isDisposed)
throw new ObjectDisposedException(GetType().FullName);
}
#endregion
}