# 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
  object
  DbDataSource
  NpgsqlDataSource
  NpgsqlMultiHostDataSource

Implements
  IDisposable
  IAsyncDisposable

Inherited Members
  NpgsqlDataSource.ConnectionString
  NpgsqlDataSource.CreateConnection()
  NpgsqlDataSource.OpenConnection()
  NpgsqlDataSource.OpenConnectionAsync(CancellationToken)
  NpgsqlDataSource.CreateCommand(string)
  NpgsqlDataSource.CreateBatch()
  NpgsqlDataSource.Create(string)
  NpgsqlDataSource.Create(NpgsqlConnectionStringBuilder)
  NpgsqlDataSource.ReloadTypes()
  NpgsqlDataSource.ReloadTypesAsync(CancellationToken)
  NpgsqlDataSource.Password
  DbDataSource.Dispose()
  DbDataSource.DisposeAsync()
  object.Equals(object)
  object.Equals(object, object)
  object.GetHashCode()
  object.GetType()
  object.ReferenceEquals(object, object)
  object.ToString()

## Remarks

See https://www.npgsql.org/doc/failover-and-load-balancing.html.

## Methods

### Clear()

If the data source pools connections, clears any idle connections and flags any busy connections to be closed as soon as they're returned to the pool.

    public override void Clear()

### 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