Officially out now: The TypeDB 3.0 Roadmap

Open a transaction

This guide covers transaction control and best practices with Studio, Console, or one of the drivers. Studio provides automated transaction control, so you don’t need to open transactions manually.

Understanding transactions

All queries to a TypeDB database are performed through transactions. TypeDB transactions provide full ACID guarantees up to snapshot isolation. There are two types of transactions: read and write. Read transactions are read-only, faster, and support inference. To open a transaction, you need an open session.

Open a new transaction

Before you begin, make sure to open a transaction using instructions from the Open a session page. To open a transaction, you need to select a transaction type and use an open session. Optionally, you can set transaction options.

  • Studio

  • Console

Studio controls transactions automatically when you run a query.

Use the Transaction type switch at the top toolbar to select the type of transaction to use to send a query. For the selector to be active, you need to connect Studio to TypeDB server and select a database from a drop-down list in the top toolbar.

Use the transaction keyword with a database name, session type, and transaction type, for example:

Open a transaction
transaction sample_db schema write

Where sample_db is the name of the database.

TypeDB drivers provide the most flexible approach to managing transactions via native API by manually opening them:

  • Rust

  • Python

  • Node.js

  • Java

  • C#

  • C++

  • C

Open a transaction
let tx = session.transaction(TransactionType::Write)?;
Open a transaction
tx = session.transaction(TransactionType.WRITE)
Open a transaction
let tx = await session.transaction(TransactionType.WRITE);
Open a transaction
TypeDBTransaction tx = session.transaction(TypeDBTransaction.Type.WRITE);
Open a transaction
TypeDBOptions options = new();
ITypeDBTransaction tx = session.Transaction(TransactionType.Read, options);
Open a transaction
TypeDB::Options options;
TypeDB::Transaction tx = session.transaction(TypeDB::TransactionType::WRITE, options);
Open a transaction
Options* opts = options_new();
tx = transaction_new(session, Read, opts);

An open transaction can be closed, committed, or even rolled back.

Close a transaction

Closing transaction doesn’t save any changes made within the transaction. To persist changes, you need to commit the transaction.

To close a transaction, follow the steps below:

  • Studio

  • Console

In the top toolbar click the Close Transaction button with the red cross icon: studio fail.

The button is active only when a transaction is open.

Use the following command in Console’s transaction input:

Close a transaction
close

To programmatically close a transaction, use TypeDB drivers:

  • Rust

  • Python

  • Node.js

  • Java

  • C#

  • C++

  • C

A transaction is automatically closed when the transaction’s variable is dropped from memory.

To explicitly close a transaction, use the force_close method:

Close a transaction
tx.force_close();

Use a context manager to close a transaction when it’s longer needed, or use the close method to explicitly close a transaction:

Close a transaction
tx.close()

Use the close method to explicitly close a transaction:

Close a transaction
await tx.close()

Use the Try with resources statement to close a transaction when it’s longer needed, or use the close method to explicitly close a transaction:

Close a transaction
tx.close();

A transaction is automatically closed when the transaction’s variable is dropped from memory.

To explicitly close a transaction, use the close method:

Close a transaction
tx.Close();

A transaction is automatically closed when the transaction’s variable is dropped from memory.

To explicitly close a transaction, use the close method:

Close a transaction
tx.close();

A transaction is automatically closed when the transaction’s variable is dropped from memory.

To explicitly close a transaction, use the close function:

Close a transaction
transaction_close(tx);

Commit a transaction

Committing a transaction validates and applies changes to the database. A successful commit persists all changes made within a transaction and then closes the transaction. A commit can fail if an inconsistency arises, like a concurrent modifications by another successful commit. A failed commit closes the transaction.

To commit a transaction, follow the steps below:

  • Studio

  • Console

In the top toolbar click the Commit Transaction button with a green check mark: studio check.

The button is active only when a transaction is open.

Use the following command in Console’s transaction input:

Commit a transaction
commit

To programmatically commit a transaction, use TypeDB drivers:

  • Rust

  • Python

  • Node.js

  • Java

  • C#

  • C++

  • C

To commit a transaction, use the commit method:

Commit a transaction
let _ = tx.commit();

To commit a transaction, use the commit method:

Commit a transaction
tx.commit()

To commit a transaction, use the commit method:

Commit a transaction
tx.commit()

To commit a transaction, use the commit method:

Commit a transaction
tx.commit();

To commit a transaction, use the commit method:

Commit a transaction
tx.commit();

To commit a transaction, use the commit method:

Commit a transaction
tx.commit();

To commit a transaction, use the commit function:

Commit a transaction
void_promise_resolve(transaction_commit(tx));

Transaction management

Avoid long-running transactions, which can result in conflicts and resource contention. A good principle to follow is for logically coherent queries to be grouped into a transaction.

TypeDB transactions use snapshot isolation and optimistic concurrency control to support concurrent, lock-free read/write transactions. For more information, see the ACID guarantees page.

Transaction timeout

TypeDB transactions have a duration limit. By default, it’s 5 minutes. The default value can be changed with Client options.

Query types and sessions/transactions

All types of sessions and transactions allow for Get and Fetch queries. Other query types are bound to write transactions and a proper session type. See the table below:

Table 1. Session and transaction types
Transaction types Possible query types
for a schema session
Possible query types
for a data session

write transaction

Define
Undefine
Fetch
Get

Insert
Delete
Update
Fetch
Get

read transaction

Fetch
Get

Fetch
Get

Learn more

See how to define a schema for your database.

See how to write data into TypeDB database.