And*_*rea 5 hbase connection-pooling
HTablePool的正确使用模式是什么?我的意思是,假设我有用HTablePool实例初始化的DAO。此DAO是无状态会话Bean的成员实例,因此可以在调用之间重用。
以下各项之间的正确用法是什么?
private HTableInterface aTable;
public XYZDAO(final HTablePool pool)
{
this.aTable = pool.getTable(...);
}
public void doSomething(...)
{
aTable.get(...)
}
Run Code Online (Sandbox Code Playgroud)
或HTablePool应该像数据源一样使用,因此更合适的用法是
private HTablePool datasource;
public XYZDAO(final HTablePool pool)
{
this.datasource = pool;
}
public void doSomething(...)
{
HTableInterface aTable = datasource.getTable(...);
aTable.get(...);
aTable.close();
}
Run Code Online (Sandbox Code Playgroud)
第二种方法是最好的,因为类不是线程安全HTablePool的,Datasource所以应该像使用a 一样使用HTable。调用close方法HTableInterface将自动将表返回到池中。
请注意,在较新的HBase版本中,有HConnection替换HTablePool旧版本的接口。