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:
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
let tx = session.transaction(TransactionType::Write)?;
tx = session.transaction(TransactionType.WRITE)
let tx = await session.transaction(TransactionType.WRITE);
TypeDBTransaction tx = session.transaction(TypeDBTransaction.Type.WRITE);
TypeDBOptions options = new();
ITypeDBTransaction tx = session.Transaction(TransactionType.Read, options);
TypeDB::Options options;
TypeDB::Transaction tx = session.transaction(TypeDB::TransactionType::WRITE, options);
Options* opts = options_new();
tx = transaction_new(session, Read, opts);
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: .
The button is active only when a transaction is open.
Use the following command in Console’s transaction input:
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:
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:
tx.close()
Use the close method to explicitly 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:
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:
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:
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:
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: .
The button is active only when a transaction is open.
Use the following command in Console’s transaction input:
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:
let _ = tx.commit();
To commit a transaction, use the commit method:
tx.commit()
To commit a transaction, use the commit method:
tx.commit()
To commit a transaction, use the commit method:
tx.commit();
To commit a transaction, use the commit method:
tx.commit();
To commit a transaction, use the commit method:
tx.commit();
To commit a transaction, use the commit function:
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.
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:
Transaction types | Possible query types for a schema session |
Possible query types for a data session |
---|---|---|
|
||
|