Skip to content

Commit

Permalink
DA-452: Adding _commit example, suggest await syntax for commit/rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
ejscribner committed Jun 7, 2024
1 parent 561472f commit 530d5ec
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions docusaurus/docs/advanced/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,38 @@ Keep a sharp eye on it!

### Rollback

To trigger a `rollback` manually you should execute `ctx._rollback` function.
To trigger a `rollback` manually you should execute the `ctx._rollback` function.

```typescript
await otttoman.$transactions(async (ctx: TransactionAttemptContext) => {
const odette = Swan.create({ name: 'Odette', age: 30 }, { transactionContext: ctx });
ctx._rollback();
await ctx._rollback();
})
```

This way you are canceling the transaction, so no changes inside the `$transaction` function will be committed.

:::info Important
**Be sure to `await` the `ctx._rollback()`**, instead of using promise chaining. Not doing so can lead to unexpected results and race conditions.
:::

### Commit

To manually commit the transaction, you should execute the `ctx._commit` function.

```typescript
await otttoman.$transactions(async (ctx: TransactionAttemptContext) => {
const odette = Swan.create({ name: 'Gloria', age: 26 }, { transactionContext: ctx });
await ctx._commit();
})
```

This will commit the changes inside the `$transaction` function.

:::info Important
**Be sure to `await` the `ctx._commit()`**, instead of using promise chaining. Not doing so can lead to unexpected results and race conditions.
:::

### Handle Error

While creating a transaction you always should wrap it inside a `try catch` block and handle the exceptions.
Expand Down

0 comments on commit 530d5ec

Please sign in to comment.