Transaction
事务对象用于标识正在运行的事务。它通过调用Sequelize.transaction()创建。要在事务下运行查询,应在选项对象中传递事务。
静态成员摘要
| 静态公共成员 | ||
| public static get |
ISOLATION_LEVELS: {"READ_UNCOMMITTED": string, "READ_COMMITTED": string, "REPEATABLE_READ": string, "SERIALIZABLE": string} 隔离级别可以通过将 |
|
| public static get |
行锁定的可能选项。 |
|
| public static get |
类型可以通过将 |
|
构造函数摘要
| 公共构造函数 | ||
| public |
constructor(sequelize: Sequelize, options: object) 创建一个新的事务实例 |
|
成员摘要
| 公共成员 | ||
| public get |
LOCK: * |
|
方法摘要
| 公共方法 | ||
| public |
afterCommit(fn: Function) 一个在事务提交后运行的钩子 |
|
| public |
提交事务 |
|
| public |
async forceCleanup() 终止此事务使用的连接。 |
|
| public |
async prepareEnvironment(useCLS: boolean): Promise 用于获取要使用的连接并在连接上设置正确的选项。 |
|
| public |
回滚(中止)事务 |
|
静态公共成员
public static get ISOLATION_LEVELS: {"READ_UNCOMMITTED": string, "READ_COMMITTED": string, "REPEATABLE_READ": string, "SERIALIZABLE": string} source
隔离级别可以通过将options.isolationLevel传递给sequelize.transaction来为每个事务设置。Sequelize 使用数据库的默认隔离级别,可以通过在 Sequelize 构造函数选项中传递options.isolationLevel来覆盖它。
将所需的级别作为第一个参数传入
属性
| 名称 | 类型 | 属性 | 描述 |
| 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.
}
public static get LOCK: object: {"UPDATE": string, "SHARE": string, "KEY_SHARE": string, "NO_KEY_UPDATE": string} source
行锁定的可能选项。与find调用一起使用
属性
| 名称 | 类型 | 属性 | 描述 |
| UPDATE | * | ||
| SHARE | * | ||
| KEY_SHARE | * | 仅限 Postgres 9.3+ |
|
| NO_KEY_UPDATE | * | 仅限 Postgres 9.3+ |
返回值
| object |
返回属性
| 名称 | 类型 | 属性 | 描述 |
| UPDATE | * | ||
| SHARE | * | ||
| KEY_SHARE | * | 仅限 Postgres 9.3+ |
|
| NO_KEY_UPDATE | * | 仅限 Postgres 9.3+ |
示例
// 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
public static get TYPES: {"DEFERRED": string, "IMMEDIATE": string, "EXCLUSIVE": string} source
类型可以通过将options.type传递给sequelize.transaction来为每个事务设置。默认设置为DEFERRED,但可以通过在new Sequelize中传递options.transactionType来覆盖默认类型。仅限 Sqlite。
将所需的级别作为第一个参数传入
属性
| 名称 | 类型 | 属性 | 描述 |
| 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.
}
公共成员
公共方法
public async forceCleanup() source
结束此事务使用的连接。 作为最后的手段,例如,由于 COMMIT 或 ROLLBACK 导致错误,并且事务处于损坏状态,将连接释放回连接池将很危险。
指南 参考 源代码
