Skip to main content

OperationTransaction

@webda/core


Class: OperationTransaction

Defined in: packages/core/src/core/transactions.ts:56

The transaction of an operation (or of runWithTransaction): the participants enlisted in it and their state

It follows the async call chain of the operation, independently of the execution context: code switching to another context inside the operation (runAsSystem, a nested callOperation) stays in the transaction.

Constructors​

Constructor​

new OperationTransaction(context, operation?): OperationTransaction

Defined in: packages/core/src/core/transactions.ts:79

Create a new OperationTransaction instance

Parameters​

context​

Context

the execution context

operation?​

OperationDefinition

the operation, undefined outside an operation

Returns​

OperationTransaction

Properties​

context​

readonly context: Context

Defined in: packages/core/src/core/transactions.ts:80

the execution context


ended​

protected ended: boolean = false

Defined in: packages/core/src/core/transactions.ts:72

True once committed or rolled back


finished​

protected readonly finished: Set<OperationTransactionParticipant>

Defined in: packages/core/src/core/transactions.ts:64

Participants committed or rolled back


operation?​

optional operation?: OperationDefinition

Defined in: packages/core/src/core/transactions.ts:81

the operation, undefined outside an operation


participants​

protected readonly participants: OperationTransactionParticipant[] = []

Defined in: packages/core/src/core/transactions.ts:60

Participants enlisted, in order


slots​

protected readonly slots: Map<string, any>

Defined in: packages/core/src/core/transactions.ts:68

Per-participant state

Methods​

commit()​

commit(): Promise<void>

Defined in: packages/core/src/core/transactions.ts:141

Commit every participant in enlistment order

When a commit fails, the participants not committed yet (the failing one included) are rolled back and the commit error is thrown. The ones already committed stay committed: there is no two-phase commit.

Returns​

Promise<void>


enlist()​

enlist(participant): Promise<void>

Defined in: packages/core/src/core/transactions.ts:127

Enlist a participant and begin it; a participant already enlisted is left as is

The participant is enlisted before begin so a failing begin still gets a rollback to clean up.

Parameters​

participant​

OperationTransactionParticipant

the participant

Returns​

Promise<void>


get()​

get<T>(key): T

Defined in: packages/core/src/core/transactions.ts:97

Return the state stored by a participant

Type Parameters​

T​

T = any

Parameters​

key​

string

the slot key

Returns​

T

the state stored by a participant


has()​

has(participant): boolean

Defined in: packages/core/src/core/transactions.ts:117

Return true when the participant is enlisted

Parameters​

participant​

OperationTransactionParticipant

the participant

Returns​

boolean

true when the participant is enlisted


isActive()​

isActive(): boolean

Defined in: packages/core/src/core/transactions.ts:88

Check whether active

Returns​

boolean

true until the transaction is committed or rolled back


rollback()​

rollback(error): Promise<void>

Defined in: packages/core/src/core/transactions.ts:162

Roll back every participant not committed yet; their errors are logged, never thrown

Parameters​

error​

unknown

the error failing the transaction

Returns​

Promise<void>


run()​

run<T>(fn): T

Defined in: packages/core/src/core/transactions.ts:183

Run a function inside this transaction: useOperationTransaction() returns it in the whole async chain

Type Parameters​

T​

T

Parameters​

fn​

() => T

the function

Returns​

T

the function result


set()​

set(key, value): this

Defined in: packages/core/src/core/transactions.ts:107

Store a participant state

Parameters​

key​

string

the slot key, e.g. sql.tx.<store name>

value​

any

the state

Returns​

this

this