You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by ss...@apache.org on 2013/06/18 22:16:11 UTC

git commit: - further performance improvements, roughly factor 8 now

Updated Branches:
  refs/heads/develop 257ff6bf7 -> 54b71f4c4


- further performance improvements, roughly factor 8 now


Project: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/commit/54b71f4c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/tree/54b71f4c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/diff/54b71f4c

Branch: refs/heads/develop
Commit: 54b71f4c47858d388cf4e660eaec3cc4a93cab61
Parents: 257ff6b
Author: Sebastian Schaffert <ss...@apache.org>
Authored: Tue Jun 18 22:16:05 2013 +0200
Committer: Sebastian Schaffert <ss...@apache.org>
Committed: Tue Jun 18 22:16:05 2013 +0200

----------------------------------------------------------------------
 .../persistence/mysql/statements.properties     |   3 +
 .../persistence/pgsql/statements.properties     |   3 +
 .../marmotta/kiwi/config/KiWiConfiguration.java |  35 +++++-
 .../kiwi/persistence/KiWiConnection.java        | 112 +++++++++++++++----
 .../kiwi/persistence/KiWiPersistence.java       |  20 +++-
 .../persistence/mysql/statements.properties     |   3 +
 .../persistence/pgsql/statements.properties     |   3 +
 .../persistence/mysql/statements.properties     |   1 +
 .../persistence/pgsql/statements.properties     |   1 +
 .../LDCachingKiWiPersistenceConnection.java     |   3 +
 .../persistence/mysql/statements.properties     |   1 +
 .../persistence/pgsql/statements.properties     |   1 +
 12 files changed, 163 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/54b71f4c/libraries/kiwi/kiwi-reasoner/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-reasoner/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties b/libraries/kiwi/kiwi-reasoner/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
index 68e60f8..499b934 100644
--- a/libraries/kiwi/kiwi-reasoner/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
+++ b/libraries/kiwi/kiwi-reasoner/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
@@ -19,12 +19,15 @@
 # get sequence numbers
 seq.rules.prep         = UPDATE seq_rules SET id=LAST_INSERT_ID(id+1);
 seq.rules              = SELECT LAST_INSERT_ID();
+seq.rules.set          = UPDATE seq_rules SET id=?;
 
 seq.programs.prep      = UPDATE seq_programs SET id=LAST_INSERT_ID(id+1);
 seq.programs           = SELECT LAST_INSERT_ID();
+seq.programs.set       = UPDATE seq_programs SET id=?;
 
 seq.justifications.prep= UPDATE seq_justifications SET id=LAST_INSERT_ID(id+1);
 seq.justifications     = SELECT LAST_INSERT_ID();
+seq.justifications.set = UPDATE seq_justifications SET id=?;
 
 
 # rules

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/54b71f4c/libraries/kiwi/kiwi-reasoner/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-reasoner/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties b/libraries/kiwi/kiwi-reasoner/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
index 2d7d788..2f3fa5c 100644
--- a/libraries/kiwi/kiwi-reasoner/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
+++ b/libraries/kiwi/kiwi-reasoner/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
@@ -18,8 +18,11 @@
 
 # get sequence numbers
 seq.rules             = SELECT nextval('seq_rules')
+seq.rules.set         = SELECT setval('seq_rules',?);
 seq.programs          = SELECT nextval('seq_programs')
+seq.programs.set      = SELECT setval('seq_programs',?);
 seq.justifications    = SELECT nextval('seq_justifications')
