2017-06-14 252 views
1

在每個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打開連接,獲取數據並最終關閉它。我聽到有人說這個連接不需要關閉。

請指教

回答

1

通常情況下,我預先創建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.