アンマネージドエクステンションを作成しています。Neo4jでユニークなシーケンス番号を作成する方法は?
Neo4Jにはシーケンス番号を取得するための機能が組み込まれていないので、私はこのメソッドを書いてそのようなものを実現しました。それは "同期"キーワードでうまく動作しますが、それがなければ、同時に複数のスレッドから呼び出すテストケースでDeadlockDetectedExceptionを使用しようとしています。
これはこの問題を解決する良い方法ですか?
"acquireReadLock"で十分ではないのに、なぜメソッドを "同期化"する必要があるのですか?
public synchronized static int getNextSequence(Node node, String property) {
int sequence = 0;
GraphDatabaseService graphDb = node.getGraphDatabase();
try(Transaction t = graphDb.beginTx()) {
t.acquireReadLock(node);
sequence = (int) node.getProperty(property);
node.setProperty(property, sequence + 1);
//The lock is automatic released on t.success().
t.success();
} catch (Exception e) {
log.error("Failed to get sequence for node: ({}), property: ({}), exception: ({})", node, property, e);
throw e;
}
return sequence;
}
EDIT
@cybersamからの応答の後、私はDeadlockProblemの問題を解決していないと私はもはや同期方法を確認する必要がありacquireWriteLockにacquireReadLockを変更しました。
更新されたコードは次のようになります。
public static int getNextSequence(Node node, String property) {
int sequence = 0;
GraphDatabaseService graphDb = node.getGraphDatabase();
try(Transaction t = graphDb.beginTx()) {
t.acquireWriteLock(node);
sequence = (int) node.getProperty(property);
node.setProperty(property, sequence + 1);
//The lock is automatic released on t.success().
t.success();
} catch (Exception e) {
log.error("Failed to get sequence for node: ({}), property: ({}), exception: ({})", node, property, e);
throw e;
}
return sequence;
}
あなたは本当に正しく、ありがとうございます。 :) –