Lesson 6.3: Transactions

Opening transactions

With a connection established to the server via a driver, open transactions directly against a database. There are three transaction kinds: READ, WRITE, and SCHEMA.

from typedb.driver import TypeDB, TransactionType, Credentials, DriverOptions

DB = "bookstore"
address = "localhost:1729"
credentials = Credentials("admin", "password")
options = DriverOptions(is_tls_enabled=True, tls_root_ca_path=None)

with TypeDB.driver(address, credentials, options) as driver:
    # Read transaction
    with driver.transaction(DB, TransactionType.READ) as tx:
        pass

    # Write transaction
    with driver.transaction(DB, TransactionType.WRITE) as tx:
        # mutations...
        tx.commit()

    # Schema transaction
    with driver.transaction(DB, TransactionType.SCHEMA) as tx:
        # define/undefine/redefine...
        tx.commit()

The transaction takes a database name and a transaction type. Use a context manager to ensure closure. Write and schema transactions must be committed to persist changes.

Behavior and lifecycle

  • Read transactions: read-only; lazily stream results.

  • Write transactions: buffer mutations; commit persists or rollback discards.

  • Schema transactions: exclusive with other write/schema transactions for a database.

Transactions are short-lived atomic units of work and provide ACID guarantees up to snapshot isolation. See Transactions for details.

To get a deeper understanding of using TypeDB transactions - including concurrency, configuration, and best practices - see the driver-side Transaction Concepts page.