felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ccust...@apache.org
Subject svn commit: r785865 - in /felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main: DefaultJDBCLock.java Main.java Statements.java
Date Thu, 18 Jun 2009 00:30:17 GMT
Author: ccustine
Date: Thu Jun 18 00:30:17 2009
New Revision: 785865

URL: http://svn.apache.org/viewvc?rev=785865&view=rev
Log:
FELIX-1192 - KARAF: Locking error in DefaultJDBCLock, also contains an eventual OutOfMemory
error on locked processes.

Modified:
    felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/DefaultJDBCLock.java
    felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/Main.java
    felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/Statements.java

Modified: felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/DefaultJDBCLock.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/DefaultJDBCLock.java?rev=785865&r1=785864&r2=785865&view=diff
==============================================================================
--- felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/DefaultJDBCLock.java
(original)
+++ felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/DefaultJDBCLock.java
Thu Jun 18 00:30:17 2009
@@ -26,7 +26,7 @@
 
 /**
  * Represents an exclusive lock on a database,
- * used to avoid multiple SMX instances attempting
+ * used to avoid multiple Karaf instances attempting
  * to become master.
  * 
  * @version $Revision: $
@@ -70,45 +70,41 @@
         }
         if (user == null) { user = ""; }
         if (password == null) { password = ""; }
-        try {
-            obtainLock();
-        } catch (Exception e) {
-            System.err.println("Error occured while attempting to obtain connection: " +
e.getMessage());
-        }
     }
 
     /**
-     * obtainLock - obtain the lock connection.
+     * setUpdateCursor - Send Update directive to data base server.
      *
      * @throws Exception
      */
-    private void obtainLock() throws Exception {
+    private boolean setUpdateCursor() throws Exception {
         PreparedStatement statement = null;
-        while (true) {
-            try {
+        boolean result = false;
+        try { 
+            if ((lockConnection == null) || (lockConnection.isClosed())) { 
                 lockConnection = getConnection(driver, url, user, password);
                 lockConnection.setAutoCommit(false);
                 statements.init(lockConnection);
-                String sql = statements.testLockTableStatus();
-                statement = lockConnection.prepareStatement(sql);
-                statement.execute();
-                break;
-            } catch (Exception e) {
-                System.err.println("Could not obtain lock: " + e.getMessage());
-                Thread.sleep(this.timeout);
-            } finally {
-                if (null != statement) {
-                    try {
-                        statement.close();
-                    } catch (SQLException e1) {
-                        System.err.println("Caught while closing statement: " + e1.getMessage());
-                    }
-                    statement = null;
+            }
+            //statements.init(lockConnection);
+            String sql = statements.setUpdateCursor();
+            statement = lockConnection.prepareStatement(sql);
+            result = statement.execute();
+        } catch (Exception e) {
+            System.err.println("Could not obtain connection: " + e.getMessage());
+        } finally {
+            if (null != statement) {
+                try {
+                    System.err.println("Cleaning up DB connection.");
+                    statement.close();
+                } catch (SQLException e1) {
+                    System.err.println("Caught while closing statement: " + e1.getMessage());
                 }
+                statement = null;
             }
-            Thread.sleep(this.timeout);
         }
         System.out.println("Connected to data source: " + url);
+        return result;
     }
 
     /**
@@ -120,7 +116,10 @@
         PreparedStatement statement = null;
         boolean result = false;
         try {
-            if (lockConnection.isClosed()) { obtainLock(); } 
+            if (!setUpdateCursor()) {
+                System.err.println("Could not set DB update cursor");
+                return result;
+            }
             long time = System.currentTimeMillis();
             statement = lockConnection.prepareStatement(statements.getLockUpdateStatement(time));
             int rows = statement.executeUpdate();
@@ -155,18 +154,32 @@
      * isAlive - test if lock still exists.
      */
     public boolean isAlive() throws Exception {
-        if (lockConnection == null) { return false; }
+        if ((lockConnection == null) || (lockConnection.isClosed())) { 
+            System.err.println("Lost lock!");
+            return false; 
+        }
         PreparedStatement statement = null;
+        boolean result = true;
         try { 
-            lockConnection.setAutoCommit(false);
-            statements.init(lockConnection);
-            String sql = statements.testLockTableStatus();
-            statement = lockConnection.prepareStatement(sql);
-            statement.execute();
+            long time = System.currentTimeMillis();
+            statement = lockConnection.prepareStatement(statements.getLockUpdateStatement(time));
+            int rows = statement.executeUpdate();
+            if (rows != 1) {
+                result = false;
+            }
         } catch (Exception ex) {
+            System.err.println("Error occured while testing lock: " + ex + " " + ex.getMessage());
             return false;
+        } finally {
+            if (statement != null) {
+                try {
+                    statement.close();
+                } catch (Exception ex1) {
+                    System.err.println("Error occured after testing lock: " + ex1.getMessage());
+                }
+            }
         }
-        return true;
+        return result;
     }
 
     /**
@@ -186,6 +199,7 @@
             Class.forName(driver);
             conn = DriverManager.getConnection(url + ";create=true", username, password);
         } catch (Exception e) {
+            System.err.println("Error occured while setting up JDBC connection: " + e);
             throw e; 
         }
         return conn;

Modified: felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/Main.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/Main.java?rev=785865&r1=785864&r2=785865&view=diff
==============================================================================
--- felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/Main.java (original)
+++ felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/Main.java Thu Jun 18
00:30:17 2009
@@ -995,6 +995,7 @@
                 String clz = props.getProperty(PROPERTY_LOCK_CLASS, PROPERTY_LOCK_CLASS_DEFAULT);
                 lock = (Lock) Class.forName(clz).getConstructor(Properties.class).newInstance(props);
                 boolean lockLogged = false;
+                setStartLevel(lockStartLevel);
                 for (;;) {
                     if (lock.lock()) {
                         if (lockLogged) {
@@ -1017,7 +1018,9 @@
                         lockLogged = true;
                     }
                     Thread.sleep(lockDelay);
-                }
+                } 
+            } else {
+                setStartLevel(defaultStartLevel);
             }
         } catch (Exception e) {
             e.printStackTrace();

Modified: felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/Statements.java
URL: http://svn.apache.org/viewvc/felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/Statements.java?rev=785865&r1=785864&r2=785865&view=diff
==============================================================================
--- felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/Statements.java (original)
+++ felix/trunk/karaf/main/src/main/java/org/apache/felix/karaf/main/Statements.java Thu Jun
18 00:30:17 2009
@@ -38,7 +38,7 @@
         this.lockPopulateStatement="insert into " + lockTableName + " (TIME, CLUSTER) values
(1, '" + clusterName + "')";
     }
 
-    public String testLockTableStatus() {
+    public String setUpdateCursor() {
         String test = "SELECT * FROM " + lockTableName + " FOR UPDATE";
         return test;
     }
@@ -92,7 +92,7 @@
             System.err.println(ignore);
         } finally {
             try {
-                s.close();
+                if (s != null) { s.close(); }
             } catch (Throwable e) {
                 // ignore
             }



Mime
View raw message