lang.transaction
ballerina/lang.transaction Ballerina library
Module overview
The lang.transaction
module supports transactions.
Functions
getData
function getData() returns readonly
Retrieves data associated with the current transaction branch.
The data is set using setData
.
function createEntity() returns error? { transaction { transaction:setData({accessType: "RO"}); transaction:getData() ⇒ {"accessType":"RO"} check commit; } } transactional function updateDB() { transaction:setData({accessType: "RO"}); transaction:getData() ⇒ {"accessType":"RO"} }
Return Type
- readonly - the data, or
()
if no data has been set.
getInfo
function getInfo(byte[] xid) returns Info?
Returns information about the transaction with the specified xid.
byte[] xid = [48, 53, 101, 102, 101, 55]; transaction:getInfo(xid) ⇒ {"xid":[48, 53, 101, 102, 101, 55],"retryNumber":0,"prevAttempt":null,"startTime":2022-12-20 16:03:37,228}
Parameters
- xid byte[] - transaction id
Return Type
- Info? - information about the transaction
getRollbackOnly
function getRollbackOnly() returns boolean
Tells whether it is known that the transaction will be rolled back.
function createEntity() returns error? { transaction { transaction:setRollbackOnly(error("marked as rollback only")); transaction:getRollbackOnly() ⇒ true check commit; } } transactional function updateDB() { transaction:getRollbackOnly() ⇒ false }
Return Type
- boolean - true if it is known that the transaction manager will, when it makes the decision whether to commit or rollback, decide to rollback
info
function info() returns Info
Returns information about the current transaction.
function createEntity() returns error? { transaction { transaction:Info info = transaction:info(); check commit; } } transactional function updateDB() { transaction:Info info = transaction:info(); info.xid ⇒ [100,102,53,51,97,57,57,51,45] }
Return Type
- Info - information about the current transaction
onCommit
function onCommit(CommitHandler handler)
Adds a handler to be called if and when the global transaction commits.
function createEntity() returns error? { transaction { transaction:onCommit(onCommitHandle); check commit; } } transactional function updateDB() { transaction:onCommit(onCommitHandle); } isolated function onCommitHandle(transaction:Info info) { // Include the code to be executed when the transaction commits. }
Parameters
- handler CommitHandler - the function to be called on commit
onRollback
function onRollback(RollbackHandler handler)
Adds a handler to be called if and when the global transaction rolls back.
function createEntity() returns error? { transaction { transaction:onRollback(onRollBackHandle); check commit; } } transactional function updateDB() { transaction:onRollback(onRollBackHandle); } isolated function onRollBackHandle(transaction:Info info, error? cause, boolean willRetry) { // Include the code to be executed when the transaction rollback. }
Parameters
- handler RollbackHandler - the function to be called on rollback
setData
function setData(readonly data)
Associates some data with the current transaction branch.
function createEntity() returns error? { transaction { transaction:setData({accessType: "RO"}); check commit; } } transactional function updateDB() { transaction:setData({accessType: "RO"}); }
Parameters
- data readonly - Data to be set
setRollbackOnly
function setRollbackOnly(error? e)
Prevents the global transaction from committing successfully.
This ask the transaction manager that when it makes the decision whether to commit or rollback, it should decide to rollback.
function createEntity() returns error? { transaction { transaction:setRollbackOnly(error("marked as rollback only")); check commit; } } transactional function updateDB() { transaction:setRollbackOnly(error("marked as rollback only")); }
Parameters
- e error? - the error that caused the rollback or
()
, if there is none
Configurables
lang.transaction: managerEnabled
Config to enable transaction manager.
lang.transaction: logBase
Config to specify transaction log directory.
lang.transaction: transactionAutoCommitTimeout
Config to specify the timeout for auto commit.
lang.transaction: transactionCleanupTimeout
Config to specify the timeout for cleaning up dead transactions.
Records
lang.transaction: InfoInternal
Internally used record to hold information about a transaction.
Fields
- xid byte[] - Unique identifier for the transaction branch.
- retryNumber int - The number of previous attempts in a sequence of retries.
- prevAttempt Info? - Information about the previous attempt in a sequence of retries.
This will be
()
if theretryNumber
is 0.
- startTime Timestamp - The time at which the transaction was started.
Errors
lang.transaction: Error
Type of error returned by commit action.
Object types
lang.transaction: Timestamp
An instant in time.
toMillisecondsInt
function toMillisecondsInt() returns int
Returns milliseconds since 1970-01-01T00:00:00Z, not including leap seconds
Return Type
- int - milliseconds since 1970-01-01T00:00:00Z, not including leap seconds
toString
function toString() returns string
Returns a string representation of the timestamp in ISO 8601 format
Return Type
- string - string representation of the timestamp in ISO 8601 format
Intersection types
lang.transaction: Info
Info
Information about a transaction that does not change after the transaction is started.