Skip to content
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

Implement DbDataSource #70006

Merged
merged 6 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
2 changes: 1 addition & 1 deletion src/libraries/System.Data.Common/ref/System.Data.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2415,7 +2415,7 @@ public abstract class DbDataSource : IDisposable, IAsyncDisposable
protected virtual System.Data.Common.DbBatch CreateDbBatch() { throw null; }
public System.Data.Common.DbConnection CreateConnection() { throw null; }
public System.Data.Common.DbConnection OpenConnection() { throw null; }
public System.Threading.Tasks.ValueTask<System.Data.Common.DbConnection> OpenConnectionAsync() { throw null; }
public System.Threading.Tasks.ValueTask<System.Data.Common.DbConnection> OpenConnectionAsync(System.Threading.CancellationToken cancellationToken = default) { throw null; }
public System.Data.Common.DbCommand CreateCommand(string? commandText = null) { throw null; }
public System.Data.Common.DbBatch CreateBatch() { throw null; }
public void Dispose() { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,33 @@ public abstract class DbDataSource : IDisposable, IAsyncDisposable
protected virtual DbConnection OpenDbConnection()
{
var connection = CreateDbConnection();
connection.Open();

return connection;
try
{
connection.Open();
return connection;
}
catch
{
connection.Dispose();
throw;
}
}

protected virtual async ValueTask<DbConnection> OpenDbConnectionAsync(CancellationToken cancellationToken = default)
{
var connection = CreateDbConnection();
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);

return connection;
try
{
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
return connection;
}
catch
{
await connection.DisposeAsync().ConfigureAwait(false);
throw;
}
}

protected virtual DbCommand CreateDbCommand(string? commandText = null)
Expand All @@ -41,11 +57,20 @@ protected virtual DbCommand CreateDbCommand(string? commandText = null)
protected virtual DbBatch CreateDbBatch()
=> new DbBatchWrapper(CreateDbConnection().CreateBatch());

public DbConnection CreateConnection() => CreateDbConnection();
public DbConnection OpenConnection() => OpenDbConnection();
public ValueTask<DbConnection> OpenConnectionAsync() => OpenDbConnectionAsync();
public DbCommand CreateCommand(string? commandText = null) => CreateDbCommand();
public DbBatch CreateBatch() => CreateDbBatch();
public DbConnection CreateConnection()
=> CreateDbConnection();

public DbConnection OpenConnection()
=> OpenDbConnection();

public ValueTask<DbConnection> OpenConnectionAsync(CancellationToken cancellationToken = default)
=> OpenDbConnectionAsync(cancellationToken);

public DbCommand CreateCommand(string? commandText = null)
=> CreateDbCommand();
roji marked this conversation as resolved.
Show resolved Hide resolved

public DbBatch CreateBatch()
=> CreateDbBatch();

public void Dispose()
{
Expand Down Expand Up @@ -97,7 +122,10 @@ public override int ExecuteNonQuery()
}
catch
{
// Swallow to allow the original exception to bubble up
// Swallow to allow the original exception to bubble up.
// Also, refrain from bubbling up the close exception even if there's no original exception,
// since it's not relevant to the user - execution did complete successfully, and the connection
// close is just an internal detail that shouldn't cause user code to fail.
}
}
}
Expand All @@ -120,6 +148,9 @@ public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellat
catch
{
// Swallow to allow the original exception to bubble up
// Also, refrain from bubbling up the close exception even if there's no original exception,
// since it's not relevant to the user - execution did complete successfully, and the connection
// close is just an internal detail that shouldn't cause user code to fail.
}
}
}
Expand All @@ -141,6 +172,9 @@ public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellat
catch
{
// Swallow to allow the original exception to bubble up
// Also, refrain from bubbling up the close exception even if there's no original exception,
// since it's not relevant to the user - execution did complete successfully, and the connection
// close is just an internal detail that shouldn't cause user code to fail.
}
}
}
Expand All @@ -163,6 +197,9 @@ public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellat
catch
{
// Swallow to allow the original exception to bubble up
// Also, refrain from bubbling up the close exception even if there's no original exception,
// since it's not relevant to the user - execution did complete successfully, and the connection
// close is just an internal detail that shouldn't cause user code to fail.
}
}
}
Expand All @@ -184,6 +221,9 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
catch
{
// Swallow to allow the original exception to bubble up
// Also, refrain from bubbling up the close exception even if there's no original exception,
roji marked this conversation as resolved.
Show resolved Hide resolved
// since it's not relevant to the user - execution did complete successfully, and the connection
// close is just an internal detail that shouldn't cause user code to fail.
}

throw;
Expand Down Expand Up @@ -212,6 +252,9 @@ protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(
catch
{
// Swallow to allow the original exception to bubble up
// Also, refrain from bubbling up the close exception even if there's no original exception,
// since it's not relevant to the user - execution did complete successfully, and the connection
// close is just an internal detail that shouldn't cause user code to fail.
}

throw;
Expand Down