Code Examples

This section guides the developer of Omid-based transactional applications over the different steps required to execute multi-row transactions on HBase and the different APIs involved in the process.

Transactional Contexts and Operations with Omid

Applications requiring transactions will use the interfaces provided by the Omid Transactional Client. These interfaces are provided by the TransactionManager and TTable classes described below.

When starting a transaction, the Omid Transactional Client requests a start timestamp from the TSO. This timestamp marks the beginning of a transactional context, that is, a transaction. From that point on, the client application can perform transactional read and write operations on the data source:

  • Read Ops. The client first checks if the cell has a Shadow Cell (SC). If the cell has a SC and the commit timestamp is lower than the start timestamp of the reading transaction, then the client can “see” the cell, that is, it’s in its snapshot. If there is no SC, the Commit Table (CT) is consulted to find the commit timestamp of the cell. If the commit timestamp does not exist in the CT, then the cell is assumed to below to an aborted transaction.
  • Write Ops. The client directly writes optimistically in the data source any data cells it wishes adding the start timestamp as the version of each data cell. The metadata of each cell written is added also to the so-called write-set of the transaction.

To commit a transaction, the client sends the write-set of the transaction to the TSO, which will check for conflicts in the transaction write-set with other transactions executing concurrently:

  • Conflicts. The transaction is aborted and the roll-back outcome is returned to the client.
  • No conflicts. The TSO assigns a commit timestamp to the transaction, writes the mapping start timestamp -> commit timestamp to the CT and returns commit as an outcome for the transaction to the client.

On receiving the outcome for the transaction, the client:

  • For roll-backs, it clears the written cells.
  • For commits, it updates the shadow cells for the cells in the write-set and clears the entry for that transaction in the CT.

The following sections show step-by-step the process for creating transactions in client applications with the Omid APIs.

Obtaining the Transaction Manager

In order to use transactions, a client application needs to create an instance of the TransactionManager interface with is part of the Transactional Client described in the architecture. The current Omid version offers an implementation of a transaction manager for HBase. Please, make sure core-site.xml and hbase-site.xml HBase configuration files are present in the CLASSPATH of your client application.

To create a transaction manager just add the following code to your application:

    TransactionManager tm = HBaseTransactionManager.newInstance();

If nothing is specified, the HBase transaction manager instance is created with default configuration settings loaded from the default-hbase-omid-client-config.yml file included in the HBase Omid client jar. To change the client default settings, there are two possibilities:

1) Put the specific configuration settings in a file named hbase-omid-client-config.yml and include it in the CLASSPATH. The file has the following format:

# HBase related

# TSO/ZK connection
omidClientConfiguration: !!
    connectionType: !!$ConnType ZK
    connectionString: "my_zk_cluster_conn_string"

# Instrumentation
metrics: !! [ ]

2) Create an instance of the HBaseOmidClientConfiguration class in the application code and pass it in the creation of the transaction manager instance:

    HBaseOmidClientConfiguration omidClientConfiguration = new HBaseOmidClientConfiguration();
    TransactionManager tm = HBaseTransactionManager.newInstance(omidClientConfiguration);

Please, refer to the ConfigurationExample in the source code to experiment with the configuration options.

Creating Transactions

Once the TransactionManager is created, client applications can use its interface to demarcate transactional boundaries.

In order to create a transaction the TransactionManager.begin() method is used:

    Transaction tx = tm.begin();

The transaction manager will return an instance of the Transaction interface representing the recently created transaction. This instance is necessary to instruct the operations on the data source, in which transactional context they should operate (See next section).

Executing Transactional Operations

In order to perform transaction operations on data, the client application requires to use a wrapper on the HBase’s HTableInterface abstraction. The wrapper is called TTable and is also part of what is described as Transactional Client in the Omid architecture (See section About Omid). TTable basically offers the same interface as HTableInterface enhanced with a parameter representing the transactional context. As was previously described, a Transaction instance is obtained on return of TransactionManager.begin() method calls.

    TTable txTable = new TTable(conf, "EXAMPLE_TABLE"); 

