]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-4077: Creation of database indexes and modification of field datatypes during...
authorAron Roberts <aron@socrates.berkeley.edu>
Mon, 13 Jun 2011 20:16:37 +0000 (20:16 +0000)
committerAron Roberts <aron@socrates.berkeley.edu>
Mon, 13 Jun 2011 20:16:37 +0000 (20:16 +0000)
services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java
services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java
services/common/src/main/java/org/collectionspace/services/common/init/ModifyFieldDatatypes.java
services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java

index 058bf38894441269991dc4a2898363b1263a3231..c0dd989d4080b8e1ffa2e9d17c008192f1b0e8b0 100644 (file)
@@ -56,7 +56,9 @@ public class ServiceMain {
     private static final String DEFAULT_ADMIN_PASSWORD = "Administrator";\r
     private static final String DEFAULT_READER_PASSWORD = "reader";\r
     \r
-    public static String DEFAULT_REPOSITORY_NAME = "CspaceDS";\r
+    public static final String NUXEO_REPOSITORY_NAME = "NuxeoDS";\r
+    public static final String CSPACE_REPOSITORY_NAME = "CspaceDS";\r
+    public static final String DEFAULT_REPOSITORY_NAME = CSPACE_REPOSITORY_NAME;\r
 \r
     private ServiceMain() {\r
        //empty\r
index 92ad73a3464de78457e5e0ab490ba5e58614e652..529bc5f89154ac3b594f4502c2c3f139804feed2 100755 (executable)
@@ -131,7 +131,16 @@ public class AddIndices extends InitHandler implements IInitHandler {
                 throw new Exception("Unrecognized database system " + databaseProductType);\r
             }\r
             if (sql != null && ! sql.trim().isEmpty()) {\r
-                rows = JDBCTools.executeUpdate(sql);\r
+                // Assumes indicies will only be created at post-init time\r
+                // for the Nuxeo repository.\r
+                // \r
+                // To date, for the CSpace repository, indices have typically been\r
+                // created during the build process, via manual or generated\r
+                // DDL SQL scripts.\r
+                //\r
+                // If this assumption is no longer valid, we might instead\r
+                // identify the relevant repository from the table name here.\r
+                rows = JDBCTools.executeUpdate(sql, JDBCTools.getNuxeoRepositoryName());\r
                 logger.trace("Index added to column ("+columnName+") on table ("+tableName+")");\r
             }\r
             return rows;\r
@@ -147,8 +156,10 @@ public class AddIndices extends InitHandler implements IInitHandler {
         // FIXME: May need to qualify table name by database/catalog,\r
         // as table names likely will not be globally unique across same\r
         // (although index names *may* be unique within scope).\r
-        // - Pass in database name as parameter, retrieved via\r
-        //   getDatabaseName(field) in onRepositoryInitialized, above.\r
+        //\r
+        // If we do need to do this, we might:\r
+        // - Pass in the database name as a parameter to this method, retrieved\r
+        //   via getDatabaseName(field) in onRepositoryInitialized, above.\r
         // - Add 'IN databaseName' after tableName in MySQL variant, below.\r
         //   (PostgreSQL variant, below, uses a view that doesn't include\r
         //   a foreign key for associating a database/catalog to the index.)\r
@@ -173,7 +184,16 @@ public class AddIndices extends InitHandler implements IInitHandler {
         }\r
 \r
         try {\r
-            conn = JDBCTools.getConnection(JDBCTools.getDefaultRepositoryName());\r
+            // Assumes indicies will only be created at post-init time\r
+            // for the Nuxeo repository.\r
+            // \r
+            // To date, for the CSpace repository, indices have typically been\r
+            // created during the build process, via manual or generated\r
+            // DDL SQL scripts.\r
+            //\r
+            // If this assumption is no longer valid, we might instead\r
+            // identify the relevant repository from the table name here.\r
+            conn = JDBCTools.getConnection(JDBCTools.getNuxeoRepositoryName());\r
             stmt = conn.createStatement();\r
             rs = stmt.executeQuery(sql);\r
             if (rs.last()) {\r
index 9f9788a1659571ca2c6e09a8e98fa4277554e812..d2638f0dfb5437b96f14129446d31045e0a08b98 100644 (file)
@@ -82,7 +82,16 @@ public class ModifyFieldDatatypes extends InitHandler implements IInitHandler {
                 } else {
                     throw new Exception("Unrecognized database system.");
                 }
-                rows = JDBCTools.executeUpdate(sql);
+                // Assumes field datatypes will only be modified at post-init
+                // time for the Nuxeo repository.
+                // 
+                // To date, for the CSpace repository, field datatypes have
+                // typically been specified during the build process, via
+                // manual or generated DDL SQL scripts.
+                //
+                // If this assumption is no longer valid, we might instead
+                // identify the relevant repository from the table name here.
+                rows = JDBCTools.executeUpdate(sql, JDBCTools.getNuxeoRepositoryName());
             }
         } catch (Exception e) {
             throw e;
@@ -138,7 +147,7 @@ public class ModifyFieldDatatypes extends InitHandler implements IInitHandler {
         }
 
         try {
-            conn = JDBCTools.getConnection(JDBCTools.getDefaultRepositoryName());
+            conn = JDBCTools.getConnection(JDBCTools.getNuxeoRepositoryName());
             stmt = conn.createStatement();
             rs = stmt.executeQuery(sql);
             while (rs.next()) {
index 4348ac9de7dc63ec1a06230e5f6922e00c605ebc..b29914a30c3990169bae6a39a088b4208577b875 100755 (executable)
@@ -26,6 +26,7 @@ import javax.naming.InitialContext;
 import javax.naming.NamingException;\r
 import javax.security.auth.login.LoginException;\r
 import javax.sql.DataSource;\r
+import java.sql.DatabaseMetaData;\r
 import java.sql.Connection;\r
 import java.sql.ResultSet;\r
 import java.sql.SQLException;\r
@@ -42,7 +43,7 @@ public class JDBCTools {
     final static Logger logger = LoggerFactory.getLogger(JDBCTools.class);\r
 \r
     public static Connection getConnection(String repositoryName) throws LoginException, SQLException {\r
-        if (Tools.isEmpty(repositoryName)) {\r
+        if (Tools.isBlank(repositoryName)) {\r
             repositoryName = getDefaultRepositoryName();\r
         }\r
         InitialContext ctx = null;\r
@@ -59,21 +60,31 @@ public class JDBCTools {
             LoginException le = new LoginException("Error looking up DataSource from: " + repositoryName);\r
             le.initCause(ex);\r
             throw le;\r
+        } catch (SQLException e) {\r
+            throw e;\r
         } finally {\r
             if (ctx != null) {\r
                 try {\r
                     ctx.close();\r
                 } catch (Exception e) {\r
+                    // Do nothing with exception in 'finally' clause.\r
                 }\r
             }\r
         }\r
     }\r
-\r
+    \r
     public static ResultSet executeQuery(String sql) throws Exception {\r
+        return executeQuery(sql, getDefaultRepositoryName());\r
+    }\r
+\r
+    public static ResultSet executeQuery(String sql, String repositoryName) throws Exception {\r
         Connection conn = null;\r
         Statement stmt = null;\r
         try {\r
-            conn = getConnection(getDefaultRepositoryName());\r
+            if (Tools.isBlank(repositoryName)) {\r
+                repositoryName = getDefaultRepositoryName();\r
+            }\r
+            conn = getConnection(repositoryName);\r
             stmt = conn.createStatement();\r
             ResultSet rs = stmt.executeQuery(sql);\r
             stmt.close();\r
@@ -99,12 +110,19 @@ public class JDBCTools {
             }\r
         }\r
     }\r
-\r
+    \r
     public static int executeUpdate(String sql) throws Exception {\r
+        return executeUpdate(sql, getDefaultRepositoryName());\r
+    }\r
+\r
+    public static int executeUpdate(String sql, String repositoryName) throws Exception {\r
         Connection conn = null;\r
         Statement stmt = null;\r
         try {\r
-            conn = getConnection(getDefaultRepositoryName());\r
+            if (Tools.isBlank(repositoryName)) {\r
+                repositoryName = getDefaultRepositoryName();\r
+            }\r
+            conn = getConnection(repositoryName);\r
             stmt = conn.createStatement();\r
             int rows = stmt.executeUpdate(sql);\r
             stmt.close();\r
@@ -140,6 +158,16 @@ public class JDBCTools {
         }\r
     }\r
 \r
+    /**\r
+     * Returns the database product name, from the metadata for a\r
+     * JDBC connection to the default repository.\r
+     * \r
+     * Assumes that the database product name will be the same for the\r
+     * default repository and for all other repositories to which JDBC\r
+     * connections will be made, through the methods of this class.\r
+     * \r
+     * @return the database product name\r
+     */\r
     public static String getDatabaseProductName() {\r
         String productName = "";\r
         Connection conn = null;\r
@@ -160,6 +188,13 @@ public class JDBCTools {
         return productName;\r
     }\r
 \r
+    /**\r
+     * Returns an enumerated value uniquely identifying the database product type;\r
+     * e.g. MySQL, PostgreSQL, based on the database product name.\r
+     * \r
+     * @return an enumerated value identifying the database product type\r
+     * @throws Exception \r
+     */\r
     public static DatabaseProductType getDatabaseProductType() throws Exception {\r
         DatabaseProductType productType = DatabaseProductType.UNRECOGNIZED;\r
         String productName = getDatabaseProductName();\r
@@ -176,4 +211,26 @@ public class JDBCTools {
     public static String getDefaultRepositoryName() {\r
         return ServiceMain.DEFAULT_REPOSITORY_NAME;\r
     }\r
+    \r
+    public static String getNuxeoRepositoryName() {\r
+        return ServiceMain.NUXEO_REPOSITORY_NAME;\r
+    }\r
+\r
+    /**\r
+     * Prints metadata, such as database username and connection URL,\r
+     * for an open JDBC connection.  This is a utility method for use\r
+     * during debugging.\r
+     * \r
+     * @param conn an open JDBC Connection\r
+     * @throws SQLException \r
+     */\r
+    private static void printConnectionMetaData(Connection conn) throws SQLException {\r
+        if (conn != null) {\r
+            DatabaseMetaData metadata = conn.getMetaData();\r
+            // FIXME: Outputs via System.out, rather than Logger, for\r
+            // cases where this may be called during server startup.\r
+            System.out.println("username=" + metadata.getUserName());\r
+            System.out.println("database url=" + metadata.getURL());\r
+        }\r
+    }\r
 }\r