# Multiple sessions

The .NET SDK supports multiple isolated sessions within a single connection, each with their own authentication and context.

The .NET SDK allows you to create multiple isolated sessions within a single connection. Each session maintains its own namespace, database, variables, and authentication state, while sharing the underlying connection to SurrealDB. This is useful when different parts of your application need to operate under different credentials or contexts simultaneously.

<table>
  <thead>
    <tr>
      <th scope="col">Method</th>
      <th scope="col">Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td scope="row" data-label="Method">
        <a href="#sessions">
          <code> db.Sessions() </code>
        </a>
      </td>
      <td scope="row" data-label="Description">
        List all active sessions on the current connection.
      </td>
    </tr>
    <tr>
      <td scope="row" data-label="Method">
        <a href="#createsession">
          <code> db.CreateSession() </code>
        </a>
      </td>
      <td scope="row" data-label="Description">
        Create a new isolated session on the current connection.
      </td>
    </tr>
    <tr>
      <td scope="row" data-label="Method">
        <a href="#forksession">
          <code> session.ForkSession() </code>
        </a>
      </td>
      <td scope="row" data-label="Description">
        Creates a copy of a session, inheriting its state.
      </td>
    </tr>
    <tr>
      <td scope="row" data-label="Method">
        <a href="#closesession">
          <code> session.CloseSession() </code>
        </a>
      </td>
      <td scope="row" data-label="Description">
        Closes the current session and disposes of it. After this method is
        called, the session cannot be used again.
      </td>
    </tr>
  </tbody>
</table>

## `.Sessions()` {#sessions}

Returns a list of all active sessions on the current connection.

```csharp title="Method Syntax"
await db.Sessions(cancellationToken)
```

### Arguments

<table>
  <thead>
    <tr>
      <th colspan="2" scope="col">
        Arguments
      </th>
      <th colspan="2" scope="col">
        Description
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="2" scope="row" data-label="Arguments">
        <code>cancellationToken</code>
        <label label="optional" />
      </td>
      <td colspan="2" scope="row" data-label="Description">
        The cancellationToken enables graceful cancellation of asynchronous
        operations.
      </td>
    </tr>
  </tbody>
</table>

### Example usage

```csharp
// List all active sessions on the current connection
IEnumerable<Guid> sessions = await db.Sessions();
```

<br />

## `.CreateSession()` {#createsession}

Creates a new isolated session on the current connection. The new session is independent and maintains its own namespace, database, variables, and authentication state.

```csharp title="Method Syntax"
await db.CreateSession(cancellationToken)
```

### Arguments

<table>
  <thead>
    <tr>
      <th colspan="2" scope="col">
        Arguments
      </th>
      <th colspan="2" scope="col">
        Description
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="2" scope="row" data-label="Arguments">
        <code>cancellationToken</code>
        <label label="optional" />
      </td>
      <td colspan="2" scope="row" data-label="Description">
        The cancellationToken enables graceful cancellation of asynchronous
        operations.
      </td>
    </tr>
  </tbody>
</table>

### Example usage

```csharp
// Create a new isolated session on the same connection
var session = await db.CreateSession();

// Use the session independently
await session.Use("my_namespace", "my_database");
await session.SignIn(new RootAuth { Username = "root",
    Password = "secret" });
```

<br />

## `.ForkSession()` {#forksession}

Creates a copy of the current session, inheriting its namespace, database, variables, and authentication state. Changes made in the forked session do not affect the original.

```csharp title="Method Syntax"
await session.ForkSession(cancellationToken)
```

### Arguments

<table>
  <thead>
    <tr>
      <th colspan="2" scope="col">
        Arguments
      </th>
      <th colspan="2" scope="col">
        Description
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="2" scope="row" data-label="Arguments">
        <code>cancellationToken</code>
        <label label="optional" />
      </td>
      <td colspan="2" scope="row" data-label="Description">
        The cancellationToken enables graceful cancellation of asynchronous
        operations.
      </td>
    </tr>
  </tbody>
</table>

### Example usage

```csharp
// Fork the current session to create an independent copy
var forkedSession = await session.ForkSession();

// The forked session inherits namespace, database, and auth state
// Changes made here do not affect the original session
await forkedSession.Set("x", 42);
```

<br />

## `.CloseSession()` {#closesession}

Closes the current session and disposes of it. After this method is called, the session cannot be used again.

> [!NOTE]
> `CloseSession()` only closes the session itself. The underlying connection shared with the parent `SurrealDbClient` remains open.

```csharp title="Method Syntax"
await session.CloseSession(cancellationToken)
```

### Arguments

<table>
  <thead>
    <tr>
      <th colspan="2" scope="col">
        Arguments
      </th>
      <th colspan="2" scope="col">
        Description
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="2" scope="row" data-label="Arguments">
        <code>cancellationToken</code>
        <label label="optional" />
      </td>
      <td colspan="2" scope="row" data-label="Description">
        The cancellationToken enables graceful cancellation of asynchronous
        operations.
      </td>
    </tr>
  </tbody>
</table>

### Example usage

```csharp
// Close and dispose of the session when done
await session.CloseSession();

// The session can no longer be used after this call
```
