lang.transaction
ballerina/lang.transaction Ballerina library
Module overview
The lang.transaction
module supports transactions.
Functions![](/images/permalink.svg)
getData![](/images/permalink.svg)
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![](/images/permalink.svg)
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![](/images/permalink.svg)
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![](/images/permalink.svg)
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![](/images/permalink.svg)
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![](/images/permalink.svg)
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![](/images/permalink.svg)
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![](/images/permalink.svg)
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![](/images/permalink.svg)
lang.transaction: managerEnabled![](/images/permalink.svg)
Config to enable transaction manager.
lang.transaction: logBase![](/images/permalink.svg)
Config to specify transaction log directory.
lang.transaction: transactionAutoCommitTimeout![](/images/permalink.svg)
Config to specify the timeout for auto commit.
lang.transaction: transactionCleanupTimeout![](/images/permalink.svg)
Config to specify the timeout for cleaning up dead transactions.
Records![](/images/permalink.svg)
lang.transaction: InfoInternal![](/images/permalink.svg)
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![](/images/permalink.svg)
lang.transaction: Error![](/images/permalink.svg)
Type of error returned by commit action.
Object types![](/images/permalink.svg)
lang.transaction: Timestamp![](/images/permalink.svg)
An instant in time.
toMillisecondsInt![](/images/permalink.svg)
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![](/images/permalink.svg)
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![](/images/permalink.svg)
lang.transaction: Info![](/images/permalink.svg)
Info
Information about a transaction that does not change after the transaction is started.