Transactions
Transactionality
TypeDB transactions come in 3 flavors: read, write, and schema transactions.
All transactions operate over snapshots of the database, providing a consistent view regardless of concurrent transactions' operations.
TypeDB transactions provide ACID guarantees up to snapshot isolation.
Read transactions
Read transactions can execute only read queries, and will error when provided data update queries or schema mutation queries.
They are the most lightweight and should be used whenever possible. Any number of read transactions can be opened concurrently, and they will never fail to open even when write or schema transactions are open.
Write transactions
Write transactions can execute read queries and data update queries, and will error when provided schema mutation queries.
Write transactions can be opened concurrently to other write transactions, and read transactions. They may fail on commit, as required to enforce data consistency and isolation, such as when two write transactions concurrently update same piece of data and commit.
We open transaction 1, modifies the attribute value of an entity, and then commits the transaction.
We open transaction 2 while transaction 1 is still running (i.e. hasn’t been committed yet). They modify the same attribute value and commit their changes after transaction 1 has been commit. In this case, the commit of transaction 2 will fail.
Schema transactions
Schema transactions can execute any type of query: read, data update, and schema mutation queries.
Schema transactions are exclusive against other schema transactions, and write transactions. Existing open write transactions will block schema transactions from opening. Similarly, a schema transaction will block write transactions from opening. This prevents the data from mutating while the schema is being updated.
Schema transactions can mutate both data and schema at the same time, allowing atomic migrations from one database state to another.
Executing queries in transaction
All transactions types have an identical API to submit queries.
-
Console
-
Python
-
Rust
-
Java
-
Studio
Open the transaction with
transaction my-db <TYPE>
where <TYPE>
can be one of schema
, write
, or read
.
Then run your queries in the newly opened prompt, using enters for newlines and double enters for query submissions. Use
commit
if you want to commit changes.
Open a transaction and send a query with
with driver.transaction(<DATABASE NAME>, TransactionType.<TYPE>) as transaction:
answer = transaction.query(<QUERY>).resolve()
where:
-
<QUERY>
and<DATABASE NAME>
arestr
s. -
<TYPE>
can be one ofSCHEMA
,WRITE
, orREAD
.
Use
transaction.commit()
if you want to commit changes.
Open a transaction and send a query with
let transaction = driver.transaction(<DATABASE NAME>, TransactionType::<TYPE>).await.unwrap();
{
let answer = transaction.query(<QUERY>).await.unwrap();
}
where:
-
<QUERY>
and<DATABASE NAME>
are&str
s. -
<TYPE>
can be one ofSchema
,Write
, orRead
.
Use
transaction.commit().await.unwrap();
if you want to commit changes.
Open a transaction and send a query with
try (TypeDBTransaction transaction = session.transaction(<DATABASE NAME>, Transaction.Type.<TYPE>)) {
transaction.query(<QUERY>).resolve();
}
where:
-
<QUERY>
and<DATABASE NAME>
areString
s. -
<TYPE>
can be one ofSCHEMA
,WRITE
, orREAD
.
Use
transaction.commit();
if you want to commit changes.
Select your database using the database selector (), then select your transaction type in the top menu.
Open your query file. Then click the run button to run your query. Finally, use the commit button () to persist any changes made.