Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/other-topics/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,44 @@ const sequelize = new Sequelize('sqlite::memory:', {

**Note for MSSQL:** _The `SET ISOLATION LEVEL` queries are not logged since the specified `isolationLevel` is passed directly to `tedious`._

## Manually retrying the failed transactions

Sequelize does not automically retries on potential write conflicts or deadlocks, when using a certain level of isolations, it is expected that transactions may fail due to potential write conflicts in concurrent transactions or deadlocks.

You can programmatically fix this by relying on options from the [retry-as-promised](https://github.com/mickhansen/retry-as-promised/blob/master/README.md) library:

```typescript
const { Sequelize } = require('@sequelize/core');

const sequelize = new Sequelize(process.env.DB_NAME,
process.env.DB_NAME,
process.env.DB_PASS,
{
host: process.env.DB_HOST,
dialect: 'mysql',
// Here:
retry: {
max: 3, // this may cause memory leaks if you set the retries limit too much.
match: [
Sequelize.ConnectionError,
Sequelize.ConnectionRefusedError,
Sequelize.ConnectionTimedOutError,
Sequelize.TimeoutError,
/Deadlock/i, // retry-as-promised also takes RegExp, for example to find deadlocks error.
],
}
});
```

You can use other possible error classes [here](https://sequelize.org/api/v7/classes/error).


:::caution

Be careful when manually retrying the transactions, this can lead to retries for the same reason (possible memory leak if you set `max` option value to a large number or `Infinity`), always check what was thrown.

:::

## Usage with other sequelize methods

The `transaction` option goes with most other options, which are usually the first argument of a method.
Expand Down