Skip to content

informat.transaction Database Transaction Operations

Overview

Use informat.transaction to perform transaction-related operations

defaultTransactionDefinition

Create default transaction definition

javascript
informat.transaction.defaultTransactionDefinition();

Return Value Default transaction definition, type is TransactionDefinition


currentTransactionStatus

Get current transaction status

js
informat.transaction.currentTransactionStatus();

Return Value

Current transaction status, type is TransactionStatus


getTransactionStatus

Get transaction status based on the given transaction definition (TransactionDefinition)

js
informat.transaction.getTransactionStatus(td);
ParameterTypeDescription
tdTransactionDefinitionTransaction definition

Return Value

Transaction status, type is TransactionStatus

commit

Commit transaction

js
informat.transaction.commit(ts);
ParameterTypeDescription
tsTransactionStatusTransaction status

rollback

Rollback transaction

js
informat.transaction.rollback(ts);
ParameterTypeDescription
tsTransactionStatusTransaction status

TransactionDefinition

setPropagationBehavior

Set transaction propagation behavior

js
td.setPropagationBehavior(pb);
ParameterTypeDescription
pbIntegerTransaction propagation behavior

Transaction propagation behaviors are as follows:

Propagation TypeValueDescription
PROPAGATION_REQUIRED0If no transaction exists, create a new one; if a transaction already exists, join it. This is the default propagation behavior.
PROPAGATION_SUPPORTS1If a transaction exists, join it; if none exists, execute non-transactionally.
PROPAGATION_MANDATORY2If a transaction exists, join it; if none exists, throw an exception.
PROPAGATION_REQUIRES_NEW3Always create a new transaction. If a current transaction exists, suspend it and resume it after the new transaction completes.
PROPAGATION_NOT_SUPPORTED4Execute non-transactionally. If a current transaction exists, suspend it and resume it after the operation completes.
PROPAGATION_NEVER5Execute non-transactionally. If a current transaction exists, throw an exception.
PROPAGATION_NESTED6If a current transaction exists, execute within a nested transaction; otherwise create a new one. The nested transaction can be rolled back independently.

setPropagationBehaviorName

Set transaction propagation behavior name

js
td.setPropagationBehaviorName(pbName);

Example:

js
td.setPropagationBehaviorName("PROPAGATION_REQUIRED");

getPropagationBehavior

Get transaction propagation behavior

js
td.getPropagationBehavior();

Return Value

Transaction propagation behavior, type is Integer


setIsolationLevel

Set transaction isolation level

js
td.setIsolationLevel(isolationLevel);
ParameterTypeDescription
isolationLevelIntegerTransaction isolation level

The isolation levels are defined as follows:

Isolation LevelValueDescription
ISOLATION_DEFAULT-1Use the default isolation level of the underlying data source.
ISOLATION_READ_UNCOMMITTED0Read uncommitted data; allows dirty reads, non-repeatable reads, and phantom reads.
ISOLATION_READ_COMMITTED1Read committed data; prevents dirty reads, but allows non-repeatable reads and phantom reads.
ISOLATION_REPEATABLE_READ2Repeatable read; prevents dirty and non-repeatable reads, but allows phantom reads.
ISOLATION_SERIALIZABLE3Serializable; prevents dirty reads, non-repeatable reads, and phantom reads.

setIsolationLevelName

Set transaction isolation level name

js
td.setIsolationLevelName(isolationLevelName);

Example:

js
td.setIsolationLevelName("ISOLATION_READ_UNCOMMITTED");

getIsolationLevel

Get transaction isolation level

js
td.getIsolationLevel();

Return Value

Transaction isolation level, type is Integer


setTimeout

Set transaction timeout

js
td.setTimeout(seconds);
ParameterTypeDescription
secondsIntegerTransaction timeout in seconds

Example:

js
var td = informat.transaction.defaultTransactionDefinition();
td.setTimeout(30);

