xid()

Overview

Returns a transaction ID object suitable for passing to the two-phase-commit functions, starting with tpc_begin().

See also: MariaDB Connector/Python 1.1 and in 1.0

DETAILS

Returns a transaction ID object (xid) suitable for passing to the two-phase-commit (TPC) methods of this connection. The three required arguments are:

format_id

A shared id between the xid calls of your app.

global_transaction_id

Choose a shared string to identify this specific transaction.

branch_qualifier

Choose a per-connection string that is unique among all the transaction members.

The two-phase-commit support allows you to coordinate multiple DB connections so that the multi-connection transactions are only made final if all the commits can finish successfully. Each connection needs an xid created by this method. You then use tpc_begin(), tpc_prepare(), and tpc_commit (or tpc_rollback) in concert.

EXAMPLES

import mariadb

conn1 = mariadb.connect(
    host = 'host1',
    user = 'user1',
    password = 'secret1',
    database = 'db1',
    )

conn2 = mariadb.connect(
    host = 'host2',
    user = 'user2',
    password = 'secret2',
    database = 'db2',
    )

cursor1 = conn1.cursor()
cursor2 = conn2.cursor()

xid1 = conn1.xid(999, 'xyz trans', 'conn1')
xid2 = conn2.xid(999, 'xyz trans', 'conn2')

conn1.tpc_begin(xid1)
conn2.tpc_begin(xid2)

# ... perform the updates for your interrelated change ...

try:
    # These prepare calls validate that all the commits can be made.
    conn1.tpc_prepare(xid1)
    conn2.tpc_prepare(xid2)
except mariadb.DatabaseError:
    conn1.tpc_rollback(xid1)
    conn2.tpc_rollback(xid2)
else:
    conn1.tpc_commit(xid1)
    conn2.tpc_commit(xid2)

CHANGE HISTORY

Release Series

History

1.1

  • Present starting in MariaDB Connector/Python 1.1.0rc1.

1.0

  • Present starting in MariaDB Connector/Python 1.0.0.