使用apache的commons-pool完成了一个简单的HBASE的数据连接池,实现的功能为:
继承GenericObjectPool
public class HbasePool extends GenericObjectPool<Connection>{
private static GenericObjectPoolConfig config = new GenericObjectPoolConfig();
private static HBaseContainer hBaseContainer;
static {
config.setMaxTotal(200);
config.setLifo(false);
config.setMaxIdle(200);
config.setMaxWaitMillis(5 * 1000);
config.setMinEvictableIdleTimeMillis(30 * 1000);
config.setSoftMinEvictableIdleTimeMillis(60 * 1000);
config.setNumTestsPerEvictionRun(200);
config.setTimeBetweenEvictionRunsMillis(60 * 1000);
config.setTestOnBorrow(true);
}
public void sethBaseContainer(HBaseContainer hBaseContainer) {
this.hBaseContainer = hBaseContainer;
}
public HbasePool() {
super(new HbasePoolFactory(),config);
}
public static class HbasePoolFactory implements PooledObjectFactory<Connection>{
@Override
public PooledObject<Connection> makeObject() throws Exception {
Connection connection = ConnectionFactory.createConnection(hBaseContainer.getConfiguration());
return new DefaultPooledObject<Connection>(connection);
}
@Override
public void destroyObject(PooledObject<Connection> p) throws Exception {
p.getObject().close();
}
@Override
public boolean validateObject(PooledObject<Connection> p) {
// TODO Auto-generated method stub
return true;
}
@Override
public void activateObject(PooledObject<Connection> p) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void passivateObject(PooledObject<Connection> p)
throws Exception {
// TODO Auto-generated method stub
}
}
}