In this example we create a default TransactionDefinition object and call setTimeout to set the transaction timeout to 30 seconds. If the transaction has not completed within 30 seconds, the transaction manager will abort it and roll back.


getTimeout

Get transaction timeout

js
td.getTimeout();

Return Value Transaction timeout in seconds, type is Integer


setReadOnly

Set transaction read-only

js
td.setReadOnly(readOnly);
ParameterTypeDescription
readOnlyBooleanWhether the transaction is read-only

Read-only transactions are typically used for queries, as they perform no data modifications. Marking a transaction as read-only can improve performance, since some databases apply optimizations specifically for read-only transactions.


isReadOnly

Return whether the transaction is read-only

js
td.isReadOnly();

Return Value Whether the transaction is read-only, type is Boolean


setName

Set transaction name

js
td.setName(name);
ParameterTypeDescription
nameStringTransaction name

A transaction name helps developers quickly identify and trace transactions in logs and monitoring tools.

getName

Get transaction name

js
td.getName();

Return Value Transaction name, type is String

TransactionStatus

isNewTransaction

Return whether the transaction is new

js
ts.isNewTransaction();

Return Value Whether the transaction is new, type is Boolean

isRollbackOnly

Return whether the transaction is rollback-only

js
ts.isRollbackOnly();

Return Value Whether the transaction is rollback-only, type is Boolean

isCompleted

Return whether the transaction is completed

js
ts.isCompleted();

Return Value Whether the transaction is completed, type is Boolean

setRollbackOnly

Set transaction rollback-only

js
ts.setRollbackOnly();

Example

js
var ts = informat.transaction.currentTransactionStatus(); // Get the current transaction status
informat.transaction.commit(ts); // Commit the current transaction
//
var td = informat.transaction.defaultTransactionDefinition(); // Create a default transaction definition
td.setPropagationBehaviorName("PROPAGATION_REQUIRES_NEW"); // Modify the transaction propagation behavior to new transaction
var ts = informat.transaction.getTransactionStatus(td); // Get the transaction status
try {
  informat.table.insert("task", {
    name: "From Script",
  }); // Insert a record into the table
  informat.transaction.commit(ts); // Commit the transaction
} catch (e) {
  informat.transaction.rollback(ts); // Roll back the transaction if an exception occurs
}

TIP

Note: If you need to start a new transaction during automation so that subsequent steps can run within a transaction, execute the following command:

let td=informat.transaction.defaultTransactionDefinition(); td.setPropagationBehaviorName('PROPAGATION_REQUIRES_NEW'); let ts=informat.transaction.getTransactionStatus(td);

In the code snippet above, after executing these three commands, subsequent automation steps will run within a new transaction.

registerSynchronization

Register transaction synchronization callback

javascript
informat.transaction.registerSynchronization(ts);
ParameterTypeDescription
tsTransactionSynchronizationTransaction synchronization callback object

TransactionSynchronization Transaction synchronization callback object contains the following configurable listener functions:

beforeCommit

Type: Function Trigger Time: Before transaction commit Parameter: readOnly (Boolean) - Whether the transaction is read-only

beforeCompletion

Type: Function Trigger Time: Before transaction completion (whether committed or rolled back)

afterCommit

Type: Function Trigger Time: After transaction commit (only when committed)

afterCompletion

Type: Function Trigger Time: After transaction completion (whether committed or rolled back) Parameter: status (Integer) - Completion status (0=committed, 1=rolled back)

Example

js
// Create a synchronization callback object
var sync = {};
// Configure callback functions
sync.beforeCommit = function (readOnly) {
  console.log("Transaction is about to commit, read-only mode:", readOnly);
};

sync.beforeCompletion = function () {
  console.log("Transaction completion pre-processing");
};

sync.afterCommit = function () {
  console.log("Transaction has been successfully committed");
};

sync.afterCompletion = function (status) {
  console.log("Transaction completion status:", status === 0 ? "Commit" : "Rollback");
};

// Register the callback
informat.transaction.registerSynchronization(sync);