MysqlTransaction

MysqlTransaction

MysqlTransaction

Function

MySQL transaction handle. Intra-transaction SQL reuses the same underlying transaction in sequence, which is suitable for multiple SQLs that require atomic commit or rollback.

Syntax

Method

MethodParametersReturn valueDescription
query(sql)sql:StringMysqlTransactionSet intra-transaction SQL.
bind(value1, value2, ...)Any valueMysqlTransactionAppend binding parameters.
all()NoneArrayExecute a query and return multiple rows.
one()NoneObject/emptyExecute the query and return a single row; returns empty if there are no results.
exec()NoneObjectExecute writing SQL and return SQL execution statistics object; fields are shown below.
commit()NoneBoolCommit the transaction and return true on success.
rollback()NoneBoolRollback the transaction, return true on success.
close()NoneBoolThe active transaction will be rolled back and closed; returns false if it has ended.
status()NoneStringReturns active, committed, rolled_back, closed, or failed.
sql(render_binds)Bool, optionalStringReturns the SQL preview text; default render bind parameters.

Return value

TypeDescription
MysqlTransactionquery/bind Returns a new lightweight builder, but sharing the same transaction state.
Arrayall Returns an array of row objects. For row object fields, see "Query row object fields" below.
Object/Emptyone returns a single row object when there is a result; returns empty when there is no result. For row object fields, see "Query row object fields" below.
Objectexec Returns the SQL execution statistics object. See "exec returns object fields" below for fields.
BoolStatus results of commit, rollback, close.
StringThe text result of status or sql.

Query row object fields

The row objects returned by all() and one() are dynamic objects with fields derived from SQL result set column names.

FieldTypeMust existDescription
Any column nameAnyNoThe column value returned by the database. The field name is the result set column name; when the field does not exist, the read result is empty. Database NULL returns null.

exec returns object fields

FieldTypeMust existDescription
totalIntYesThe number of bound rows processed by exec() within this transaction. binds() batch execution is not currently supported within a transaction, so it is currently fixed to 1.
rows_affectedIntYesThe number of affected rows reported by the database.
last_insert_idIntYesThe last insert ID reported by the database.
batch_countIntYesThe actual number of batches split executed. Batch execution is not currently supported within a transaction, so it is currently fixed to 1.
batch_sizeIntYesThe batch size for the current execution. batch() is not currently supported within transactions, so it is currently 0.
workersIntYesThe number of concurrent workers used by the current execution. The same underlying transaction is reused sequentially within a transaction, so currently 1.

Code Example

Notes

  • Do not reuse the same transaction object concurrently; an error will be reported if used again during transaction execution.
  • Batch execution configuration of binds, batch, and workers is not currently supported within the transaction.
  • Uncommitted transactions should call rollback or close; SQLx will attempt to rollback when the object is discarded, but the script should explicitly close the resource.
  • BT.stats().mysql.transactions_active can be used to observe the current number of active transactions.