[exo-jcr-commits] exo-jcr SVN: r3716 - in jcr/trunk/exo.jcr.component.ext/src: test/java/org/exoplatform/services/jcr/ext/backup/load and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Dec 23 09:52:07 EST 2010


Author: tolusha
Date: 2010-12-23 09:52:06 -0500 (Thu, 23 Dec 2010)
New Revision: 3716

Modified:
   jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/backup/impl/rdbms/FullBackupJob.java
   jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/services/jcr/ext/backup/load/TestLoadBackup.java
Log:
EXOJCR-1078: lock tables before backup doing

Modified: jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/backup/impl/rdbms/FullBackupJob.java
===================================================================
--- jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/backup/impl/rdbms/FullBackupJob.java	2010-12-23 10:25:41 UTC (rev 3715)
+++ jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/backup/impl/rdbms/FullBackupJob.java	2010-12-23 14:52:06 UTC (rev 3716)
@@ -49,6 +49,7 @@
 import java.net.URL;
 import java.security.PrivilegedExceptionAction;
 import java.sql.Connection;
+import java.sql.DatabaseMetaData;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
@@ -178,8 +179,6 @@
       notifyListeners();
 
       Connection jdbcConn = null;
-      Statement st = null;
-
       try
       {
          WorkspaceEntry workspaceEntry = null;
@@ -227,8 +226,6 @@
             }
          });
          
-         int dialect = DialectDetecter.detect(jdbcConn.getMetaData()).hashCode();
-
          RDBMSBackupInfoWriter backupInfoWriter = new RDBMSBackupInfoWriter(getStorageURL().getFile());
 
          backupInfoWriter.setRepositoryName(repository.getConfiguration().getName());
@@ -263,13 +260,43 @@
          backupInfoWriter.setValueTableName(scripts[1][0]);
          backupInfoWriter.setRefTableName(scripts[2][0]);
 
-         // Lock db
-         if (dialect != DB_DIALECT_HSQLDB)
+         // Lock tables
+         ResultSet rs = null;
+         Statement st = null;
+         try
          {
+            DatabaseMetaData metaData = jdbcConn.getMetaData();
+
+            rs = metaData.getTables(null, null, "%", new String[]{"TABLE"});
             st = jdbcConn.createStatement();
-            st.execute("LOCK TABLES " + scripts[0][0] + " WRITE");
+            int dialect = DialectDetecter.detect(metaData).hashCode();
+
+            while (rs.next())
+            {
+               String tableName = rs.getString("TABLE_NAME");
+               if (dialect == DB_DIALECT_HSQLDB)
+               {
+                  st.execute("SET TABLE " + tableName + " READONLY TRUE");
+               }
+               else
+               {
+                  st.execute("LOCK TABLES " + tableName + " WRITE");
+               }
+            }
          }
