diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java index ed9cd42349..50a06db840 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java @@ -22,6 +22,7 @@ import java.util.Iterator; import java.util.List; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Future; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; @@ -1001,10 +1002,17 @@ public synchronized void close() throws Exception { this.storeProvider.close(); LockUtil.destroy(this.name); } + // Make sure that all transactions are closed in all threads + if (!this.tx.closed()) { + for (String key : this.tx.openedThreads) { + LOG.warn("thread [{}] did not close transaction", key); + } + } E.checkState(this.tx.closed(), "Ensure tx closed in all threads when closing graph '%s'", this.name); + } @Override @@ -1356,6 +1364,8 @@ private class TinkerPopTransaction extends AbstractThreadLocalTransaction { // Times opened from the upper layer private final AtomicInteger refs; + private final ConcurrentHashMap.KeySetView openedThreads = + ConcurrentHashMap.newKeySet(); // Flag opened of each thread private final ThreadLocal opened; // Backend transactions @@ -1470,6 +1480,7 @@ private void setOpened() { assert !this.opened.get(); this.opened.set(true); this.transactions.get().openedTime(DateUtil.now().getTime()); + this.openedThreads.add(Thread.currentThread().getName()); this.refs.incrementAndGet(); } @@ -1477,6 +1488,7 @@ private void setClosed() { // Just set flag opened=false to reuse the backend tx if (this.opened.get()) { this.opened.set(false); + this.openedThreads.remove(Thread.currentThread().getName()); this.refs.decrementAndGet(); } } diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java index 1395888611..52cedeb96d 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java @@ -154,6 +154,12 @@ public void restoreTasks() { LOG.info("restore task {}", task); this.restore(task); } + try { + this.graph.graphTransaction().commit(); + } + finally { + this.graph.closeTx(); + } } private Future restore(HugeTask task) {