diff --git a/docs/other-topics/transactions.md b/docs/other-topics/transactions.md index 054b8f6c6..900d6fb9b 100644 --- a/docs/other-topics/transactions.md +++ b/docs/other-topics/transactions.md @@ -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.