+         finally
+         {
+            if (rs != null)
+            {
+               rs.close();
+            }
 
+            if (st != null)
+            {
+               st.close();
+            }
+         }
+
          for (String script[] : scripts)
          {
             dumpTable(jdbcConn, script[0], script[1]);
@@ -326,33 +353,46 @@
       }
       finally
       {
-         if (st != null)
+         if (jdbcConn != null)
          {
+            // unlock tables
             try
             {
-               st.execute("UNLOCK TABLES");
-            }
-            catch (SQLException e)
-            {
-               log.error("Full backup failed " + getStorageURL().getPath(), e);
-               notifyError("Full backup failed", e);
-            }
+               ResultSet rs = null;
+               Statement st = null;
+               try
+               {
+                  DatabaseMetaData metaData = jdbcConn.getMetaData();
+                  st = jdbcConn.createStatement();
+                  int dialect = DialectDetecter.detect(metaData).hashCode();
 
-            try
-            {
-               st.close();
-            }
-            catch (SQLException e)
-            {
-               log.error("Full backup failed " + getStorageURL().getPath(), e);
-               notifyError("Full backup failed", e);
-            }
-         }
+                  if (dialect == DB_DIALECT_HSQLDB)
+                  {
+                     rs = metaData.getTables(null, null, "%", new String[]{"TABLE"});
+                     while (rs.next())
+                     {
+                        String tableName = rs.getString("TABLE_NAME");
+                        st.execute("SET TABLE " + tableName + " READONLY FALSE");
+                     }
+                  }
+                  else
+                  {
+                     st.execute("UNLOCK TABLES");
+                  }
+               }
+               finally
+               {
+                  if (rs != null)
+                  {
+                     rs.close();
+                  }
 
-         if (jdbcConn != null)
-         {
-            try
-            {
+                  if (st != null)
+                  {
+                     st.close();
+                  }
+               }
+
                jdbcConn.close();
             }
             catch (SQLException e)

Modified: jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/services/jcr/ext/backup/load/TestLoadBackup.java
===================================================================
--- jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/services/jcr/ext/backup/load/TestLoadBackup.java	2010-12-23 10:25:41 UTC (rev 3715)
+++ jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/services/jcr/ext/backup/load/TestLoadBackup.java	2010-12-23 14:52:06 UTC (rev 3716)
@@ -34,6 +34,8 @@
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.sql.Connection;
+import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -44,6 +46,8 @@
 import javax.jcr.Value;
 import javax.jcr.version.VersionException;
 import javax.jcr.version.VersionHistory;
+import javax.naming.InitialContext;
+import javax.sql.DataSource;
 
 /**
  * Created by The eXo Platform SAS.
@@ -65,8 +69,6 @@
 
    protected final int BACKUP_TYPE = BackupManager.FULL_BACKUP_ONLY;
 
-   protected final boolean RESTORE_INTO_EXISTED = false;
-
    protected ManageableRepository repository;
 
    /**
@@ -99,7 +101,6 @@
          }
          catch (Exception e)
          {
-            e.printStackTrace();
          }
       }
    }
@@ -126,7 +127,7 @@
          sessions.add(writerSession);
       }
 
-      Thread.sleep(5 * 1000);
+      Thread.sleep(10 * 1000);
 
       System.out.println(" ============ BACKUP START ============");
 
@@ -159,9 +160,19 @@
 
       if (BACKUP_TYPE == BackupManager.FULL_AND_INCREMENTAL)
       {
-         Thread.sleep(30 * 1000);
+         Thread.sleep(5 * 1000);
       }
 
+      for (Thread thread : threads)
+      {
+         thread.interrupt();
+      }
+
+      if (BACKUP_TYPE == BackupManager.FULL_AND_INCREMENTAL)
+      {
+         Thread.sleep(5 * 1000);
+      }
+
       // stop backup
       if (bch != null)
       {
@@ -175,11 +186,6 @@
 
       System.out.println(" ============ BACKUP FINISHED ============");
 
-      //      for (Thread thread : threads)
-      //      {
-      //         thread.interrupt();
-      //      }
-
       // restore
       WorkspaceEntry ws1back = makeWorkspaceEntry("ws1back", "jdbcjcr3");
 
@@ -217,7 +223,6 @@
       {
          Node n = root.addNode("testNode" + i);
          n.addMixin("mix:referenceable");
-         n.addMixin("mix:versionable");
          n.setProperty("long", i);
          n.setProperty("ref", n);
          n.setProperty("string", new String[]{"test" + System.currentTimeMillis()});
@@ -225,8 +230,7 @@
 
          session.save();
 
-         n.checkin();
-         n.checkout();
+         //         System.out.println("Child has been added");
 
          Thread.sleep(50);
 
@@ -301,7 +305,35 @@
          }
       }
    }
+   
+   public void _testTableLock() throws Exception
+   {
+      Session writerSession = repository.login(credentials, WORKSPACE_NAME);
+      //      TreeWriterThread writer = new TreeWriterThread(writerSession, "subnode");
+      //      writer.start();
 
+      DataSource ds = (DataSource)new InitialContext().lookup("jdbcjcr_to_repository_restore_singel_db");
+      Connection conn = ds.getConnection();
+
+      Thread.sleep(2 * 1000);
+
+      Statement st = conn.createStatement();
+      st.executeQuery("SET PROPERTY \"readonly\" TRUE");
+      System.out.println("LOCK TABLES JCR_SITEM WRITE");
+      Thread.sleep(5 * 1000);
+
+      //      st.executeQuery("SET READONLY FALSE");
+      //      System.out.println("UNLOCK TABLES");
+      //      Thread.sleep(5 * 1000);
+
+      st.close();
+      conn.close();
+      System.out.println("Con closed");
+      Thread.sleep(5 * 1000);
+
+      addChilds(writerSession, writerSession.getRootNode(), 0);
+   }
+
    /**
     * {@inheritDoc}
     */



More information about the exo-jcr-commits mailing list