make it thread safe using ThreadLocal

this is the easiest thing to do to make it thread safe while reusing
hbase connections
This commit is contained in:
Noah Levitt
2018-11-09 17:08:01 -08:00
parent 8bda0d4cf0
commit f8f80249f2
@@ -42,8 +42,8 @@ public class HBaseTable extends HBaseTableBean {
Logger.getLogger(HBaseTable.class.getName());
protected boolean create = false;
protected HConnection hconn = null;
protected HTableInterface htable;
protected ThreadLocal<HConnection> hconn = new ThreadLocal<HConnection>();
protected ThreadLocal<HTableInterface> htable = new ThreadLocal<HTableInterface>();
public boolean getCreate() {
return create;
@@ -57,24 +57,23 @@ public class HBaseTable extends HBaseTableBean {
}
protected HConnection hconnection() throws IOException {
if (hconn == null) {
hconn = HConnectionManager.createConnection(hbase.configuration());
if (hconn.get() == null) {
hconn.set(HConnectionManager.createConnection(hbase.configuration()));
}
return hconn;
return hconn.get();
}
protected HTableInterface htable() throws IOException {
if (htable == null) {
htable = hconnection().getTable(htableName);
if (htable.get() == null) {
htable.set(hconnection().getTable(htableName));
}
return htable();
return htable.get();
}
@Override
public void put(Put p) throws IOException {
HTableInterface table = hconnection().getTable(htableName);
try {
table.put(p);
htable().put(p);
} catch (IOException e) {
reset();
throw e;
@@ -83,9 +82,8 @@ public class HBaseTable extends HBaseTableBean {
@Override
public Result get(Get g) throws IOException {
HTableInterface table = hconnection().getTable(htableName);
try {
return table.get(g);
return htable().get(g);
} catch (IOException e) {
reset();
throw e;
@@ -93,9 +91,8 @@ public class HBaseTable extends HBaseTableBean {
}
public HTableDescriptor getHtableDescriptor() throws IOException {
HTableInterface table = hconnection().getTable(htableName);
try {
return table.getTableDescriptor();
return htable().getTableDescriptor();
} catch (IOException e) {
reset();
throw e;
@@ -128,23 +125,22 @@ public class HBaseTable extends HBaseTableBean {
}
protected void reset() {
logger.info("attempting to reset hbase connection state");
if (htable != null) {
if (htable.get() != null) {
try {
htable.close();
htable.get().close();
} catch (IOException e) {
logger.log(Level.WARNING, "htablename='" + htableName + "' htable.close() threw " + e, e);
}
htable = null;
htable.remove();
}
if (hconn != null) {
if (hconn.get() != null) {
try {
hconn.close();
hconn.get().close();
} catch (IOException e) {
logger.log(Level.WARNING, "hconn.close() threw " + e, e);
}
hconn = null;
hconn.remove();
}
}