+seq.justifications.set= SELECT setval('seq_justifications',?);
 
 # rules
 rules.insert          = INSERT INTO reasoner_rules (id,name,description,body,createdAt) VALUES (?,?,?,?,now())

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/54b71f4c/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/config/KiWiConfiguration.java
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/config/KiWiConfiguration.java b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/config/KiWiConfiguration.java
index f8db80e..a80901a 100644
--- a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/config/KiWiConfiguration.java
+++ b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/config/KiWiConfiguration.java
@@ -65,11 +65,20 @@ public class KiWiConfiguration {
      */
     private boolean queryLoggingEnabled = false;
 
-
+    /**
+     * Enable batched commit (if supported by the database dialect). If this is enabled, the KiWiConnection will
+     * use an in-memory buffer for stored triples and nodes that are committed in a batch once the limit is reached
+     * or the connection committed. Enabling this can significantly improve the performance (EXPERIMENTAL).
+     */
     private boolean batchCommit;
 
     private int batchSize = 1000;
 
+    /**
+     * If enabled, and batchCommit is also true, load sequence values into static memory fields once and increment
+     * values purely in-memory. The last value is then written back on batch commits.
+     */
+    private boolean memorySequences = true;
 
     public KiWiConfiguration(String name, String jdbcUrl, String dbUser, String dbPassword, KiWiDialect dialect) {
         this(name, jdbcUrl, dbUser, dbPassword, dialect, null, null);
@@ -132,10 +141,20 @@ public class KiWiConfiguration {
         this.inferredContext = inferredContext;
     }
 
+    /**
+     * Return true if batched commit is enabled. If this is enabled, the KiWiConnection will
+     * use an in-memory buffer for stored triples and nodes that are committed in a batch once the limit is reached
+     * or the connection committed. Enabling this can significantly improve the performance (EXPERIMENTAL).
+     */
     public boolean isBatchCommit() {
         return batchCommit;
     }
 
+    /**
+     * Enable batched commit (if supported by the database dialect). If this is enabled, the KiWiConnection will
+     * use an in-memory buffer for stored triples and nodes that are committed in a batch once the limit is reached
+     * or the connection committed. Enabling this can significantly improve the performance (EXPERIMENTAL).
+     */
     public void setBatchCommit(boolean batchCommit) {
         if(dialect.isBatchSupported()) {
             this.batchCommit = batchCommit;
@@ -149,4 +168,18 @@ public class KiWiConfiguration {
     public void setBatchSize(int batchSize) {
         this.batchSize = batchSize;
     }
+
+    public boolean isMemorySequences() {
+        return memorySequences;
+    }
+
+    /**
+     * Enable in-memory sequences. If enabled, and batchCommit is also true, load sequence values into static memory
+     * fields once and increment values purely in-memory. The last value is then written back on batch commits. This
+     * feature can avoid many database accesses and connections and therefore give significant performance improvements.
+     * (EXPERIMENTAL).
+     */
+    public void setMemorySequences(boolean memorySequences) {
+        this.memorySequences = memorySequences;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/54b71f4c/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java
index d570fac..ecfbf1d 100644
--- a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java
+++ b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java
@@ -28,6 +28,8 @@ import org.apache.commons.lang3.LocaleUtils;
 import org.apache.marmotta.kiwi.caching.KiWiCacheManager;
 import org.apache.marmotta.kiwi.model.caching.TripleTable;
 import org.apache.marmotta.kiwi.model.rdf.*;
+import org.apache.marmotta.kiwi.persistence.mysql.MySQLDialect;
+import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect;
 import org.apache.marmotta.kiwi.persistence.util.ResultSetIteration;
 import org.apache.marmotta.kiwi.persistence.util.ResultTransformerFunction;
 import org.openrdf.model.Literal;
@@ -793,9 +795,9 @@ public class KiWiConnection {
 
 
     public synchronized long getNodeId() throws SQLException {
-        requireJDBCConnection();
+        long result = getNextSequence("seq.nodes");
 
-        return getNextSequence("seq.nodes");
+        return result;
     }
 
     /**
@@ -1535,7 +1537,7 @@ public class KiWiConnection {
         requireJDBCConnection();
 
         PreparedStatement statement = statementCache.get(key);
-        if(statement == null) {
+        if(statement == null || statement.isClosed()) {
             statement = connection.prepareStatement(dialect.getStatement(key));
             statementCache.put(key,statement);
         }
@@ -1552,26 +1554,73 @@ public class KiWiConnection {
      * @throws SQLException
      */
     public long getNextSequence(String sequenceName) throws SQLException {
-        requireJDBCConnection();
+        if(batchCommit && persistence.getConfiguration().isMemorySequences()) {
+            // use in-memory sequences
+            if(persistence.getMemorySequences() == null) {
+                persistence.setMemorySequences(new HashMap<String,Long>());
+            }
 
-        // retrieve a new node id and set it in the node object
+            synchronized (persistence.getMemorySequences()) {
+                Long sequence = persistence.getMemorySequences().get(sequenceName);
+                if(sequence == null) {
+                    requireJDBCConnection();
 
-        // if there is a preparation needed to update the transaction, run it first
-        if(dialect.hasStatement(sequenceName+".prep")) {
-            PreparedStatement prepNodeId = getPreparedStatement(sequenceName+".prep");
-            prepNodeId.executeUpdate();
-        }
+                    // load sequence value from database
+                    // if there is a preparation needed to update the transaction, run it first
+                    if(dialect.hasStatement(sequenceName+".prep")) {
+                        PreparedStatement prepNodeId = getPreparedStatement(sequenceName+".prep");
+                        prepNodeId.executeUpdate();
+                    }
 
-        PreparedStatement queryNodeId = getPreparedStatement(sequenceName);
-        ResultSet resultNodeId = queryNodeId.executeQuery();
-        try {
-            if(resultNodeId.next()) {
-                return resultNodeId.getLong(1);
-            } else {
-                throw new SQLException("the sequence did not return a new value");
+                    PreparedStatement queryNodeId = getPreparedStatement(sequenceName);
+                    ResultSet resultNodeId = queryNodeId.executeQuery();
+                    try {
+                        if(resultNodeId.next()) {
+                            sequence = resultNodeId.getLong(1);
+                        } else {
+                            throw new SQLException("the sequence did not return a new value");
+                        }
+                    } finally {
+                        resultNodeId.close();
+                    }
+
+                    // this is a very ugly hack needed by MySQL because the sequences are not atomic
+                    // it is only necessary for the nodes sequence, because the value factory always keeps
+                    // a connection open to it; we could solve it maybe differently by remembering the
+                    // connection responsible for a in-memory sequence...
+                    if(sequenceName.equals("seq.nodes") && dialect instanceof MySQLDialect) {
+                        connection.commit();
+                    }
+
+                } else {
+                    sequence += 1;
+                }
+                persistence.getMemorySequences().put(sequenceName,sequence);
+                return sequence;
+            }
+
+        } else {
+            requireJDBCConnection();
+
+            // retrieve a new node id and set it in the node object
+
+            // if there is a preparation needed to update the transaction, run it first
+            if(dialect.hasStatement(sequenceName+".prep")) {
+                PreparedStatement prepNodeId = getPreparedStatement(sequenceName+".prep");
+                prepNodeId.executeUpdate();
+            }
+
+            PreparedStatement queryNodeId = getPreparedStatement(sequenceName);
+            ResultSet resultNodeId = queryNodeId.executeQuery();
+            try {
+                if(resultNodeId.next()) {
+                    return resultNodeId.getLong(1);
+                } else {
+                    throw new SQLException("the sequence did not return a new value");
+                }
+            } finally {
+                resultNodeId.close();
             }
-        } finally {
-            resultNodeId.close();
         }
 
     }
@@ -1763,9 +1812,31 @@ public class KiWiConnection {
      * @see #setAutoCommit
      */
     public void commit() throws SQLException {
+        if(persistence.getMemorySequences() != null) {
+            requireJDBCConnection();
+
+            try {
+                synchronized (persistence.getMemorySequences()) {
+                    for(Map.Entry<String,Long> entry : persistence.getMemorySequences().entrySet()) {
+                        PreparedStatement updateSequence = getPreparedStatement(entry.getKey()+".set");
+                        updateSequence.setLong(1, entry.getValue());
+                        if(updateSequence.execute()) {
+                            updateSequence.getResultSet().close();
+                        } else {
+                            updateSequence.getUpdateCount();
+                        }
+                    }
+                }
+            } catch(SQLException ex) {
+                log.error("SQL exception:",ex);
+                throw ex;
+            }
+        }
+
         if(tripleBatch != null && tripleBatch.size() > 0) {
             flushBatch();
         }
+
         if(connection != null) {
             connection.commit();
         }
@@ -1854,7 +1925,7 @@ public class KiWiConnection {
     }
 
 
-    private void flushBatch() throws SQLException {
+    public void flushBatch() throws SQLException {
         if(batchCommit && tripleBatch != null) {
             requireJDBCConnection();
 
@@ -1888,6 +1959,7 @@ public class KiWiConnection {
                 }
                 insertTriple.executeBatch();
                 tripleBatch.clear();
+
             } catch (Throwable ex) {
                 ex.printStackTrace();
                 throw ex;

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/54b71f4c/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiPersistence.java
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiPersistence.java b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiPersistence.java
index 05cd986..b1f4a4d 100644
--- a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiPersistence.java
+++ b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiPersistence.java
@@ -37,6 +37,7 @@ import java.io.StringReader;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Collections;
+import java.util.Map;
 import java.util.Set;
 import java.util.WeakHashMap;
 
@@ -68,6 +69,12 @@ public class KiWiPersistence {
      */
     private KiWiConfiguration     configuration;
 
+    /**
+     * A map holding in-memory sequences to be used for sequence caching in case the appropriate configuration option
+     * is configued and batched commits are enabled.
+     */
+    private Map<String,Long> memorySequences;
+
 
     /**
      * A reference to the value factory used to access this store. Used for notifications when to flush batches.
@@ -218,7 +225,7 @@ public class KiWiPersistence {
                     log.info("connecting to existing KiWi database (version: {})",version);
                 }
             }
-            connection.commit();
+            connection.getJDBCConnection().commit();
         } catch (SQLException ex) {
             log.error("SQL exception while initialising database, rolling back");
             connection.rollback();
@@ -280,7 +287,7 @@ public class KiWiPersistence {
                         log.debug("- found table: {}",table);
                     }
                 }
-                connection.commit();
+                connection.getJDBCConnection().commit();
             } catch (SQLException ex) {
                 log.error("SQL exception while dropping database, rolling back");
                 connection.rollback();
@@ -423,6 +430,7 @@ public class KiWiPersistence {
         connectionPool.close();
 
         connectionPool = null;
+        memorySequences = null;
     }
 
     /**
@@ -444,4 +452,12 @@ public class KiWiPersistence {
     public KiWiConfiguration getConfiguration() {
         return configuration;
     }
+
+    public Map<String, Long> getMemorySequences() {
+        return memorySequences;
+    }
+
+    public void setMemorySequences(Map<String, Long> memorySequences) {
+        this.memorySequences = memorySequences;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/54b71f4c/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
index e40f24b..2645d5b 100644
--- a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
+++ b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
@@ -23,12 +23,15 @@ meta.version           = SELECT mvalue FROM metadata WHERE mkey = 'version';
 # get sequence numbers
 seq.nodes.prep         = UPDATE seq_nodes SET id=LAST_INSERT_ID(id+1);
 seq.nodes              = SELECT LAST_INSERT_ID();
+seq.nodes.set          = UPDATE seq_nodes SET id=?;
 
 seq.triples.prep       = UPDATE seq_triples SET id=LAST_INSERT_ID(id+1);
 seq.triples            = SELECT LAST_INSERT_ID();
+seq.triples.set        = UPDATE seq_triples SET id=?;
 
 seq.namespaces.prep    = UPDATE seq_namespaces SET id=LAST_INSERT_ID(id+1);
 seq.namespaces         = SELECT LAST_INSERT_ID();
+seq.namespaces.set     = UPDATE seq_namespaces SET id=?;
 
 # load entities
 load.node_by_id        = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,bvalue,lang,ltype,createdAt FROM nodes WHERE id = ?

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/54b71f4c/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
index 994af7d..9e1ffac 100644
--- a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
+++ b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
@@ -22,8 +22,11 @@ meta.version           = SELECT mvalue FROM metadata WHERE mkey = 'version';
 
 # get sequence numbers
 seq.nodes              = SELECT nextval('seq_nodes')
+seq.nodes.set          = SELECT setval('seq_nodes',?)
 seq.triples            = SELECT nextval('seq_triples')
+seq.triples.set        = SELECT setval('seq_triples',?)
 seq.namespaces         = SELECT nextval('seq_namespaces')
+seq.namespaces.set     = SELECT setval('seq_namespaces',?)
 
 # load entities
 load.node_by_id        = SELECT id,ntype,svalue,ivalue,dvalue,tvalue,bvalue,lang,ltype,createdAt FROM nodes WHERE id = ?

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/54b71f4c/libraries/kiwi/kiwi-versioning/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-versioning/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties b/libraries/kiwi/kiwi-versioning/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
index 65a39a2..5fce913 100644
--- a/libraries/kiwi/kiwi-versioning/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
+++ b/libraries/kiwi/kiwi-versioning/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
@@ -19,6 +19,7 @@
 # get sequence numbers
 seq.versions.prep     = UPDATE seq_versions SET id=LAST_INSERT_ID(id+1);
 seq.versions          = SELECT LAST_INSERT_ID();
+seq.versions.set      = UPDATE seq_versions SET id=?;
 
 load.version_by_id    = SELECT id,creator,createdAt FROM versions WHERE id = ?
 load.version_between  = SELECT id,creator,createdAt FROM versions WHERE createdAt > ? AND createdAt <= ? ORDER BY createdAt ASC

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/54b71f4c/libraries/kiwi/kiwi-versioning/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-versioning/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties b/libraries/kiwi/kiwi-versioning/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
index 442fb02..313e5b7 100644
--- a/libraries/kiwi/kiwi-versioning/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
+++ b/libraries/kiwi/kiwi-versioning/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
@@ -18,6 +18,7 @@
 
 # get sequence numbers
 seq.versions          = SELECT nextval('seq_versions')
+seq.versions.set      = SELECT setval('seq_versions',?)
 
 load.version_by_id    = SELECT id,creator,createdAt FROM versions WHERE id = ?
 load.version_between  = SELECT id,creator,createdAt FROM versions WHERE createdAt >= ? AND createdAt <= ? ORDER BY createdAt ASC

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/54b71f4c/libraries/ldcache/ldcache-backend-kiwi/src/main/java/org/apache/marmotta/ldcache/backend/kiwi/persistence/LDCachingKiWiPersistenceConnection.java
----------------------------------------------------------------------
diff --git a/libraries/ldcache/ldcache-backend-kiwi/src/main/java/org/apache/marmotta/ldcache/backend/kiwi/persistence/LDCachingKiWiPersistenceConnection.java b/libraries/ldcache/ldcache-backend-kiwi/src/main/java/org/apache/marmotta/ldcache/backend/kiwi/persistence/LDCachingKiWiPersistenceConnection.java
index 9ea30ed..63e4063 100644
--- a/libraries/ldcache/ldcache-backend-kiwi/src/main/java/org/apache/marmotta/ldcache/backend/kiwi/persistence/LDCachingKiWiPersistenceConnection.java
+++ b/libraries/ldcache/ldcache-backend-kiwi/src/main/java/org/apache/marmotta/ldcache/backend/kiwi/persistence/LDCachingKiWiPersistenceConnection.java
@@ -153,6 +153,9 @@ public class LDCachingKiWiPersistenceConnection  {
             throw new IllegalStateException("the resource contained in the cache entry is not a KiWiResource!");
         }
 
+        // needed before the entry can be inserted
+        connection.flushBatch();
+
         kEntry.setId(connection.getNextSequence("seq.ldcache"));
 
         PreparedStatement insertEntry = connection.getPreparedStatement("store.entry");

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/54b71f4c/libraries/ldcache/ldcache-backend-kiwi/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
----------------------------------------------------------------------
diff --git a/libraries/ldcache/ldcache-backend-kiwi/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties b/libraries/ldcache/ldcache-backend-kiwi/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
index 06864e1..95faac7 100644
--- a/libraries/ldcache/ldcache-backend-kiwi/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
+++ b/libraries/ldcache/ldcache-backend-kiwi/src/main/resources/org/apache/marmotta/kiwi/persistence/mysql/statements.properties
@@ -19,6 +19,7 @@
 # get sequence numbers
 seq.ldcache.prep         = UPDATE seq_ldcache SET id=LAST_INSERT_ID(id+1);
 seq.ldcache              = SELECT LAST_INSERT_ID();
+seq.ldcache.set          = UPDATE seq_ldcache SET id=?;
 
 load.entry_by_id      = SELECT id,retrieved_at,expires_at,resource_id,update_count,triple_count FROM ldcache_entries WHERE id = ?
 load.entry_by_uri     = SELECT E.id,retrieved_at,expires_at,resource_id,update_count,triple_count FROM ldcache_entries E, nodes N WHERE e.resource_id = N.id AND N.ntype = 'uri' and N.svalue = ?

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/54b71f4c/libraries/ldcache/ldcache-backend-kiwi/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
----------------------------------------------------------------------
diff --git a/libraries/ldcache/ldcache-backend-kiwi/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties b/libraries/ldcache/ldcache-backend-kiwi/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
index e8af848..d6cbb1f 100644
--- a/libraries/ldcache/ldcache-backend-kiwi/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
+++ b/libraries/ldcache/ldcache-backend-kiwi/src/main/resources/org/apache/marmotta/kiwi/persistence/pgsql/statements.properties
@@ -18,6 +18,7 @@
 
 # get sequence numbers
 seq.ldcache           = SELECT nextval('seq_ldcache')
+seq.ldcache.set       = SELECT setval('seq_ldcache',?)
 
 load.entry_by_id      = SELECT id,retrieved_at,expires_at,resource_id,update_count,triple_count FROM ldcache_entries WHERE id = ?
 load.entry_by_uri     = SELECT E.id,retrieved_at,expires_at,resource_id,update_count,triple_count FROM ldcache_entries E, nodes N WHERE e.resource_id = N.id AND N.ntype = 'uri' and N.svalue = ?