The transaction object is used to identify a running transaction. It is created by calling Sequelize.transaction(). To run a query under a transaction, you should pass the transaction in the options object.
| Static Public Members | ||
|---|---|---|
| public static get | ISOLATION_LEVELS: {"READ_UNCOMMITTED": string, "READ_COMMITTED": string, "REPEATABLE_READ": string, "SERIALIZABLE": string} Isolation levels can be set per-transaction by passing | |
| public static get | Possible options for row locking. | |
| public static get | Types can be set per-transaction by passing | |
| Public Constructor | ||
|---|---|---|
| public | constructor(sequelize: Sequelize, options: object) Creates a new transaction instance | |
| Public Members | ||
|---|---|---|
| public get | LOCK: * Please see Transaction.LOCK | |
| Public Methods | ||
|---|---|---|
| public | afterCommit(fn: Function) A hook that is run after a transaction is committed | |
| public | Commit the transaction | |
| public | async forceCleanup() Kills the connection this transaction uses. | |
| public | async prepareEnvironment(useCLS: boolean): Promise Called to acquire a connection to use and set the correct options on the connection. | |
| public | Rollback (abort) the transaction | |
Isolation levels can be set per-transaction by passing options.isolationLevel to sequelize.transaction. Sequelize uses the default isolation level of the database, you can override this by passing options.isolationLevel in Sequelize constructor options.
Pass in the desired level as the first argument:
| Name | Type | Attribute | Description |
|---|---|---|---|
| READ_UNCOMMITTED | * | ||
| READ_COMMITTED | * | ||
| REPEATABLE_READ | * | ||
| SERIALIZABLE | * |
try {
const result = await sequelize.transaction({isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.SERIALIZABLE}, transaction => {
// your transactions
});
// transaction has been committed. Do something after the commit if required.
} catch(err) {
// do something with the err.
} Possible options for row locking. Used in conjunction with find calls:
| Name | Type | Attribute | Description |
|---|---|---|---|
| UPDATE | * | ||
| SHARE | * | ||
| KEY_SHARE | * |
Postgres 9.3+ only | |
| NO_KEY_UPDATE | * |
Postgres 9.3+ only |
| object |
| Name | Type | Attribute | Description |
|---|---|---|---|
| UPDATE | * | ||
| SHARE | * | ||
| KEY_SHARE | * |
Postgres 9.3+ only | |
| NO_KEY_UPDATE | * |
Postgres 9.3+ only |
// t1 is a transaction
Model.findAll({
where: ...,
transaction: t1,
lock: t1.LOCK...
}); UserModel.findAll({
where: ...,
include: [TaskModel, ...],
transaction: t1,
lock: {
level: t1.LOCK...,
of: UserModel
}
});
# UserModel will be locked but TaskModel won't! // t1 is a transaction
Model.findAll({
where: ...,
transaction: t1,
lock: true,
skipLocked: true
});
# The query will now return any rows that aren't locked by another transaction Types can be set per-transaction by passing options.type to sequelize.transaction. Default to DEFERRED but you can override the default type by passing options.transactionType in new Sequelize. Sqlite only.
Pass in the desired level as the first argument:
| Name | Type | Attribute | Description |
|---|---|---|---|
| DEFERRED | * | ||
| IMMEDIATE | * | ||
| EXCLUSIVE | * |
try {
await sequelize.transaction({ type: Sequelize.Transaction.TYPES.EXCLUSIVE }, transaction => {
// your transactions
});
// transaction has been committed. Do something after the commit if required.
} catch(err) {
// do something with the err.
} Creates a new transaction instance
| Name | Type | Attribute | Description |
|---|---|---|---|
| sequelize | Sequelize |
A configured sequelize Instance | |
| options | object |
An object with options | |
| options.type | string |
|
Sets the type of the transaction. Sqlite only |
| options.isolationLevel | string |
|
Sets the isolation level of the transaction. |
| options.deferrable | string |
|
Sets the constraints to be deferred or immediately checked. PostgreSQL only |
Please see Transaction.LOCK
A hook that is run after a transaction is committed
| Name | Type | Attribute | Description |
|---|---|---|---|
| fn | Function |
A callback function that is called with the committed transaction |
Kills the connection this transaction uses. Used as a last resort, for instance because COMMIT or ROLLBACK resulted in an error and the transaction is left in a broken state, and releasing the connection to the pool would be dangerous.
Called to acquire a connection to use and set the correct options on the connection. We should ensure all of the environment that's set up is cleaned up in cleanup() below.
| Name | Type | Attribute | Description |
|---|---|---|---|
| useCLS | boolean |
Defaults to true: Use CLS (Continuation Local Storage) with Sequelize. With CLS, all queries within the transaction callback will automatically receive the transaction object. |
| Promise |
Copyright © 2014–present Sequelize contributors
Licensed under the MIT License.
https://sequelize.org/api/v6/class/src/transaction.js~Transaction.html