You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by re...@apache.org on 2015/03/26 16:58:33 UTC

svn commit: r1669361 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb: RDBBlobStore.java RDBConnectionHandler.java RDBDocumentStore.java

Author: reschke
Date: Thu Mar 26 15:58:32 2015
New Revision: 1669361

URL: http://svn.apache.org/r1669361
Log:
OAK-1266 - address various FindBugs warnings

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBConnectionHandler.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java?rev=1669361&r1=1669360&r2=1669361&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java Thu Mar 26 15:58:32 2015
@@ -75,12 +75,15 @@ public class RDBBlobStore extends Cachin
                 Connection con = null;
                 try {
                     con = this.ch.getRWConnection();
+                    Statement stmt = null;
                     try {
-                        Statement stmt = con.createStatement();
+                        stmt = con.createStatement();
                         stmt.execute("drop table " + tname);
                         stmt.close();
+                        stmt = null;
                         con.commit();
                     } catch (SQLException ex) {
+                        this.ch.closeStatement(stmt);
                         LOG.debug("attempting to drop: " + tname);
                     }
                 } catch (SQLException ex) {
@@ -268,31 +271,39 @@ public class RDBBlobStore extends Cachin
         List<String> tablesCreated = new ArrayList<String>();
         List<String> tablesPresent = new ArrayList<String>();
 
+        Statement createStatement = null;
+
         try {
             for (String tableName : new String[] { this.tnData, this.tnMeta }) {
+                PreparedStatement checkStatement = null;
                 try {
-                    PreparedStatement stmt = con.prepareStatement("select ID from " + tableName + " where ID = ?");
-                    stmt.setString(1, "0");
-                    stmt.executeQuery();
+                    checkStatement = con.prepareStatement("select ID from " + tableName + " where ID = ?");
+                    checkStatement.setString(1, "0");
+                    checkStatement.executeQuery();
+                    checkStatement.close();
+                    checkStatement = null;
                     con.commit();
                     tablesPresent.add(tableName);
                 } catch (SQLException ex) {
+                    this.ch.closeStatement(checkStatement);
+ 
                     // table does not appear to exist
                     con.rollback();
 
                     DB db = DB.getValue(md.getDatabaseProductName());
 
-                    Statement stmt = con.createStatement();
+                    createStatement = con.createStatement();
 
                     if (this.tnMeta.equals(tableName)) {
                         String ct = db.getMetaTableCreationStatement(tableName);
-                        stmt.execute(ct);
+                        createStatement.execute(ct);
                     } else {
                         String ct = db.getDataTableCreationStatement(tableName);
-                        stmt.execute(ct);
+                        createStatement.execute(ct);
                     }
 
-                    stmt.close();
+                    createStatement.close();
+                    createStatement = null;
 
                     con.commit();
 
@@ -316,6 +327,7 @@ public class RDBBlobStore extends Cachin
 
             this.callStack = LOG.isDebugEnabled() ? new Exception("call stack of RDBBlobStore creation") : null;
         } finally {
+            this.ch.closeStatement(createStatement);
             this.ch.closeConnection(con);
         }
     }
@@ -469,18 +481,20 @@ public class RDBBlobStore extends Cachin
     @Override
     protected void mark(BlockId blockId) throws Exception {
         Connection con = this.ch.getRWConnection();
+        PreparedStatement prep = null;
         try {
             if (minLastModified == 0) {
                 return;
             }
             String id = StringUtils.convertBytesToHex(blockId.getDigest());
-            PreparedStatement prep = con.prepareStatement("update " + this.tnMeta + " set LASTMOD = ? where ID = ? and LASTMOD < ?");
+            prep = con.prepareStatement("update " + this.tnMeta + " set LASTMOD = ? where ID = ? and LASTMOD < ?");
             prep.setLong(1, System.currentTimeMillis());
             prep.setString(2, id);
             prep.setLong(3, minLastModified);
             prep.executeUpdate();
             prep.close();
         } finally {
+            this.ch.closeStatement(prep);
             con.commit();
             this.ch.closeConnection(con);
         }
@@ -497,29 +511,43 @@ public class RDBBlobStore extends Cachin
 
     private int sweepFromDatabase() throws SQLException {
         Connection con = this.ch.getRWConnection();
+        PreparedStatement prepCheck = null, prepDelMeta = null, prepDelData = null;
+        ResultSet rs = null;
         try {
             int count = 0;
-            PreparedStatement prep = con.prepareStatement("select ID from " + this.tnMeta + " where LASTMOD < ?");
-            prep.setLong(1, minLastModified);
-            ResultSet rs = prep.executeQuery();
+            prepCheck = con.prepareStatement("select ID from " + this.tnMeta + " where LASTMOD < ?");
+            prepCheck.setLong(1, minLastModified);
+            rs = prepCheck.executeQuery();
             ArrayList<String> ids = new ArrayList<String>();
             while (rs.next()) {
                 ids.add(rs.getString(1));
             }
-            prep = con.prepareStatement("delete from " + this.tnMeta + " where ID = ?");
-            PreparedStatement prepData = con.prepareStatement("delete from " + this.tnData + " where ID = ?");
+            rs.close();
+            rs = null;
+            prepCheck.close();
+            prepCheck = null;
+
+            prepDelMeta = con.prepareStatement("delete from " + this.tnMeta + " where ID = ?");
+            prepDelData = con.prepareStatement("delete from " + this.tnData + " where ID = ?");
+
             for (String id : ids) {
-                prep.setString(1, id);
-                prep.execute();
-                prepData.setString(1, id);
-                prepData.execute();
+                prepDelMeta.setString(1, id);
+                prepDelMeta.execute();
+                prepDelData.setString(1, id);
+                prepDelData.execute();
                 count++;
             }
-            prepData.close();
-            prep.close();
+            prepDelMeta.close();
+            prepDelMeta = null;
+            prepDelData.close();
+            prepDelData = null;
             minLastModified = 0;
             return count;
         } finally {
+            this.ch.closeResultSet(rs);
+            this.ch.closeStatement(prepCheck);
+            this.ch.closeStatement(prepDelMeta);
+            this.ch.closeStatement(prepDelData);
             con.commit();
             this.ch.closeConnection(con);
         }
@@ -535,10 +563,10 @@ public class RDBBlobStore extends Cachin
         }
 
         Connection con = this.ch.getRWConnection();
-        try {
-            PreparedStatement prepMeta = null;
-            PreparedStatement prepData = null;
+        PreparedStatement prepMeta = null;
+        PreparedStatement prepData = null;
 
+        try {
             StringBuilder inClause = new StringBuilder();
             int batch = chunkIds.size();
             for (int i = 0; i < batch; i++) {
@@ -569,8 +597,12 @@ public class RDBBlobStore extends Cachin
             prepMeta.execute();
             prepData.execute();
             prepMeta.close();
+            prepMeta = null;
             prepData.close();
+            prepData = null;
         } finally {
+            this.ch.closeStatement(prepMeta);
+            this.ch.closeStatement(prepData);
             con.commit();
             this.ch.closeConnection(con);
         }
@@ -633,23 +665,29 @@ public class RDBBlobStore extends Cachin
             Connection connection = null;
             try {
                 connection = this.ch.getROConnection();
+                PreparedStatement prep = null;
+                ResultSet rs = null;
                 try {
-                    PreparedStatement prep = connection.prepareStatement(query.toString());
+                    prep = connection.prepareStatement(query.toString());
                     int idx = 1;
                     if (maxLastModifiedTime > 0) {
                         prep.setLong(idx++, maxLastModifiedTime);
                     }
                     if (lastId != null) {
-                        prep.setString(idx++, lastId);
+                        prep.setString(idx, lastId);
                     }
                     prep.setFetchSize(BATCHSIZE);
-                    ResultSet rs = prep.executeQuery();
+                    rs = prep.executeQuery();
                     while (rs.next()) {
                         lastId = rs.getString(1);
                         results.add(lastId);
                     }
+                    rs.close();
+                    rs = null;
                     return !results.isEmpty();
                 } finally {
+                    this.ch.closeResultSet(rs);
+                    this.ch.closeStatement(prep);
                     connection.commit();
                     this.ch.closeConnection(connection);
                 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBConnectionHandler.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBConnectionHandler.java?rev=1669361&r1=1669360&r2=1669361&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBConnectionHandler.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBConnectionHandler.java Thu Mar 26 15:58:32 2015
@@ -17,8 +17,11 @@
 package org.apache.jackrabbit.oak.plugins.document.rdb;
 
 import java.sql.Connection;
+import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Statement;
 
+import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import javax.sql.DataSource;
@@ -103,4 +106,36 @@ public class RDBConnectionHandler {
             }
         }
     }
+
+    /**
+     * Closes a {@link Statement}, logging potential problems.
+     * @return null
+     */
+    public <T extends Statement> T closeStatement(@CheckForNull T stmt) {
+        if (stmt != null) {
+            try {
+                stmt.close();
+            } catch (SQLException ex) {
+                LOG.debug("Closing statement", ex);
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Closes a {@link ResultSet}, logging potential problems.
+     * 
+     * @return null
+     */
+    public ResultSet closeResultSet(@CheckForNull ResultSet rs) {
+        if (rs != null) {
+            try {
+                rs.close();
+            } catch (SQLException ex) {
+                LOG.debug("Closing result set", ex);
+            }
+        }
+
+        return null;
+    }
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java?rev=1669361&r1=1669360&r2=1669361&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java Thu Mar 26 15:58:32 2015
@@ -323,7 +323,7 @@ public class RDBDocumentStore implements
                     } catch (SQLException ex) {
                         LOG.debug("attempting to drop: " + tname, ex);
                     } finally {
-                        close(stmt);
+                        this.ch.closeStatement(stmt);
                     }
                 } catch (SQLException ex) {
                     LOG.debug("attempting to drop: " + tname, ex);
@@ -685,7 +685,7 @@ public class RDBDocumentStore implements
                 con.commit();
             }
             finally {
-                close(stmt);
+                this.ch.closeStatement(stmt);
             }
         }
 
@@ -763,9 +763,9 @@ public class RDBDocumentStore implements
             }
         }
         finally {
-            close(checkResultSet);
-            close(checkStatement);
-            close(creatStatement);
+            this.ch.closeResultSet(checkResultSet);
+            this.ch.closeStatement(checkStatement);
+            this.ch.closeStatement(creatStatement);
         }
     }
 
@@ -1687,30 +1687,6 @@ public class RDBDocumentStore implements
         return n != null ? n.longValue() : -1;
     }
 
-    private static <T extends Statement> T close(@CheckForNull T stmt) {
-        if (stmt != null) {
-            try {
-                stmt.close();
-            } catch (SQLException ex) {
-                LOG.debug("Closing statement", ex);
-            }
-        }
-
-        return null;
-    }
-
-    private static ResultSet close(@CheckForNull ResultSet rs) {
-        if (rs != null) {
-            try {
-                rs.close();
-            } catch (SQLException ex) {
-                LOG.debug("Closing result set", ex);
-            }
-        }
-
-        return null;
-    }
-
     /**
      * Adds a document to the {@link #nodesCache} iff there is no document in
      * the cache with the document key. This method does not acquire a lock from