diff --git a/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java b/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java index 3d1c67f97c..f44674847f 100644 --- a/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java +++ b/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java @@ -3912,13 +3912,13 @@ private void removeUnusedColumnDescriptor(MColumnDescriptor oldCD) { } boolean success = false; - QueryWrapper queryWrapper = new QueryWrapper(); + Query query = null; try { openTransaction(); LOG.debug("execute removeUnusedColumnDescriptor"); - Query query = pm.newQuery("select count(1) from " + + query = pm.newQuery("select count(1) from " + "org.apache.hadoop.hive.metastore.model.MStorageDescriptor where (this.cd == inCD)"); query.declareParameters("MColumnDescriptor inCD"); long count = ((Long)query.execute(oldCD)).longValue(); @@ -3931,7 +3931,7 @@ private void removeUnusedColumnDescriptor(MColumnDescriptor oldCD) { success = commitTransaction(); LOG.debug("successfully deleted a CD in removeUnusedColumnDescriptor"); } finally { - rollbackAndCleanup(success, queryWrapper); + rollbackAndCleanup(success, query); } } @@ -8790,14 +8790,13 @@ private MFunction getMFunction(String db, String function) { public Function getFunction(String dbName, String funcName) throws MetaException { boolean commited = false; Function func = null; + Query query = null; try { openTransaction(); func = convertToFunction(getMFunction(dbName, funcName)); commited = commitTransaction(); } finally { - if (!commited) { - rollbackTransaction(); - } + rollbackAndCleanup(commited, query); } return func; } @@ -8805,17 +8804,16 @@ public Function getFunction(String dbName, String funcName) throws MetaException @Override public List getAllFunctions() throws MetaException { boolean commited = false; + Query query = null; try { openTransaction(); - Query query = pm.newQuery(MFunction.class); + query = pm.newQuery(MFunction.class); List allFunctions = (List) query.execute(); pm.retrieveAll(allFunctions); commited = commitTransaction(); return convertToFunctions(allFunctions); } finally { - if (!commited) { - rollbackTransaction(); - } + rollbackAndCleanup(commited, query); } } @@ -8876,10 +8874,7 @@ public NotificationEventResponse getNextNotification(NotificationEventRequest rq } return result; } finally { - if (!commited) { - rollbackAndCleanup(commited, query); - return null; - } + rollbackAndCleanup(commited, query); } } @@ -8909,6 +8904,7 @@ private void lockForUpdate() throws MetaException { query.setUnique(true); // only need to execute it to get db Lock query.execute(); + query.closeAll(); }).run(); } @@ -8974,8 +8970,8 @@ public void addNotificationEvent(NotificationEvent entry) { try { openTransaction(); lockForUpdate(); - Query objectQuery = pm.newQuery(MNotificationNextId.class); - Collection ids = (Collection) objectQuery.execute(); + query = pm.newQuery(MNotificationNextId.class); + Collection ids = (Collection) query.execute(); MNotificationNextId mNotificationNextId = null; boolean needToPersistId; if (CollectionUtils.isEmpty(ids)) { @@ -9504,12 +9500,7 @@ private String getPrimaryKeyConstraintName(String db_name, String tbl_name) thro } commited = commitTransaction(); } finally { - if (!commited) { - rollbackTransaction(); - } - if (query != null) { - query.closeAll(); - } + rollbackAndCleanup(commited, query); } return uniqueConstraints; } @@ -9574,12 +9565,7 @@ private String getPrimaryKeyConstraintName(String db_name, String tbl_name) thro } commited = commitTransaction(); } finally { - if (!commited) { - rollbackTransaction(); - } - if (query != null) { - query.closeAll(); - } + rollbackAndCleanup(commited, query); } return notNullConstraints; } diff --git a/standalone-metastore/src/test/java/org/apache/hadoop/hive/metastore/TestObjectStore.java b/standalone-metastore/src/test/java/org/apache/hadoop/hive/metastore/TestObjectStore.java index 372dee6369..ee5b3e0716 100644 --- a/standalone-metastore/src/test/java/org/apache/hadoop/hive/metastore/TestObjectStore.java +++ b/standalone-metastore/src/test/java/org/apache/hadoop/hive/metastore/TestObjectStore.java @@ -442,7 +442,7 @@ public void testQueryCloseOnError() throws Exception { spy.getAllFunctions(); spy.getAllTables(DB1); spy.getPartitionCount(); - Mockito.verify(spy, Mockito.times(2)) + Mockito.verify(spy, Mockito.times(3)) .rollbackAndCleanup(Mockito.anyBoolean(), Mockito.anyObject()); }