Once the access point to the data has been created, applications can use it to trigger transactional operations:

    private final byte[] family = Bytes.toBytes("EXAMPLE_CF");    
    private final byte[] qualifier = Bytes.toBytes("foo");
    // Retrieve transactionally a specific cell
    Get get = new Get(Bytes.toBytes("EXAMPLE_ROW");
    get.add(family, qualifier);
    Result txGetResult = txTable.get(tx, get);

    // Add a new cell value inside a transactional context
    Put updatedRow = new Put(Bytes.toBytes("EXAMPLE_ROW");
    updatedRow.add(family, qualifier, Bytes.toBytes("Another_value"));
    txTable.put(tx, updatedRow);

Committing and Aborting Transactions

Once the client application has finished reading/writting from/into the datasource, it must decide whether to make the changes visible or to discard them. In order to do this, it must instruct the Omid TransactionManager either to commit() or to rollback() the transactional context where the changes were produced. In case of commit, the TSO server will be notified to perform the SI validation phase. If the validation succeeds the changes will be visible to new transactions started from that point on. Otherwise, it will roll back the changes.

In order to commit the data, the client application should do something like this:

    try {
    } catch (RollbackException e) {

        // Here the transaction was rolled-back when
        // trying to commit due to conflicts with other
        // some other concurrent transaction
        // The client application should do whatever is
        // required according to its business logic

A transaction can also be specifically aborted, for example if something goes wrong when executing the business logic:

    try {
        if( ! some_business_logic_condition ) {
            throw AnyApplicationException("Changes aborted due to...");
    } catch (RollbackException e) {

Complete Example

The following example summarizes the steps described above.

import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.omid.transaction.HBaseTransactionManager;
import org.apache.omid.transaction.RollbackException;
import org.apache.omid.transaction.TTable;
import org.apache.omid.transaction.Transaction;
import org.apache.omid.transaction.TransactionManager;

public class Example {

    public static final byte[] exampleRow = Bytes.toBytes("EXAMPLE_ROW");
    public static final byte[] family = Bytes.toBytes("EXAMPLE_CF");
    public static final byte[] qualifier = Bytes.toBytes("foo");
    public static final byte[] dataValueToAvoid = Bytes.toBytes("valToAvoid");
    public static final byte[] dataValue = Bytes.toBytes("val");

    public static void main(String[] args) throws Exception {

        try (TransactionManager tm = HBaseTransactionManager.newInstance();
             Connection conn = ConnectionFactory.createConnection();
             TTable txTable = new TTable(conn, "EXAMPLE_TABLE")) {

            Transaction tx = tm.begin();
            System.out.println("Transaction started");

            // Retrieve data transactionally
            Get get = new Get(exampleRow);
            get.addColumn(family, qualifier);
            Result txGetResult = txTable.get(tx, get);
            byte[] retrievedValue = txGetResult.getValue(family, qualifier);

            // Add a condition in the application logic to show explicit transaction
            // aborts just for illustrative purposes
            if (Bytes.equals(retrievedValue, dataValueToAvoid)) {
                throw new RuntimeException("Illegal value found in database!");

            // Otherwise, add a value in other column and try to commit the transaction
            try {
                Put putOnRow = new Put(exampleRow);
                putOnRow.addColumn(family, qualifier, dataValue);
                txTable.put(tx, putOnRow);
                System.out.println("Transaction committed. New value written to example row");
            } catch(RollbackException e) {
                System.out.println("Transaction aborted due to conflicts. Changes to row aborted");




Additional Examples

The examples module contains complete examples showing the Omid functionality that can be executed in your Omid+HBase environment. Just clone the Omid project, go to the examples module and execute mvn clean package to create a tar.gz file that includes all the examples. In order to execute each example, just execute the script and follow the instructions.