Repository: marmotta Updated Branches: refs/heads/develop ea28ddb1a -> 83be62b49
MARMOTTA-620: Merged patch from @marktriggs on github, closes PR #15 Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/83be62b4 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/83be62b4 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/83be62b4 Branch: refs/heads/develop Commit: 83be62b495ce73e49313c97ebc4dfd3ac7881a26 Parents: ea28ddb Author: Sergio Fernández <[email protected]> Authored: Tue Feb 2 10:27:53 2016 +0100 Committer: Sergio Fernández <[email protected]> Committed: Tue Feb 2 10:27:53 2016 +0100 ---------------------------------------------------------------------- .../marmotta/kiwi/sail/KiWiValueFactory.java | 28 ++++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/83be62b4/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java index 42dd4ed..3a717a2 100644 --- a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java +++ b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java @@ -565,6 +565,8 @@ public class KiWiValueFactory implements ValueFactory { KiWiTriple result = new KiWiTriple(ksubject,kpredicate,kobject,kcontext); + boolean needsDBLookup = false; + synchronized (registry) { long tripleId = registry.lookupKey(cacheKey); @@ -575,13 +577,29 @@ public class KiWiValueFactory implements ValueFactory { registry.registerKey(cacheKey, connection.getTransactionId(), result.getId()); } else { // not found in registry, try loading from database - result.setId(connection.getTripleId(ksubject,kpredicate,kobject,kcontext)); + needsDBLookup = true; } + } + + if(needsDBLookup) { + result.setId(connection.getTripleId(ksubject,kpredicate,kobject,kcontext)); + } - // triple has no id from registry or database, so we create one and flag it for reasoning - if(result.getId() < 0) { - result.setId(connection.getNextSequence()); - result.setNewTriple(true); + // triple has no id from registry or database, so we create one and flag it for reasoning + if(result.getId() < 0) { + synchronized (registry) { + // It's possible a concurrent thread might have created this + // triple while we were blocked. Check the registry again. + long tripleId = registry.lookupKey(cacheKey); + + if(tripleId >= 0) { + // A concurrent thread got in first. Take the one it created. + result.setId(tripleId); + } else { + // Create the new triple + result.setId(connection.getNextSequence()); + result.setNewTriple(true); + } registry.registerKey(cacheKey, connection.getTransactionId(), result.getId()); }
