Every graph operation in Titan occurs within the context of a transaction. According to the Blueprints’ specification, each thread opens its own transaction against the graph database with the first operation (i.e. retrieval or mutation) on the graph.

TitanGraph g = TitanFactory.open("/tmp/titan");
Vertex juno = g.addVertex(null); //Automatically opens a new transaction
juno.setProperty("name", "juno");
g.commit(); //Commits transaction

In this example, a local Titan graph database is opened. Adding the vertex “juno” is the first operation (in this thread) which automatically opens a new transaction. All subsequent operations occur in the context of that same transaction until the transaction is explicitly stopped or the graph database shutdown() which commits all currently running transactions.

Transactional Scope

All graph elements (vertices, edges, and types) are only available in the transactional scope in which they were originally created. After a transaction is stopped, all its associated elements become unavailable and can no longer be accessed. For example, after creating the vertex “juno” and stopping the transaction in the code snippet above, the “juno” vertex becomes unavailable and accessing it would result in an exception. The transactional scope is strictly enforced to avoid accessing “stale” elements. To illustrate this point, assume a multi-user environment for the code example above. After stopping the transaction, all (potentially) held locks on the “juno” vertex are released and another transaction running in a different thread (or on another machine) might mutate the vertex. The vertex reference “juno” would thus be stale and further access to the vertex could lead to inconsistencies.
To re-active the “juno” vertex we need to refresh it in the currently running transaction as shown in the following example:

TitanGraph g = TitanFactory.open("/tmp/titan");
Vertex juno = g.addVertex(null); //Automatically opens a new transaction
juno.setProperty("name", "juno");
g.commit(); //Commits transaction
Vertex jupiter = g.addVertex(null); //Automatically opens another transaction
jupiter.setProperty("name", "jupiter");
juno = g.getVertex(juno);
Edge married = g.addEdge(null, juno, jupiter, "married");

Transaction Failures

When committing a transaction, Titan will attempt to persist all changes to the storage backend. This might not always be successful due to IO exceptions, network errors, machine crashes or resource unavailability. Hence, transactions can fail. In fact, transactions will eventually fail in sufficiently large systems. Therefore, we highly recommend that your code expects and accommodates such failures.

try {
    if (g.getVertices("name",name).iterator().hasNext())
        throw new IllegalArgumentException("Username already taken: " + name);
    Vertex user = g.addVertex(null);
    user.setProperty("name", name);
    g.commit();
} catch (TitanException e) {
    //Recover, retry, or return error message
}

The example above demonstrates a simplified user signup implementation where name is the name of the user who wishes to register. First, it is checked whether a user with that name already exists. If not, a new user vertex is created and the name assigned. Finally, the transaction is committed.

If the transaction fails, a TitanException is thrown. There are a variety of reasons why a transaction may fail. Titan differentiates between potentially temporary and permanent failures.

Potentially temporary failures are those related to resource unavailability and IO hickups (e.g. network timeouts). Titan automatically tries to recover from temporary failures by retrying to persist the transactional state after some delay. The number of retry attempts and the retry delay can be configured through the Titan graph configuration.

Permanent failures can be caused by complete connection loss, hardware failure or lock contention. To understand the cause of lock contention, consider the signup example above and suppose a user tries to signup with username “juno”. That username may still be available at the beginning of the transaction but by the time the transaction is committed, another user might have concurrently registered with “juno” as well and that transaction holds the lock on the username therefore causing the other transaction to fail. It some cases one can recover from a lock contention failure by re-running the entire transaction.

Gotcha

Transactions are started automatically with the first operation executed against the graph. One does NOT have to start a transaction manually. The method newTransaction is used to start multi threaded transactions only.

Next Steps