You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by jo...@apache.org on 2014/01/28 20:57:11 UTC

svn commit: r1562195 - /cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migrator.java

Author: johnthuss
Date: Tue Jan 28 19:57:11 2014
New Revision: 1562195

URL: http://svn.apache.org/r1562195
Log:
Smarter database locking; Add logging for better clarity

Modified:
    cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migrator.java

Modified: cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migrator.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migrator.java?rev=1562195&r1=1562194&r2=1562195&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migrator.java (original)
+++ cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migrator.java Tue Jan 28 19:57:11 2014
@@ -70,8 +70,20 @@ public class Migrator {
 	}
 	
 	int currentDbVersion(DataMap map) throws SQLException {
-		String sql = String.format("SELECT version FROM %s WHERE dataMap = '%s'", migrationTableName(), map.getName());
-        return executeSqlReturnInt(sql).intValue();
+	    String sql = String.format("SELECT version FROM %s WHERE dataMap = '%s'", migrationTableName(), map.getName());
+	    Integer version = null;
+	    try {
+	        version = executeSqlReturnInt(sql);
+	        return version != null ? version.intValue() : -1;
+	    } catch (Exception e) {
+	        try {
+	            createInternalMigrationSchema();
+	        } catch (Exception e2) {
+	            node.getJdbcEventLogger().log(e.getMessage());
+	        }
+	        version = executeSqlReturnInt(sql);
+	        return version != null ? version.intValue() : -1;
+	    }
 	}
 	
 	void setDbVersion(DataMap map, int version) throws SQLException {
@@ -85,13 +97,9 @@ public class Migrator {
 		String sql = String.format("UPDATE %s SET locked = 1 WHERE locked=0 and dataMap='%s'", migrationTableName(), map.getName());
 		int count = 0;
 		try {
-			try {
-				count = executeSqlWithUpdateCount(sql);
-				if (count > 0) {
-					return true; // got the lock
-				}
-			} catch (Exception e) {
-				createInternalMigrationSchema();
+			count = executeSqlWithUpdateCount(sql);
+			if (count > 0) {
+				return true; // got the lock
 			}
 		
 	        sql = String.format("SELECT locked FROM %s WHERE dataMap='%s'", migrationTableName(), map.getName());
@@ -109,8 +117,12 @@ public class Migrator {
 		}
 	}
 	
+	String unlockSql(DataMap map) {
+	    return String.format("UPDATE %s SET locked = 0 WHERE locked = 1 AND dataMap = '%s'", migrationTableName(), map.getName());
+	}
+	
 	void unlock(DataMap map) throws SQLException {
-		int count = executeSqlWithUpdateCount(String.format("UPDATE %s SET locked = 0 WHERE locked = 1 AND dataMap = '%s'", migrationTableName(), map.getName()));
+		int count = executeSqlWithUpdateCount(unlockSql(map));
 		if (count == 0) {
 			throw new IllegalStateException("Unable to remove migration lock.");
 		}
@@ -145,35 +157,42 @@ public class Migrator {
 
 				for (DataMap map : node.getDataMaps()) {
 					
-					while (!lock(map)) {
-						//log.debug("Waiting to obtain migration lock for node: " + node.getName());
-						try {
-							Thread.sleep(200);
-						} catch (InterruptedException e) {
-							return;
-						}
-					}
+                    int version = currentDbVersion(map)+1;
+
+                    Migration migration = createMigrationClassForVersion(map, version);
+                    if (migration != null) {
+                        while (!lock(map)) {
+                            node.getJdbcEventLogger().log("Waiting to obtain migration lock for node: " + node.getName() + ". " +
+                                    "If you terminated the application while a migration was in progress " +
+                                    "you will need to clear the migration lock by running: " +
+                                    unlockSql(map));
+                            try {
+                                Thread.sleep(2000);
+                            } catch (InterruptedException e) {
+                                return;
+                            }
+                        }
 					
-					try {
-						int version = currentDbVersion(map)+1;
-						
-						Migration migration;
-						while ((migration = createMigrationClassForVersion(map, version)) != null) {
-							//log.info(String.format("Updating %s to version %d", map.getName(), version));
-						    migration.getDatabase().setDatabaseProductName(getConnection().getMetaData().getDatabaseProductName());
-							migration.run();
-							try {
-							    executeOperations(migration.getDatabase().getOperations());
-							} catch (Exception e) {
-							    throw new RuntimeException("Failed to migrate node=" + node.getName() + ", dataMap=" + map.getName() + " to version=" + version + ": " + e.getMessage(), e);
-							}
-							setDbVersion(map, version);
-							getConnection().commit();
-							version++;
-						}
-					} finally {
-						unlock(map);
-					}
+                        version = currentDbVersion(map)+1;
+                        
+    					try {
+    						while ((migration = createMigrationClassForVersion(map, version)) != null) {
+    						    node.getJdbcEventLogger().log(String.format("Updating dataMap '%s' to version %d", map.getName(), version));
+    						    migration.getDatabase().setDatabaseProductName(getConnection().getMetaData().getDatabaseProductName());
+    							migration.run();
+    							try {
+    							    executeOperations(migration.getDatabase().getOperations());
+    							} catch (Exception e) {
+    							    throw new RuntimeException("Failed to migrate node=" + node.getName() + ", dataMap=" + map.getName() + " to version=" + version + ": " + e.getMessage());
+    							}
+    							setDbVersion(map, version);
+    							getConnection().commit();
+    							version++;
+    						}
+    					} finally {
+    						unlock(map);
+    					}
+                    }
 				}
 				
 			} finally {