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/07/11 14:32:46 UTC

git commit: tried with a fix for MARMOTTA-63, untested because it is hard to test

Updated Branches:
  refs/heads/develop deb2d4f30 -> b8d9e5670


tried with a fix for MARMOTTA-63, untested because it is hard to test


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

Branch: refs/heads/develop
Commit: b8d9e567075d9a69e24cbb03fafbadfb0f7feac7
Parents: deb2d4f
Author: Sebastian Schaffert <ss...@apache.org>
Authored: Thu Jul 11 14:32:38 2013 +0200
Committer: Sebastian Schaffert <ss...@apache.org>
Committed: Thu Jul 11 14:32:38 2013 +0200

----------------------------------------------------------------------
 .../kiwi/persistence/KiWiConnection.java        |  2 +-
 .../kiwi/persistence/KiWiGarbageCollector.java  | 20 +++++--
 .../kiwi/persistence/KiWiPersistence.java       | 57 +++++++++++++++++++-
 3 files changed, 71 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/b8d9e567/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 dee2fe0..039d388 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
@@ -1987,7 +1987,7 @@ public class KiWiConnection {
                 log.debug("database system does not allow closing statements");
             }
 
-            connection.close();
+            persistence.releaseJDBCConnection(connection);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/b8d9e567/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiGarbageCollector.java
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiGarbageCollector.java b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiGarbageCollector.java
index f832dd8..0f773c2 100644
--- a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiGarbageCollector.java
+++ b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiGarbageCollector.java
@@ -102,7 +102,7 @@ public class KiWiGarbageCollector extends Thread {
     private int garbageCollect() throws SQLException {
         round++;
 
-        Connection con = persistence.getJDBCConnection();
+        Connection con = persistence.getJDBCConnection(true);
         try {
             int count = 0;
 
@@ -121,8 +121,19 @@ public class KiWiGarbageCollector extends Thread {
 
             // garbage collect nodes (only every 10th garbage collection, only makes sense when we previously deleted triples ...)
             // TODO: this is currently not working, because the nodes remain in the cache; we need to find a different solution ...
-            //if(count > 0 && round % 10 == 1) {
-            if(false) {
+            if(count > 0 && round % 10 == 1) {
+                // flush all nodes from the value factory first
+                if(persistence.getValueFactory() != null) {
+                    KiWiConnection vfConnection = persistence.getConnection();
+                    try {
+                        persistence.getValueFactory().flushBatch(vfConnection);
+                    } finally {
+                        vfConnection.close();
+                    }
+                }
+
+
+                // then delete all unconnected nodes
                 try {
                     String gcNodesQuery = buildGCNodesQuery();
                     PreparedStatement stmtGcNodes = con.prepareStatement(gcNodesQuery);
@@ -138,7 +149,7 @@ public class KiWiGarbageCollector extends Thread {
 
             return count;
         } finally {
-            con.close();
+            persistence.releaseJDBCConnection(con, true);
         }
     }
 
@@ -149,7 +160,6 @@ public class KiWiGarbageCollector extends Thread {
      *
      * @see #start()
      * @see #stop()
-     * @see #Thread(ThreadGroup, Runnable, String)
      */
     @Override
     public void run() {

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/b8d9e567/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 04f872d..50e7849 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
@@ -45,6 +45,8 @@ import java.util.Set;
 import java.util.WeakHashMap;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantLock;
 
 /**
  * Add file description here!
@@ -86,6 +88,15 @@ public class KiWiPersistence {
      */
     private KiWiValueFactory      valueFactory;
 
+
+    /**
+     * This lock allows setting the backend into maintenance mode (by locking the write lock), which essentially
+     * grants an exclusive access to the database. This is currently used by the garbage collector, but can also
+     * be used in other situations-
+     */
+    private ReadWriteLock         maintenanceLock;
+
+
     @Deprecated
     public KiWiPersistence(String name, String jdbcUrl, String db_user, String db_password, KiWiDialect dialect) {
         this(new KiWiConfiguration(name,jdbcUrl,db_user,db_password,dialect));
@@ -187,7 +198,7 @@ public class KiWiPersistence {
             }
 
             try {
-                Connection con = getJDBCConnection();
+                Connection con = getJDBCConnection(true);
                 try {
                     for(String sequenceName : getDialect().listSequences(scriptName)) {
 
@@ -214,7 +225,7 @@ public class KiWiPersistence {
                         con.commit();
                     }
                 } finally {
-                    con.close();
+                    releaseJDBCConnection(con,true);
                 }
             } catch(SQLException ex) {
                 log.warn("database error: could not initialise in-memory sequences",ex);
@@ -383,7 +394,21 @@ public class KiWiPersistence {
      * @throws SQLException
      */
     public Connection getJDBCConnection() throws SQLException {
+        return getJDBCConnection(false);
+    }
+
+    /**
+     * Return a raw JDBC connection from the connection pool, which already has the auto-commit disabled.
+     * @return
+     * @throws SQLException
+     */
+    public Connection getJDBCConnection(boolean maintenance) throws SQLException {
         if(connectionPool != null) {
+            if(maintenance) {
+                maintenanceLock.writeLock().lock();
+            } else {
+                maintenanceLock.readLock().lock();
+            }
             Connection conn = connectionPool.getConnection();
             conn.setAutoCommit(false);
 
@@ -394,6 +419,34 @@ public class KiWiPersistence {
     }
 
 
+    /**
+     * Release the JDBC connection passed as argument. This method will close the connection and release
+     * any locks that might be held by the caller.
+     * @param con
+     * @throws SQLException
+     */
+    public void releaseJDBCConnection(Connection con) throws SQLException {
+        releaseJDBCConnection(con,false);
+    }
+
+    /**
+     * Release the JDBC connection passed as argument. This method will close the connection and release
+     * any locks that might be held by the caller.
+     * @param con
+     * @throws SQLException
+     */
+    public void releaseJDBCConnection(Connection con, boolean maintenance) throws SQLException {
+        try {
+            con.close();
+        } finally {
+            if(maintenance) {
+                maintenanceLock.writeLock().unlock();
+            } else {
+                maintenanceLock.readLock().unlock();
+            }
+        }
+    }
+
     private void forceCloseConnections() {
         if(connectionPool != null) {
             connectionPool.close(true);