A transaction comprises a unit of work performed within a database management system (or similar system) against a database, and treated in a coherent and reliable way independent of other transactions. Transactions in a database environment have two main purposes:
A database transaction, by definition, must be atomic, consistent, isolated and durable. Database practitioners often refer to these properties of database transactions using the acronym ACID. --- Wikipedia
OrientDB is an ACID compliant DBMS.
"Atomicity requires that each transaction is 'all or nothing': if one part of the transaction fails, the entire transaction fails, and the database state is left unchanged. An atomic system must guarantee atomicity in each and every situation, including power failures, errors, and crashes. To the outside world, a committed transaction appears (by its effects on the database) to be indivisible ("atomic"), and an aborted transaction does not happen." - WikiPedia
"The consistency property ensures that any transaction will bring the database from one valid state to another. Any data written to the database must be valid according to all defined rules, including but not limited to constraints, cascades, triggers, and any combination thereof. This does not guarantee correctness of the transaction in all ways the application programmer might have wanted (that is the responsibility of application-level code) but merely that any programming errors do not violate any defined rules." - WikiPedia
OrientDB uses the MVCC to assure consistency. The difference between the management of MVCC on transactional and not-transactional cases is that with transactional, the exception rollbacks the entire transaction before to be caught by the application.
Look at this example:
Sequence | Client/Thread 1 | Client/Thread 2 | Version of record X |
---|---|---|---|
1 | Begin of Transaction | ||
2 | read(x) | 10 | |
3 | Begin of Transaction | ||
4 | read(x) | 10 | |
5 | write(x) | 10 | |
6 | commit | 10 -> 11 | |
7 | write(x) | 10 | |
8 | commit | 10 -> 11 = Error, in database x already is at 11 |
"The isolation property ensures that the concurrent execution of transactions results in a system state that would be obtained if transactions were executed serially, i.e. one after the other. Providing isolation is the main goal of concurrency control. Depending on concurrency control method, the effects of an incomplete transaction might not even be visible to another transaction." - WikiPedia
OrientDB has different level of isolation based on the configuration. By using "plocal" (or the old "local") and "memory" access everything has the highest level of isolation: SERIALIZABLE. By using the remote protocol it's READ COMMITTED.
Look at this examples:
Sequence | Client/Thread 1 | Client/Thread 2 |
---|---|---|
1 | Begin of Transaction | |
2 | read(x) | |
3 | Begin of Transaction | |
4 | read(x) | |
5 | write(x) | |
6 | commit | |
7 | read(x) | |
8 | commit |
At operation 7 the client 1 continues to read the same version of x read in operation 2.
Sequence | Client/Thread 1 | Client/Thread 2 |
---|---|---|
1 | Begin of Transaction | |
2 | read(x) | |
3 | Begin of Transaction | |
4 | read(y) | |
5 | write(y) | |
6 | commit | |
7 | read(y) | |
8 | commit |
At operation 7 the client 1 reads the version of y which was written at operation 6 by client 2. This is because it never reads y before.
Transactions are client-side only until the commit. This means that if you're using the "remote" protocol the server can't see local changes
In this scenario you can have different isolation levels with commands.
"Durability means that once a transaction has been committed, it will remain so, even in the event of power loss, crashes, or errors. In a relational database, for instance, once a group of SQL statements execute, the results need to be stored permanently (even if the database crashes immediately thereafter). To defend against power loss, transactions (or their effects) must be recorded in a non-volatile memory." - WikiPedia
An OrientDB instance can fail for several reasons:
You can use the OrientDB engine directly in the same process of your application. This gives superior performance due to the lack of inter-process communication. In this case, should your application crash (for any reason), the OrientDB Engine also crashes.
If you're using an OrientDB Server connected remotely, if your application crashes the engine continue to work, but any pending transaction owned by the client will be rolled back.
At start-up the OrientDB Engine checks to if it is restarting from a crash. In this case, the auto-recovery phase starts which rolls back all pending transactions.
OrientDB has different levels of durability based on storage type, configuration and settings.
PLocal (Paginated-Local) is durable by definition because use a Write Ahead Log (WAL) to recover operations in case of failure. Any change is appended into the WAL before to be applied to the underlying clusters.
By default Local uses the Operating System's Memory Mapping capabilities to speed up access to the underlying file system.
If you have a reliable IO system, such as a RAID hardware device, this is not a problem. However, if you run OrientDB on top of non-reliable hardware (no RAID controller) or you want to reduce database integrity problems on improvise crash of the Server machine, then enable a sync for each transaction log operation.
Via JVM configuration:
java ... -Dtx.log.synch=true ...
or via API:
OGlobalConfiguration.TX_LOG_SYNCH.setValue(true);
You can also execute a synch against every single database file once transaction commit is completed.
Via JVM configuration:
java ... -Dtx.commit.synch=true ...
or via API:
OGlobalConfiguration.TX_COMMIT_SYNCH.setValue(true);
Default mode. Each operation is executed instantly.
Calls to begin()
, commit()
and rollback()
have no effect.
This mode uses the well known Multi Version Control System (MVCC) by allowing multiple reads and writes on the same records. The integrity check is made on commit. If the record has been saved by another transaction in the interim, then an OConcurrentModificationException will be thrown. The application can choose either to repeat the transaction or abort it.
With Graph API transaction begins automatically, with Document API is explicit by using the begin() method. Example with Document API:
db.open("remote:localhost:7777/petshop");
try{
db.begin(TXTYPE.OPTIMISTIC);
...
// WRITE HERE YOUR TRANSACTION LOGIC
...
db.commit();
}catch( Exception e ){
db.rollback();
} finally{
db.close();
}
In Optimistic transaction new records take temporary RecordIDs to avoid to ask to the server a new RecordID every time. Temporary RecordIDs have Cluster Id -1 and Cluster Position < -1. When a new transaction begun the counter is reset to -1:-2. So if you create 3 new records you'll have:
At commit time, these temporary records RecordIDs will be converted in the final ones.
This mode is not supported yet by the engine, but you can expressly lock records during transaction. All the acquired locks are kept in the transaction until the closing, namely commit() or rollback().
Look also at SQL batch.
OrientDB uses temporary RecordIDs with transaction as scope that will be transformed to finals once the transactions is successfully committed to the database. This avoid to ask for a free slot every time a client creates a record.
In some situations transactions can improve performance, typically in the client/server scenario. If you use an Optimistic Transaction, the OrientDB engine optimizes the network transfer between the client and server, saving both CPU and bandwidth.
For further information look at Transaction tuning to know more.
Transactions can be committed across a distributed architecture.