informat.transaction Database Transaction Operations
Overview
Use informat.transaction to perform transaction-related operations
defaultTransactionDefinition
Create default transaction definition
informat.transaction.defaultTransactionDefinition();Return Value Default transaction definition, type is TransactionDefinition
currentTransactionStatus
Get current transaction status
informat.transaction.currentTransactionStatus();Return Value
Current transaction status, type is TransactionStatus
getTransactionStatus
Get transaction status based on the given transaction definition (TransactionDefinition)
informat.transaction.getTransactionStatus(td);| Parameter | Type | Description |
|---|---|---|
| td | TransactionDefinition | Transaction definition |
Return Value
Transaction status, type is TransactionStatus
commit
Commit transaction
informat.transaction.commit(ts);| Parameter | Type | Description |
|---|---|---|
| ts | TransactionStatus | Transaction status |
rollback
Rollback transaction
informat.transaction.rollback(ts);| Parameter | Type | Description |
|---|---|---|
| ts | TransactionStatus | Transaction status |
TransactionDefinition
setPropagationBehavior
Set transaction propagation behavior
td.setPropagationBehavior(pb);| Parameter | Type | Description |
|---|---|---|
| pb | Integer | Transaction propagation behavior |
Transaction propagation behaviors are as follows:
| Propagation Type | Value | Description |
|---|---|---|
| PROPAGATION_REQUIRED | 0 | If no transaction exists, create a new one; if a transaction already exists, join it. This is the default propagation behavior. |
| PROPAGATION_SUPPORTS | 1 | If a transaction exists, join it; if none exists, execute non-transactionally. |
| PROPAGATION_MANDATORY | 2 | If a transaction exists, join it; if none exists, throw an exception. |
| PROPAGATION_REQUIRES_NEW | 3 | Always create a new transaction. If a current transaction exists, suspend it and resume it after the new transaction completes. |
| PROPAGATION_NOT_SUPPORTED | 4 | Execute non-transactionally. If a current transaction exists, suspend it and resume it after the operation completes. |
| PROPAGATION_NEVER | 5 | Execute non-transactionally. If a current transaction exists, throw an exception. |
| PROPAGATION_NESTED | 6 | If 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
td.setPropagationBehaviorName(pbName);Example:
td.setPropagationBehaviorName("PROPAGATION_REQUIRED");getPropagationBehavior
Get transaction propagation behavior
td.getPropagationBehavior();Return Value
Transaction propagation behavior, type is Integer
setIsolationLevel
Set transaction isolation level
td.setIsolationLevel(isolationLevel);| Parameter | Type | Description |
|---|---|---|
| isolationLevel | Integer | Transaction isolation level |
The isolation levels are defined as follows:
| Isolation Level | Value | Description |
|---|---|---|
| ISOLATION_DEFAULT | -1 | Use the default isolation level of the underlying data source. |
| ISOLATION_READ_UNCOMMITTED | 0 | Read uncommitted data; allows dirty reads, non-repeatable reads, and phantom reads. |
| ISOLATION_READ_COMMITTED | 1 | Read committed data; prevents dirty reads, but allows non-repeatable reads and phantom reads. |
| ISOLATION_REPEATABLE_READ | 2 | Repeatable read; prevents dirty and non-repeatable reads, but allows phantom reads. |
| ISOLATION_SERIALIZABLE | 3 | Serializable; prevents dirty reads, non-repeatable reads, and phantom reads. |
setIsolationLevelName
Set transaction isolation level name
td.setIsolationLevelName(isolationLevelName);Example:
td.setIsolationLevelName("ISOLATION_READ_UNCOMMITTED");getIsolationLevel
Get transaction isolation level
td.getIsolationLevel();Return Value
Transaction isolation level, type is Integer
setTimeout
Set transaction timeout
td.setTimeout(seconds);| Parameter | Type | Description |
|---|---|---|
| seconds | Integer | Transaction timeout in seconds |
Example:
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
td.getTimeout();Return Value Transaction timeout in seconds, type is Integer
setReadOnly
Set transaction read-only
td.setReadOnly(readOnly);| Parameter | Type | Description |
|---|---|---|
| readOnly | Boolean | Whether 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
td.isReadOnly();Return Value Whether the transaction is read-only, type is Boolean
setName
Set transaction name
td.setName(name);| Parameter | Type | Description |
|---|---|---|
| name | String | Transaction name |
A transaction name helps developers quickly identify and trace transactions in logs and monitoring tools.
getName
Get transaction name
td.getName();Return Value Transaction name, type is String
TransactionStatus
isNewTransaction
Return whether the transaction is new
ts.isNewTransaction();Return Value Whether the transaction is new, type is Boolean
isRollbackOnly
Return whether the transaction is rollback-only
ts.isRollbackOnly();Return Value Whether the transaction is rollback-only, type is Boolean
isCompleted
Return whether the transaction is completed
ts.isCompleted();Return Value Whether the transaction is completed, type is Boolean
setRollbackOnly
Set transaction rollback-only
ts.setRollbackOnly();Example
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
informat.transaction.registerSynchronization(ts);| Parameter | Type | Description |
|---|---|---|
| ts | TransactionSynchronization | Transaction 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
// 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);
