# mysql.begin ## Function Opens a MySQL transaction and returns the `MysqlTransaction` transaction handle. ## Syntax ```bt tx = mysql(dsn).begin() ``` ## Parameters No parameters. DSN provided by `mysql(dsn)`. ## Return value | Type | Description | | ------ | ------ | | MysqlTransaction | Transaction handle. Query, bind, all, one, exec, commit, rollback, close and status can continue to be called within the transaction. | ## Code Example ```bt db = mysql('mysql://user:pass@127.0.0.1/test') tx = db.begin() ret = tx.query('insert into user(name) values (?)').bind('BT').exec() tx.commit() result = ret.rows_affected // Example output: 1 echo(result) ``` ## Notes - begin will exclusively occupy a connection, do not hold it for a long time until the transaction ends. - Commit, rollback, or close must be explicitly called after the transaction is completed. - begin, intra-transaction SQL and commit/rollback/close are all subject to `BT_MYSQL_QUERY_TIMEOUT_MS`. - When the transaction slow call exceeds `BT_MYSQL_SLOW_MS`, only the time consumption, method name and binding number are recorded, but the binding value is not recorded.