Search code examples
javahbase

Can we reuse the hbase Java client Connection between multi-threads?


In our environment we use the multi-threads call the hbase by hbase Java Client, and in each thread we call the Connection.close() when we finished the operation, however we found Connection.close() spent around 10 milliseconds, so may I know if it is possible reuse the connection between threads without closing the connection?


Solution

  • From API documentation of HBase 1.2, org.apache.hadoop.hbase.client.Connection : https://hbase.apache.org/1.2/apidocs/org/apache/hadoop/hbase/client/Connection.html

    Connection creation is a heavy-weight operation. Connection implementations are thread-safe, so that the client can create a connection once, and share it with different threads. Table and Admin instances, on the other hand, are light-weight and are not thread-safe. Typically, a single connection per client application is instantiated and every thread will obtain its own Table instance. Caching or pooling of Table and Admin is not recommended.

    This class replaces HConnection, which is now deprecated.

    So, yes, creating HBase connection is slow. But the obtained Connection is Thread Safe and you should only have one HBase connection in your application (that you should only close at the end / shutdown of your application).

    However, pay attention to close the objects you get from Connection : Table, Admin, ResultScanner, ... They do open resources that must be close when you have finished to handle them.