Table of Contents

Class NpgsqlMultiHostDataSource

Namespace
Npgsql
Assembly
Npgsql.dll

An NpgsqlDataSource which manages connections for multiple hosts, is aware of their states (primary, secondary, offline...) and can perform failover and load balancing across them.

public sealed class NpgsqlMultiHostDataSource : NpgsqlDataSource, IDisposable, IAsyncDisposable
Inheritance
NpgsqlMultiHostDataSource
Implements
Inherited Members

Remarks

Methods

ClearDatabaseStates()

Clears the database state (primary, secondary, offline...) for all data sources managed by this multi-host data source. Can be useful to make Npgsql retry a PostgreSQL instance which was previously detected to be offline.

public void ClearDatabaseStates()

CreateConnection(TargetSessionAttributes)

Returns a new, unopened connection from this data source.

public NpgsqlConnection CreateConnection(TargetSessionAttributes targetSessionAttributes)

Parameters

targetSessionAttributes TargetSessionAttributes

Specifies the server type (e.g. primary, standby).

Returns

NpgsqlConnection

OpenConnection(TargetSessionAttributes)

Returns a new, opened connection from this data source.

public NpgsqlConnection OpenConnection(TargetSessionAttributes targetSessionAttributes)

Parameters

targetSessionAttributes TargetSessionAttributes

Specifies the server type (e.g. primary, standby).

Returns

NpgsqlConnection

OpenConnectionAsync(TargetSessionAttributes, CancellationToken)

Returns a new, opened connection from this data source.

public ValueTask<NpgsqlConnection> OpenConnectionAsync(TargetSessionAttributes targetSessionAttributes, CancellationToken cancellationToken = default)

Parameters

targetSessionAttributes TargetSessionAttributes

Specifies the server type (e.g. primary, standby).

cancellationToken CancellationToken

An optional token to cancel the asynchronous operation. The default value is None.

Returns

ValueTask<NpgsqlConnection>

WithTargetSession(TargetSessionAttributes)

Returns an NpgsqlDataSource that wraps this multi-host one with the given server type.

public NpgsqlDataSource WithTargetSession(TargetSessionAttributes targetSessionAttributes)

Parameters

targetSessionAttributes TargetSessionAttributes

Specifies the server type (e.g. primary, standby).

Returns

NpgsqlDataSource