﻿# Transactional Session with DynamoDB Persistence

<!-- Version variant: dynamots_3; default: [/nservicebus/transactional-session/persistences/dynamodb.md](/nservicebus/transactional-session/persistences/dynamodb.md) -->


To use the [transactional session feature](/nservicebus/transactional-session/index.md) with DynamoDB persistence, add a reference to the `NServiceBus.Persistence.DynamoDB.TransactionalSession` NuGet package.

## Configuration

To enable the transactional session feature:

<!-- snippet: enabling-transactional-session-dynamo -->

```cs
var persistence = config.UsePersistence<DynamoPersistence>();
persistence.EnableTransactionalSession();
```

<!-- endsnippet -->

## Opening a session

To open a DynamoDB transactional session:

<!-- snippet: open-transactional-session-dynamo -->

```cs
using var childScope = serviceProvider.CreateScope();
var session = childScope.ServiceProvider.GetService<ITransactionalSession>();
await session.Open(new DynamoOpenSessionOptions());

// use the session

await session.Commit();
```

<!-- endsnippet -->

## Transactions usage

Message and database operations made with the transactional session are committed together once the session is committed:

<!-- snippet: use-transactional-session-dynamo -->

```cs
await session.Open(new DynamoOpenSessionOptions());

// add messages to the transaction:
await session.Send(new MyMessage());

// access the database:
var dynamoSession = session.SynchronizedStorageSession.DynamoPersistenceSession();

await session.Commit();
```

<!-- endsnippet -->

For further details about using transactions, see the [DynamoDB persistence transactions documentation](/persistence/dynamodb/transactions.md).

> [!WARNING]
> In order to guarantee atomic consistency across message and database operations, the [outbox](/nservicebus/outbox/index.md) must be enabled. Otherwise `Commit` executes database modifications first and then messages are dispatched with best-effort.
