# Transaction Control

> ## Transaction Types

> All database access occurs within transactions which are defined by the enumerated values described in the following sections.

> The following eXtremeDB Transaction Types are defined by enum `MCO_TRANS_TYPE` in `mco.h`:

>         typedef enum MCO_TRANS_TYPE_E_
>         {
>             MCO_READ_ONLY = 0, MCO_UPDATE = 1, MCO_READ_WRITE = 2, MCO_EXCLUSIVE = 3

>         } MCO_TRANS_TYPE;

> MCO_READ_ONLY | Transaction for `READ_ONLY` access to data.
> MCO_UPDATE | Transaction for "read with intent to update" instead of `READ_ONLY`. If the intent is to find a database object for modification (Update of Delete) the `MCO_UPDATE` transaction type should be specified. To update a database object a new `READ_WRITE` transaction must be started, or the current transaction must be upgraded by calling function `mco_trans_upgrade()`.
> MCO_READ_WRITE | Transaction for `READ_WRITE` access to data.
> MCO_EXCLUSIVE | (AKA "Global Lock") No other transaction can run in parallel even if the Transaction Manager would normally allow it: `MVCC` will not allow `READ_ONLY` or `READ_WRITE` transactions while `EXCLUSIVE` transaction is working, ``MURSIW`` will postpone execution on other `READ_ONLY` transactions while `EXCLUSIVE` transaction is working (`EXCLUSIVE` Transaction Manager ignores any type of transaction because it runs just one transaction at a time regardless of type)

> ## Transaction Priorities

> Transaction scheduling is partially determined by the transaction priority. Enum `MCO_TRANS_PRIORITY` is defined in `mco.h` as follows:

>         typedef enum MCO_TRANS_PRIORITY_E_
>         {
>             MCO_TRANS_IDLE       = -2,   /* Least important transactions */
>             MCO_TRANS_BACKGROUND = -1,
>             MCO_TRANS_FOREGROUND = 0,    /* Normal priority transactions */
>             MCO_TRANS_HIGH       = 1,
>             MCO_TRANS_ISR        = 2     /* Very Important Transactions */

>         } MCO_TRANS_PRIORITY;

> Transaction priority can be set to one of the following values:

> MCO_TRANS_IDLE | Least important transactions
> MCO_TRANS_BACKGROUND | Less important then foreground but more important then idle
> MCO_TRANS_FOREGROUND | Normal priority transactions
> MCO_TRANS_HIGH | More important then foreground but less important then ISR
> MCO_TRANS_ISR | Very Important Transactions

> ## Transaction Scheduling Policy

> The transaction scheduling policy for transactions with the same priority can be set to one of the following values as defined by enum `MCO_TRANS_SCHED_POLICY` in `mco.h`:

>         typedef enum MCO_TRANS_SCHED_POLICY_E_
>         {
>             MCO_SCHED_FIFO = 0,                    /* First In First Out */
>             MCO_SCHED_READER_FAVOR = 1,            /* Run read-only transactions first */
>             MCO_SCHED_WRITER_FAVOR = 2             /* Run read-write transactions first */

>         } MCO_TRANS_SCHED_POLICY;

> MCO_SCHED_FIFO | First In First Out
> MCO_SCHED_READER_FAVOR | Run read-only transactions first
> MCO_SCHED_WRITER_FAVOR | Run read-write transactions first

> ## Transaction Object States

> Within a transaction, objects can have one of the following states as defined by enum `MCO_TRANS_OBJ_STATE` in `mco.h`:

>         typedef enum MCO_TRANS_OBJ_STATE_
>         {
>             MCO_TRANS_OBJ_CREATED     = 0x01,
>             MCO_TRANS_OBJ_DELETED     = 0x02,
>             MCO_TRANS_OBJ_ALL_DELETED = 0x04

>         } MCO_TRANS_OBJ_STATE;

> MCO_TRANS_OBJ_CREATED | This object is being created.
> MCO_TRANS_OBJ_DELETED | This object is being deleted.
> MCO_TRANS_OBJ_ALL_DELETED | All objects of this class are being deleted.

> ## Transaction Isolation Levels

> The transaction isolation level can be or one of the following types as defined by enum `MCO_TRANS_ISOLATION_LEVEL` in `mco.h`::

>         typedef enum MCO_TRANS_ISOLATION_LEVEL_E_
>         {
>             MCO_DEFAULT_ISOLATION_LEVEL = 0x0, /* MCO_REPEATABLE_READ for MVCC,  MCO_SERIALIZABLE for GlobalLock / MURSIW */
>             MCO_READ_COMMITTED          = 0x1, /* Guarantees that any data read was committed at the moment is read.
>                                 It restricts the reader from seeing any uncommitted data. */
>             MCO_REPEATABLE_READ         = 0x2, /* In addition to MCO_READ_COMMITTED guarantees that any data read cannot change,
>                                 if the transaction reads the same data again,
>                                 it will find the previously read data in place, unchanged,
>                                 and available to read. */
>             MCO_SERIALIZABLE            = 0x4  /* Emulates serial transaction execution */

>         } MCO_TRANS_ISOLATION_LEVEL;

> MCO_DEFAULT_ISOLATION_LEVEL | `MCO_REPEATABLE_READ` for MVCC, `MCO_SERIALIZABLE` for GlobalLock / MURSIW
> MCO_READ_COMMITTED | Guarantees that any data read was committed at the moment was read. It restricts the reader from seeing any uncommitted data
> MCO_REPEATABLE_READ | In addition to `MCO_READ_COMMITTED` guarantees that any data read cannot change; if the transaction reads the same data again, it will find the previously read data in place, unchanged, and available to read
> MCO_SERIALIZABLE | Emulates serial transaction execution

> ## Transaction Commit Policy

> The transaction commit policy can be one of the following:

>         typedef enum MCO_COMMIT_POLICY_E
>         {
>             MCO_COMMIT_SYNC_FLUSH, /* Flush to disk on each commit (default)  */
>             MCO_COMMIT_BUFFERED,   /* Runtime buffered transactions */
>             MCO_COMMIT_DELAYED,    /* Runtime buffered transactions, with commit criteria */
>             MCO_COMMIT_NO_SYNC     /* Changes are not synchronously written to the disk */

>         } MCO_COMMIT_POLICY;

> MCO_COMMIT_SYNC_FLUSH | Flush to disk on each commit (default)
> MCO_COMMIT_BUFFERED | Runtime buffered transactions
> MCO_COMMIT_DELAYED | Runtime buffered transactions with commit criteria
> MCO_COMMIT_NO_SYNC | Changes are not synchronously written to the disk