jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r476176 - in /jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core: fs/db/DatabaseFileSystem.java fs/db/DerbyFileSystem.java persistence/db/DatabasePersistenceManager.java persistence/db/DerbyPersistenceManager.java
Date Fri, 17 Nov 2006 15:21:48 GMT
Author: stefan
Date: Fri Nov 17 07:21:47 2006
New Revision: 476176

URL: http://svn.apache.org/viewvc?view=rev&rev=476176
Log:
- some minor refactoring
- added shutDownOnClose parameter to DerbyFileSystem

Modified:
    jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/fs/db/DatabaseFileSystem.java
    jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/fs/db/DerbyFileSystem.java
    jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/persistence/db/DatabasePersistenceManager.java
    jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/persistence/db/DerbyPersistenceManager.java

Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/fs/db/DatabaseFileSystem.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/fs/db/DatabaseFileSystem.java?view=diff&rev=476176&r1=476175&r2=476176
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/fs/db/DatabaseFileSystem.java
(original)
+++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/fs/db/DatabaseFileSystem.java
Fri Nov 17 07:21:47 2006
@@ -1105,11 +1105,11 @@
     }
 
     /**
-     * initializes the prepared statements and returns the list of them. please
-     * note, that this list is used to close the statements in the {@link #close()}
+     * Initializes the prepared statements and returns them in a list. please
+     * note that this list is used to close the statements in the {@link #close()}
      * call.
      *
-     * @return the list of prepared statements.
+     * @return the list of prepared statements
      * @throws SQLException
      */
     protected List initPreparedStatements() throws SQLException {

Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/fs/db/DerbyFileSystem.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/fs/db/DerbyFileSystem.java?view=diff&rev=476176&r1=476175&r2=476176
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/fs/db/DerbyFileSystem.java
(original)
+++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/fs/db/DerbyFileSystem.java
Fri Nov 17 07:21:47 2006
@@ -16,12 +16,12 @@
  */
 package org.apache.jackrabbit.core.fs.db;
 
-import org.apache.jackrabbit.core.fs.FileSystemException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.sql.DriverManager;
 import java.sql.SQLException;
+import java.sql.Connection;
 
 /**
  * <code>DerbyFileSystem</code> is a JDBC-based <code>FileSystem</code>
@@ -37,8 +37,11 @@
  * (default: <code>"org.apache.derby.jdbc.EmbeddedDriver"</code>)</li>
  * <li><code>schema</code>: type of schema to be used
  * (default: <code>"derby"</code>)</li>
- * <li><code>user</code>: the database user (default: <code>""</code>)</li>
- * <li><code>password</code>: the user's password (default: <code>""</code>)</li>
+ * <li><code>user</code>: the database user (default: <code>null</code>)</li>
+ * <li><code>password</code>: the user's password (default: <code>null</code>)</li>
+ * <li><code>shutdownOnClose</code>: if <code>true</code> (the
default) the
+ * database is shutdown when the last connection is closed;
+ * set this to <code>false</code> when using a standalone database</li>
  * </ul>
  * See also {@link DbFileSystem}.
  * <p/>
@@ -58,37 +61,44 @@
     private static Logger log = LoggerFactory.getLogger(DerbyFileSystem.class);
 
     /**
+     * Flag indicating whether this derby database should be shutdown on close.
+     */
+    protected boolean shutdownOnClose;
+
+    /**
      * Creates a new <code>DerbyFileSystem</code> instance.
      */
     public DerbyFileSystem() {
         // preset some attributes to reasonable defaults
         schema = "derby";
         driver = "org.apache.derby.jdbc.EmbeddedDriver";
-        schemaObjectPrefix = "";
-        user = "";
-        password = "";
+        shutdownOnClose = true;
         initialized = false;
     }
 
+    //----------------------------------------------------< setters & getters >
+
+    public boolean getShutdownOnClose() {
+        return shutdownOnClose;
+    }
+
+    public void setShutdownOnClose(boolean shutdownOnClose) {
+        this.shutdownOnClose = shutdownOnClose;
+    }
+
     //-----------------------------------------------< DbFileSystem overrides >
+
     /**
-     * {@inheritDoc}
+     * Closes the given connection and shuts down the embedded Derby
+     * database if <code>shutdownOnClose</code> is set to true.
+     *
+     * @param connection database connection
+     * @throws SQLException if an error occurs
+     * @see DatabaseFileSystem#closeConnection(Connection)
      */
-    public void close() throws FileSystemException {
-        if (!initialized) {
-            throw new IllegalStateException("not initialized");
-        }
-
+    protected void closeConnection(Connection connection) throws SQLException {
         // prepare connection url for issuing shutdown command
-        String url;
-        try {
-            url = con.getMetaData().getURL();
-        } catch (SQLException e) {
-            String msg = "error closing file system";
-            log.error(msg, e);
-            throw new FileSystemException(msg, e);
-        }
-
+        String url = connection.getMetaData().getURL();
         int pos = url.lastIndexOf(';');
         if (pos != -1) {
             // strip any attributes from connection url
@@ -96,15 +106,21 @@
         }
         url += ";shutdown=true";
 
-        // call base class implementation
-        super.close();
-
-        // now it's safe to shutdown the embedded Derby database
-        try {
-            DriverManager.getConnection(url);
-        } catch (SQLException e) {
-            // a shutdown command always raises a SQLException
-            log.info(e.getMessage());
+        // we have to reset the connection to 'autoCommit=true' before closing it;
+        // otherwise Derby would mysteriously complain about some pending uncommitted
+        // changes which can't possibly be true.
+        // @todo further investigate
+        connection.setAutoCommit(true);
+        connection.close();
+
+        if (shutdownOnClose) {
+            // now it's safe to shutdown the embedded Derby database
+            try {
+                DriverManager.getConnection(url);
+            } catch (SQLException e) {
+                // a shutdown command always raises a SQLException
+                log.info(e.getMessage());
+            }
         }
     }
 }

Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/persistence/db/DatabasePersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/persistence/db/DatabasePersistenceManager.java?view=diff&rev=476176&r1=476175&r2=476176
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/persistence/db/DatabasePersistenceManager.java
(original)
+++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/persistence/db/DatabasePersistenceManager.java
Fri Nov 17 07:21:47 2006
@@ -55,6 +55,8 @@
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.util.List;
+import java.util.LinkedList;
 
 /**
  * Abstract base class for database persistence managers. This class
@@ -89,6 +91,9 @@
     // jdbc connection
     protected Connection con;
 
+    // the list of prepared statements, used in close()
+    private List preparedStatements;
+
     // shared prepared statements for NodeState management
     protected PreparedStatement nodeStateInsert;
     protected PreparedStatement nodeStateUpdate;
@@ -189,53 +194,7 @@
         checkSchema();
 
         // prepare statements
-        nodeStateInsert =
-                con.prepareStatement("insert into "
-                + schemaObjectPrefix + "NODE (NODE_DATA, NODE_ID) values (?, ?)");
-        nodeStateUpdate =
-                con.prepareStatement("update "
-                + schemaObjectPrefix + "NODE set NODE_DATA = ? where NODE_ID = ?");
-        nodeStateSelect =
-                con.prepareStatement("select NODE_DATA from "
-                + schemaObjectPrefix + "NODE where NODE_ID = ?");
-        nodeStateSelectExist =
-                con.prepareStatement("select 1 from "
-                + schemaObjectPrefix + "NODE where NODE_ID = ?");
-        nodeStateDelete =
-                con.prepareStatement("delete from "
-                + schemaObjectPrefix + "NODE where NODE_ID = ?");
-
-        propertyStateInsert =
-                con.prepareStatement("insert into "
-                + schemaObjectPrefix + "PROP (PROP_DATA, PROP_ID) values (?, ?)");
-        propertyStateUpdate =
-                con.prepareStatement("update "
-                + schemaObjectPrefix + "PROP set PROP_DATA = ? where PROP_ID = ?");
-        propertyStateSelect =
-                con.prepareStatement("select PROP_DATA from "
-                + schemaObjectPrefix + "PROP where PROP_ID = ?");
-        propertyStateSelectExist =
-                con.prepareStatement("select 1 from "
-                + schemaObjectPrefix + "PROP where PROP_ID = ?");
-        propertyStateDelete =
-                con.prepareStatement("delete from "
-                + schemaObjectPrefix + "PROP where PROP_ID = ?");
-
-        nodeReferenceInsert =
-                con.prepareStatement("insert into "
-                + schemaObjectPrefix + "REFS (REFS_DATA, NODE_ID) values (?, ?)");
-        nodeReferenceUpdate =
-                con.prepareStatement("update "
-                + schemaObjectPrefix + "REFS set REFS_DATA = ? where NODE_ID = ?");
-        nodeReferenceSelect =
-                con.prepareStatement("select REFS_DATA from "
-                + schemaObjectPrefix + "REFS where NODE_ID = ?");
-        nodeReferenceSelectExist =
-                con.prepareStatement("select 1 from "
-                + schemaObjectPrefix + "REFS where NODE_ID = ?");
-        nodeReferenceDelete =
-                con.prepareStatement("delete from "
-                + schemaObjectPrefix + "REFS where NODE_ID = ?");
+        preparedStatements = initPreparedStatements();
 
         if (externalBLOBs) {
             /**
@@ -252,22 +211,6 @@
              * store BLOBs in db
              */
             blobStore = new DbBLOBStore();
-
-            blobInsert =
-                    con.prepareStatement("insert into "
-                    + schemaObjectPrefix + "BINVAL (BINVAL_DATA, BINVAL_ID) values (?, ?)");
-            blobUpdate =
-                    con.prepareStatement("update "
-                    + schemaObjectPrefix + "BINVAL set BINVAL_DATA = ? where BINVAL_ID =
?");
-            blobSelect =
-                    con.prepareStatement("select BINVAL_DATA from "
-                    + schemaObjectPrefix + "BINVAL where BINVAL_ID = ?");
-            blobSelectExist =
-                    con.prepareStatement("select 1 from "
-                    + schemaObjectPrefix + "BINVAL where BINVAL_ID = ?");
-            blobDelete =
-                    con.prepareStatement("delete from "
-                    + schemaObjectPrefix + "BINVAL where BINVAL_ID = ?");
         }
 
         initialized = true;
@@ -283,31 +226,14 @@
 
         try {
             // close shared prepared statements
-            closeStatement(nodeStateInsert);
-            closeStatement(nodeStateUpdate);
-            closeStatement(nodeStateSelect);
-            closeStatement(nodeStateSelectExist);
-            closeStatement(nodeStateDelete);
-
-            closeStatement(propertyStateInsert);
-            closeStatement(propertyStateUpdate);
-            closeStatement(propertyStateSelect);
-            closeStatement(propertyStateSelectExist);
-            closeStatement(propertyStateDelete);
-
-            closeStatement(nodeReferenceInsert);
-            closeStatement(nodeReferenceUpdate);
-            closeStatement(nodeReferenceSelect);
-            closeStatement(nodeReferenceSelectExist);
-            closeStatement(nodeReferenceDelete);
-
-            if (!externalBLOBs) {
-                closeStatement(blobInsert);
-                closeStatement(blobUpdate);
-                closeStatement(blobSelect);
-                closeStatement(blobSelectExist);
-                closeStatement(blobDelete);
-            } else {
+            if (preparedStatements != null) {
+                while (!preparedStatements.isEmpty()) {
+                    closeStatement((PreparedStatement) preparedStatements.remove(0));
+                }
+            }
+            preparedStatements = null;
+
+            if (externalBLOBs) {
                 // close BLOB file system
                 blobFS.close();
                 blobFS = null;
@@ -788,7 +714,7 @@
     //----------------------------------< misc. helper methods & overridables >
 
     /**
-     * Initializes the database connection used by this file system.
+     * Initializes the database connection used by this persistence manager.
      * <p>
      * Subclasses should normally override the {@link #getConnection()}
      * method instead of this one. The default implementation calls
@@ -988,6 +914,85 @@
         // JCR-595: Use the class explicitly instead of using getClass()
         // to avoid problems when subclassed in a different package
         return DatabasePersistenceManager.class.getResourceAsStream(schema + ".ddl");
+    }
+
+    /**
+     * Initializes the prepared statements and returns them in a list. please
+     * note that this list is used to close the statements in the {@link #close()}
+     * call.
+     *
+     * @return the list of prepared statements
+     * @throws SQLException
+     */
+    protected List initPreparedStatements() throws SQLException {
+        List stmts = new LinkedList();
+
+        stmts.add(nodeStateInsert =
+                con.prepareStatement("insert into "
+                + schemaObjectPrefix + "NODE (NODE_DATA, NODE_ID) values (?, ?)"));
+        stmts.add(nodeStateUpdate =
+                con.prepareStatement("update "
+                + schemaObjectPrefix + "NODE set NODE_DATA = ? where NODE_ID = ?"));
+        stmts.add(nodeStateSelect =
+                con.prepareStatement("select NODE_DATA from "
+                + schemaObjectPrefix + "NODE where NODE_ID = ?"));
+        stmts.add(nodeStateSelectExist =
+                con.prepareStatement("select 1 from "
+                + schemaObjectPrefix + "NODE where NODE_ID = ?"));
+        stmts.add(nodeStateDelete =
+                con.prepareStatement("delete from "
+                + schemaObjectPrefix + "NODE where NODE_ID = ?"));
+
+        stmts.add(propertyStateInsert =
+                con.prepareStatement("insert into "
+                + schemaObjectPrefix + "PROP (PROP_DATA, PROP_ID) values (?, ?)"));
+        stmts.add(propertyStateUpdate =
+                con.prepareStatement("update "
+                + schemaObjectPrefix + "PROP set PROP_DATA = ? where PROP_ID = ?"));
+        stmts.add(propertyStateSelect =
+                con.prepareStatement("select PROP_DATA from "
+                + schemaObjectPrefix + "PROP where PROP_ID = ?"));
+        stmts.add(propertyStateSelectExist =
+                con.prepareStatement("select 1 from "
+                + schemaObjectPrefix + "PROP where PROP_ID = ?"));
+        stmts.add(propertyStateDelete =
+                con.prepareStatement("delete from "
+                + schemaObjectPrefix + "PROP where PROP_ID = ?"));
+
+        stmts.add(nodeReferenceInsert =
+                con.prepareStatement("insert into "
+                + schemaObjectPrefix + "REFS (REFS_DATA, NODE_ID) values (?, ?)"));
+        stmts.add(nodeReferenceUpdate =
+                con.prepareStatement("update "
+                + schemaObjectPrefix + "REFS set REFS_DATA = ? where NODE_ID = ?"));
+        stmts.add(nodeReferenceSelect =
+                con.prepareStatement("select REFS_DATA from "
+                + schemaObjectPrefix + "REFS where NODE_ID = ?"));
+        stmts.add(nodeReferenceSelectExist =
+                con.prepareStatement("select 1 from "
+                + schemaObjectPrefix + "REFS where NODE_ID = ?"));
+        stmts.add(nodeReferenceDelete =
+                con.prepareStatement("delete from "
+                + schemaObjectPrefix + "REFS where NODE_ID = ?"));
+
+        if (!externalBLOBs) {
+            stmts.add(blobInsert =
+                    con.prepareStatement("insert into "
+                    + schemaObjectPrefix + "BINVAL (BINVAL_DATA, BINVAL_ID) values (?, ?)"));
+            stmts.add(blobUpdate =
+                    con.prepareStatement("update "
+                    + schemaObjectPrefix + "BINVAL set BINVAL_DATA = ? where BINVAL_ID =
?"));
+            stmts.add(blobSelect =
+                    con.prepareStatement("select BINVAL_DATA from "
+                    + schemaObjectPrefix + "BINVAL where BINVAL_ID = ?"));
+            stmts.add(blobSelectExist =
+                    con.prepareStatement("select 1 from "
+                    + schemaObjectPrefix + "BINVAL where BINVAL_ID = ?"));
+            stmts.add(blobDelete =
+                    con.prepareStatement("delete from "
+                    + schemaObjectPrefix + "BINVAL where BINVAL_ID = ?"));
+        }
+        return stmts;
     }
 
     //--------------------------------------------------------< inner classes >

Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/persistence/db/DerbyPersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/persistence/db/DerbyPersistenceManager.java?view=diff&rev=476176&r1=476175&r2=476176
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/persistence/db/DerbyPersistenceManager.java
(original)
+++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/persistence/db/DerbyPersistenceManager.java
Fri Nov 17 07:21:47 2006
@@ -58,7 +58,7 @@
  *       &lt;param name="url" value="jdbc:derby:${wsp.home}/db;create=true"/&gt;
  *       &lt;param name="schemaObjectPrefix" value="${wsp.name}_"/&gt;
  *       &lt;param name="externalBLOBs" value="false"/&gt;
- *  &lt;/PersistenceManager&gt;
+ *   &lt;/PersistenceManager&gt;
  * </pre>
  */
 public class DerbyPersistenceManager extends SimpleDbPersistenceManager {
@@ -84,6 +84,8 @@
         shutdownOnClose = true;
     }
 
+    //----------------------------------------------------< setters & getters >
+
     public boolean getShutdownOnClose() {
         return shutdownOnClose;
     }
@@ -95,8 +97,8 @@
     //------------------------------------------< DatabasePersistenceManager >
 
     /**
-     * Closes the given connection by shutting down the embedded Derby
-     * database.
+     * Closes the given connection and shuts down the embedded Derby
+     * database if <code>shutdownOnClose</code> is set to true.
      *
      * @param connection database connection
      * @throws SQLException if an error occurs



Mime
View raw message