Refactor to avoid trivial errors in transactions
Include Fixing a bug not to commit the transaction on success
Change-Id: Ie1f823ab6d8fc6f54091d443d24ecc61336155da
diff --git a/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java
index ea8db97..a6921e4 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java
@@ -96,16 +96,14 @@
ResourceConsumer existing = txMap.putIfAbsent(resource, consumer);
// if the resource is already allocated to another consumer, the whole allocation fails
if (existing != null) {
- tx.abort();
- return false;
+ return abortTransaction(tx);
}
}
- tx.commit();
- return true;
+
+ return commitTransaction(tx);
} catch (TransactionException e) {
log.error("Exception thrown, abort the transaction", e);
- tx.abort();
- return false;
+ return abortTransaction(tx);
}
}
@@ -130,16 +128,14 @@
// if this single release fails (because the resource is allocated to another consumer,
// the whole release fails
if (!txMap.remove(resource, consumer)) {
- tx.abort();
- return false;
+ return abortTransaction(tx);
}
}
- return true;
+ return commitTransaction(tx);
} catch (TransactionException e) {
log.error("Exception thrown, abort the transaction", e);
- tx.abort();
- return false;
+ return abortTransaction(tx);
}
}
@@ -169,4 +165,26 @@
.map(x -> (Resource<S, T>) x.getKey())
.collect(Collectors.toList());
}
+
+ /**
+ * Abort the transaction.
+ *
+ * @param tx transaction context
+ * @return always false
+ */
+ private boolean abortTransaction(TransactionContext tx) {
+ tx.abort();
+ return false;
+ }
+
+ /**
+ * Commit the transaction.
+ *
+ * @param tx transaction context
+ * @return always true
+ */
+ private boolean commitTransaction(TransactionContext tx) {
+ tx.commit();
+ return true;
+ }
}