# Google Cloud Spanner v1 API - Class ManagedSession (5.13.0-beta07)

5.13.0-beta07 5.13.0-beta06 5.12.0 (latest) 5.11.0 5.10.0 5.9.0 5.8.0 5.7.0 5.6.0 5.5.0 5.4.0 5.3.0 5.2.0 5.1.0 5.0.0 4.6.0 4.5.0 4.4.0 4.3.0 4.2.0 4.1.0 4.0.0 3.15.1 3.14.0 3.13.0 3.12.0 3.11.0 3.10.0 3.9.0 3.8.0 3.7.0 3.6.0 3.5.0

    public sealed class ManagedSession

Reference documentation and code samples for the Google Cloud Spanner v1 API class ManagedSession.

Manages an underlying Spanner multiplex session and its lifecycle.

## Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) \> ManagedSession

## Inherited Members

[object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Namespace

[Google.Cloud.Spanner.V1](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.V1/latest/Google.Cloud.Spanner.V1)

## Assembly

Google.Cloud.Spanner.V1.dll

## Constructors

### ManagedSession(ManagedSessionOptions)

    public ManagedSession(ManagedSessionOptions options)

Creates a [ManagedSession](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.V1/latest/Google.Cloud.Spanner.V1.ManagedSession) for the given `options`.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `options` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.V1/latest/Google.Cloud.Spanner.V1.ManagedSessionOptions` |

## Methods

### BatchWriteAsync(BatchWriteRequest, CallSettings)

    public Task<AsyncResponseStream<BatchWriteResponse>> BatchWriteAsync(BatchWriteRequest request, CallSettings callSettings)

Executes a BatchWrite RPC asynchronously, returning a stream of responses.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `request` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.V1/latest/Google.Cloud.Spanner.V1.BatchWriteRequest` The batch write request. Must not be null. |
| `callSettings` | `https://cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.CallSettings.html` If not null, applies overrides to this RPC call. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1https://cloud.google.com/dotnet/docs/reference/Google.Api.Gax/latest/Google.Api.Gax.Grpc.AsyncResponseStream-1.htmlhttps://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.V1/latest/Google.Cloud.Spanner.V1.BatchWriteResponse` | A task representing the asynchronous operation. When the task completes, the result is the response stream of [BatchWriteResponse](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.V1/latest/Google.Cloud.Spanner.V1.BatchWriteResponse) objects. |

**Remarks** This method modifies the `request` to include the session name from this managed session.

### BeginTransactionAsync(TransactionOptions, bool, bool, CancellationToken)

    public Task<ManagedTransaction> BeginTransactionAsync(TransactionOptions transactionOptions, bool singleUse, bool shared, CancellationToken cancellationToken)

Creates a [ManagedTransaction](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.V1/latest/Google.Cloud.Spanner.V1.ManagedTransaction) associated to this session.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `transactionOptions` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.V1/latest/Google.Cloud.Spanner.V1.TransactionOptions` |
| `singleUse` | `https://learn.microsoft.com/dotnet/api/system.boolean` |
| `shared` | `https://learn.microsoft.com/dotnet/api/system.boolean` |
| `cancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.V1/latest/Google.Cloud.Spanner.V1.ManagedTransaction` |   |

**Remarks** Note that obtaining a [ManagedTransaction](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.V1/latest/Google.Cloud.Spanner.V1.ManagedTransaction) won't immediately start a Spanner transaction.
Instead, a [ManagedTransaction](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.V1/latest/Google.Cloud.Spanner.V1.ManagedTransaction) will start a Spanner transaction when needed, usually when
exeucting the first command, and will manage the Spanner transaction lifecycle.

### EnsureFreshAsync(CancellationToken)

    public Task EnsureFreshAsync(CancellationToken cancellationToken)

Ensures the underlying Spanner session is fresh.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `cancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task` |   |

**Remarks** Note that session freshness and lifecycle is handled automatically, so this method rarely needs to be called,
except maybe after the [ManagedSession](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.V1/latest/Google.Cloud.Spanner.V1.ManagedSession) has been created or has been idle for a long time, and before
executing the first command, to make certain refreshing the session does not penalize, latency wise, the first few commands.