HBase的连接关闭()

问题描述:

在每个API调用,我这样做:HBase的连接关闭()

 Configuration config = HBaseConfiguration.create(); 
     Path hbase = new Path("hbase-site.xml"); 
     Path core = new Path("core-site.xml"); 
     config.addResource(hbase); 
     config.addResource(core); 
     Connection connection = ConnectionFactory.createConnection(config); 

然后当我获取当前API调用数据关闭此连接:

finally { 
      if (null != connection) { 
       try { 
        connection.close(); 
       } catch (Exception e) { 

       } 
      } 
     } 

请告诉我这个模型是正确的还是错误的。每个api请求都需要以这种方式提供服务 - 使用ConnectionFactory打开连接,获取数据并最终关闭它。我听到有人说这个连接不需要关闭。

请指教

通常情况下,我预先创建Connection对象,并将其用于多个查询。 据Hbase Reference Guide

In HBase 1.0, obtain a Connection object from ConnectionFactory and thereafter, get from it instances of Table, Admin, and RegionLocator on an as-need basis. When done, close the obtained instances. Finally, be sure to cleanup your Connection instance before exiting. Connections are heavyweight objects but thread-safe so you can create one for your application and keep the instance around. Table, Admin and RegionLocator instances are lightweight. Create as you go and then let go as soon as you are done by closing them.