djondb Guide

Transactions

To use transactions you will need the version 0.3 or above

As any other transactional database djondb allows you to begin, commit or rollback transactions.
The operations that generates changes in the database will be isolated from other operations and they will applied to the main files when the user issued a commit operation.

The transaction model used by djondb is known as “optimistic” transaction, djondb will not lock records. Instead you should ensure, in your application code level that two different users will not modify the same record at the same time.

Doing a transaction using djon-shell

The commands to interact with transactions are: beginTransaction, commitTransaction and rollbackTransaction, here’s a sample using the shell

user@ubuntu> djon-shell
djondb shell version 0.30.0
Welcome to djondb shell.
Use help(); to get the commands available. 
(hint: The first command should be "connect" to start playing with a server)

> connect('localhost');
> beginTransaction();
Transaction started
> insert('test', 'txns', { name: "John" });

> var c = find('test', 'txns');

> c.next(); c.current();
{"_id":"95289b90-4cdf-4f9e-ab09-472edd254eca","_revision":"e69f3cf8-0e38-478f-9d51-1c4e7db6854d","_status":1,"name":"John"}
> commitTransaction();
Transaction committed
> var c = find('test', 'txns');
> c.next(); c.current();
{"_id":"95289b90-4cdf-4f9e-ab09-472edd254eca","_revision":"e69f3cf8-0e38-478f-9d51-1c4e7db6854d","_status":1,"name":"John"}
> 

the first step is to start the transaction using the command “beginTransaction”, then we inserted a document and issued the commitTransaction command to close the transaction.

If you open a new djon-shell session this one will not be able to see the changes made in between begin and commit operations, the modifications will be visible only after the commit command is executed.

Note that you can do several operations between begin and commit operations, these operations include inserts, updates and removes, and they could be done over multiple namespaces.

Now it’s time to use the rollback operation, which will drop all the operations and it will leave the database in the same state as it was before the beginTransaction was executed.

user@ubuntu> djon-shell --shell helper.js
djondb shell version 0.30.0
Welcome to djondb shell.
Use help(); to get the commands available. 
(hint: The first command should be "connect" to start playing with a server)

> beginTransaction();
Transaction started
> insert('test', 'txns', { name: "John2" });

> printall(find('test', 'txns'));
[
    {
        "_id": "95289b90-4cdf-4f9e-ab09-472edd254eca",
        "_revision": "e69f3cf8-0e38-478f-9d51-1c4e7db6854d",
        "_status": 1,
        "name": "John"
    },
    {
        "_id": "8a1747fc-5952-4f88-826f-a11904a8e4a6",
        "_revision": "f0769fd6-17f0-4297-8021-383290cd218a",
        "_status": 1,
        "name": "John2"
    }
]
> rollbackTransaction();
Transaction rollbacked
> printall(find('test', 'txns'));
[
    {
        "_id": "95289b90-4cdf-4f9e-ab09-472edd254eca",
        "_revision": "e69f3cf8-0e38-478f-9d51-1c4e7db6854d",
        "_status": 1,
        "name": "John"
    }
]
> 

Note: in this example we are using “printall” which is not a native command, but can be defined in a script (helper.js) to be loaded when you open the djon-shell

In this example we inserted a document, then executed the rollback and the new document will be dropped.

Suggest Edit