You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2007/06/29 13:09:49 UTC

svn commit: r551852 - in /cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access: DbGenerator.java DbGeneratorPostprocessor.java

Author: aadamchik
Date: Fri Jun 29 04:09:48 2007
New Revision: 551852

URL: http://svn.apache.org/viewvc?view=rev&rev=551852
Log:
CAY-817 HSQLDB schema generation may not correctly flush the changes

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGeneratorPostprocessor.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGenerator.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGenerator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGenerator.java?view=diff&rev=551852&r1=551851&r2=551852
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGenerator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGenerator.java Fri Jun 29 04:09:48 2007
@@ -17,7 +17,6 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.access;
 
 import java.sql.Connection;
@@ -303,8 +302,7 @@
             }
 
             // create FK
-            if (shouldCreateTables
-                    && shouldCreateFKConstraints) {
+            if (shouldCreateTables && shouldCreateFKConstraints) {
                 Iterator it = dbEntitiesInInsertOrder.iterator();
                 while (it.hasNext()) {
                     DbEntity ent = (DbEntity) it.next();
@@ -339,6 +337,8 @@
                     safeExecute(connection, (String) it.next());
                 }
             }
+
+            new DbGeneratorPostprocessor().execute(connection);
         }
         finally {
             connection.close();
@@ -383,7 +383,7 @@
     public List createFkConstraintsQueries(DbEntity table) {
         return createConstraintsQueries(table);
     }
-    
+
     /**
      * Creates FK and UNIQUE constraint statements for a given table.
      * 

Added: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGeneratorPostprocessor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGeneratorPostprocessor.java?view=auto&rev=551852
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGeneratorPostprocessor.java (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGeneratorPostprocessor.java Fri Jun 29 04:09:48 2007
@@ -0,0 +1,78 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.access;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.cayenne.dba.AutoAdapter;
+import org.apache.cayenne.dba.DbAdapter;
+import org.apache.cayenne.dba.hsqldb.HSQLDBAdapter;
+
+/**
+ * A helper class that handles postprocessing after the schema generation operation. E.g.
+ * some databases require a checkpoint command to be run for the schema changes to be
+ * flushed to disk.
+ * 
+ * @author Andrus Adamchik
+ */
+class DbGeneratorPostprocessor {
+
+    private static final Map postprocessors;
+
+    static {
+        postprocessors = new HashMap();
+        postprocessors.put(HSQLDBAdapter.class.getName(), new HSQLDBPostprocessor());
+    }
+
+    void execute(Connection connection) throws SQLException {
+
+        DbAdapter adapter = AutoAdapter.getDefaultFactory().createAdapter(
+                connection.getMetaData());
+        if (adapter != null) {
+            Postprocessor postprocessor = (Postprocessor) postprocessors.get(adapter
+                    .getClass()
+                    .getName());
+            if (postprocessor != null) {
+                postprocessor.execute(connection);
+            }
+        }
+    }
+
+    static abstract class Postprocessor {
+
+        abstract void execute(Connection c) throws SQLException;
+    }
+
+    static class HSQLDBPostprocessor extends Postprocessor {
+
+        void execute(Connection c) throws SQLException {
+            PreparedStatement st = c.prepareStatement("CHECKPOINT");
+            try {
+                st.execute();
+            }
+            finally {
+                st.close();
+            }
+        }
+    }
+}