交易
交易物件用於識別正在執行的交易。它透過呼叫 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: * 請參閱 Transaction.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+ |
範例
// 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 導致錯誤,並且交易處於損毀狀態,而且將連線釋放到集區可能會很危險。