Author: areshetnyak
Date: 2011-07-26 07:44:20 -0400 (Tue, 26 Jul 2011)
New Revision: 4680
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/backup/rdbms/SybaseDBRestore.java
Modified:
jcr/trunk/exo.jcr.component.core/pom.xml
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/backup/rdbms/DBBackup.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/backup/rdbms/DBRestore.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCWorkspaceDataContainer.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBInitializer.java
Log:
EXOJCR-1287 : The problem with restore RDBMS backup on Sybase was fixed.
Modified: jcr/trunk/exo.jcr.component.core/pom.xml
===================================================================
--- jcr/trunk/exo.jcr.component.core/pom.xml 2011-07-26 11:42:01 UTC (rev 4679)
+++ jcr/trunk/exo.jcr.component.core/pom.xml 2011-07-26 11:44:20 UTC (rev 4680)
@@ -78,6 +78,10 @@
</dependency>
<dependency>
<groupId>org.exoplatform.core</groupId>
+ <artifactId>exo.core.component.database</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.exoplatform.core</groupId>
<artifactId>exo.core.component.security.core</artifactId>
</dependency>
<dependency>
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/backup/rdbms/DBBackup.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/backup/rdbms/DBBackup.java 2011-07-26
11:42:01 UTC (rev 4679)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/backup/rdbms/DBBackup.java 2011-07-26
11:44:20 UTC (rev 4680)
@@ -19,6 +19,7 @@
package org.exoplatform.services.jcr.impl.backup.rdbms;
import org.exoplatform.commons.utils.PrivilegedFileHelper;
+import org.exoplatform.services.database.utils.ExceptionManagementHelper;
import org.exoplatform.services.jcr.core.security.JCRRuntimePermissions;
import org.exoplatform.services.jcr.impl.Constants;
import org.exoplatform.services.jcr.impl.backup.BackupException;
@@ -112,18 +113,7 @@
}
catch (SQLException e)
{
- SQLException next = e.getNextException();
- String errorTrace = "";
- while (next != null)
- {
- errorTrace += next.getMessage() + "; ";
- next = next.getNextException();
- }
-
- Throwable cause = e.getCause();
- String msg = "SQL Exception: " + errorTrace + (cause != null ? "
(Cause: " + cause.getMessage() + ")" : "");
-
- throw new BackupException(msg, e);
+ throw new BackupException("SQL Exception: " +
ExceptionManagementHelper.getFullSQLExceptionMessage(e), e);
}
finally
{
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/backup/rdbms/DBRestore.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/backup/rdbms/DBRestore.java 2011-07-26
11:42:01 UTC (rev 4679)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/backup/rdbms/DBRestore.java 2011-07-26
11:44:20 UTC (rev 4680)
@@ -20,6 +20,7 @@
import org.exoplatform.commons.utils.PrivilegedFileHelper;
import org.exoplatform.commons.utils.PrivilegedSystemHelper;
+import org.exoplatform.services.database.utils.ExceptionManagementHelper;
import org.exoplatform.services.jcr.config.RepositoryConfigurationException;
import org.exoplatform.services.jcr.config.WorkspaceEntry;
import org.exoplatform.services.jcr.core.security.JCRRuntimePermissions;
@@ -98,7 +99,7 @@
/**
* Connection to database.
*/
- private final Connection jdbcConn;
+ protected final Connection jdbcConn;
/**
* Directory with tables dump.
@@ -108,7 +109,7 @@
/**
* Restore table rules.
*/
- private final Map<String, RestoreTableRule> tables;
+ protected final Map<String, RestoreTableRule> tables;
/**
* Database cleaner.
@@ -116,6 +117,16 @@
private final DBClean dbClean;
/**
+ * Database dialect.
+ */
+ protected final int dialect;
+
+ /**
+ * Contains constraint for JCR_SITEM or JCR_MITEM table.
+ */
+ private String constraint;
+
+ /**
* Constructor DBRestor.
*
* @throws NamingException
@@ -135,6 +146,7 @@
this.storageDir = storageDir;
this.tables = tables;
this.dbClean = DBCleanService.getDBCleaner(this.jdbcConn, wsConfig);
+ this.dialect = DialectDetecter.detect(jdbcConn.getMetaData()).hashCode();
}
/**
@@ -157,56 +169,18 @@
*/
public void restore() throws BackupException
{
- Statement st = null;
-
try
{
- int dialect = DialectDetecter.detect(jdbcConn.getMetaData()).hashCode();
-
for (Entry<String, RestoreTableRule> entry : tables.entrySet())
{
String tableName = entry.getKey();
RestoreTableRule restoreRule = entry.getValue();
- String constraint = null;
- if (tableName.equals("JCR_SITEM") ||
tableName.equals("JCR_MITEM"))
- {
- if (dialect != DBBackup.DB_DIALECT_SYBASE)
- {
- // resolve constraint name depends on database
- String constraintName;
- if (dialect == DBBackup.DB_DIALECT_DB2 || dialect ==
DBBackup.DB_DIALECT_DB2V8)
- {
- constraintName = "JCR_FK_" + (restoreRule.getDstMultiDb()
? "M" : "S") + "ITEM_PAREN";
- }
- else
- {
- constraintName = "JCR_FK_" + (restoreRule.getDstMultiDb()
? "M" : "S") + "ITEM_PARENT";
- }
- constraint =
- "CONSTRAINT " + constraintName + " FOREIGN
KEY(PARENT_ID) REFERENCES " + tableName + "(ID)";
+ preRestoreTable(tableName, restoreRule);
- // drop constraint
- st = jdbcConn.createStatement();
-
- if (dialect == DBBackup.DB_DIALECT_MYSQL || dialect ==
DBBackup.DB_DIALECT_MYSQL_UTF8)
- {
- st.execute("ALTER TABLE " + tableName + " DROP
FOREIGN KEY " + constraintName);
- }
- else
- {
- st.execute("ALTER TABLE " + tableName + " DROP
CONSTRAINT " + constraintName);
- }
- }
- }
-
restoreTable(storageDir, jdbcConn, tableName, restoreRule);
- if (constraint != null)
- {
- // add constraint
- st.execute("ALTER TABLE " + tableName + " ADD " +
constraint);
- }
+ postRestoreTable(tableName, restoreRule);
}
}
catch (IOException e)
@@ -215,21 +189,99 @@
}
catch (SQLException e)
{
- SQLException next = e.getNextException();
- String errorTrace = "";
- while (next != null)
+ throw new BackupException("SQL Exception: " +
ExceptionManagementHelper.getFullSQLExceptionMessage(e), e);
+ }
+ }
+
+ /**
+ * Prepare of restore table. (Drop constraint, etc...)
+ *
+ * @param tableName
+ * name of table
+ * @param restoreRule
+ * rule of table
+ * @throws SQLException
+ * Will throw SQLException if fail.
+ */
+ public void preRestoreTable(String tableName, RestoreTableRule restoreRule) throws
SQLException
+ {
+ Statement st = null;
+
+ try
+ {
+ if (tableName.equals("JCR_SITEM") ||
tableName.equals("JCR_MITEM"))
{
- errorTrace += next.getMessage() + "; ";
- next = next.getNextException();
+ // resolve constraint name depends on database
+ String constraintName;
+ if (dialect == DBBackup.DB_DIALECT_DB2 || dialect ==
DBBackup.DB_DIALECT_DB2V8)
+ {
+ constraintName = "JCR_FK_" + (restoreRule.getDstMultiDb() ?
"M" : "S") + "ITEM_PAREN";
+ }
+ else
+ {
+ constraintName = "JCR_FK_" + (restoreRule.getDstMultiDb() ?
"M" : "S") + "ITEM_PARENT";
+ }
+ constraint = "CONSTRAINT " + constraintName + " FOREIGN
KEY(PARENT_ID) REFERENCES " + tableName + "(ID)";
+
+ // drop constraint
+ st = jdbcConn.createStatement();
+
+ if (dialect == DBBackup.DB_DIALECT_MYSQL || dialect ==
DBBackup.DB_DIALECT_MYSQL_UTF8)
+ {
+ st.execute("ALTER TABLE " + tableName + " DROP FOREIGN KEY
" + constraintName);
+ }
+ else
+ {
+ st.execute("ALTER TABLE " + tableName + " DROP CONSTRAINT
" + constraintName);
+ }
}
+ }
+ finally
+ {
+ if (st != null)
+ {
+ try
+ {
+ st.close();
+ }
+ catch (SQLException e)
+ {
+ LOG.warn("Can't close statemnt", e);
+ }
+ }
+ }
+ }
- Throwable cause = e.getCause();
- String msg = "SQL Exception: " + errorTrace + (cause != null ? "
(Cause: " + cause.getMessage() + ")" : "");
+ /**
+ * After of restore table. (Add constraint, etc...)
+ *
+ * @param tableName
+ * name of table
+ * @param restoreRule
+ * rule of table
+ * @throws SQLException
+ * Will throw SQLException if fail.
+ */
+ public void postRestoreTable(String tableName, RestoreTableRule restoreRule) throws
SQLException
+ {
+ Statement st = null;
- throw new BackupException(msg, e);
+ try
+ {
+ if (tableName.equals("JCR_SITEM") ||
tableName.equals("JCR_MITEM"))
+ {
+ if (constraint != null)
+ {
+ // add constraint
+ st = jdbcConn.createStatement();
+ st.execute("ALTER TABLE " + tableName + " ADD " +
constraint);
+ }
+ }
}
finally
{
+ constraint = null;
+
if (st != null)
{
try
@@ -312,8 +364,6 @@
PreparedStatement insertNode = null;
ResultSet tableMetaData = null;
- int dialect = DialectDetecter.detect(jdbcConn.getMetaData()).hashCode();
-
// switch table name to lower case
if (dialect == DBBackup.DB_DIALECT_PGSQL)
{
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/backup/rdbms/SybaseDBRestore.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/backup/rdbms/SybaseDBRestore.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/backup/rdbms/SybaseDBRestore.java 2011-07-26
11:44:20 UTC (rev 4680)
@@ -0,0 +1,189 @@
+/*
+ * Copyright (C) 2003-2011 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not,
see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.services.jcr.impl.backup.rdbms;
+
+import org.exoplatform.services.database.utils.ExceptionManagementHelper;
+import org.exoplatform.services.jcr.config.RepositoryConfigurationException;
+import org.exoplatform.services.jcr.config.WorkspaceEntry;
+import org.exoplatform.services.jcr.impl.backup.BackupException;
+import org.exoplatform.services.jcr.impl.util.io.FileCleaner;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.naming.NamingException;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>Date: 2011
+ *
+ * @author <a href="mailto:alex.reshetnyak@exoplatform.com.ua">Alex
Reshetnyak</a>
+ * @version $Id: SybaseDBRestore.java 111 2011-11-11 11:11:11Z rainf0x $
+ */
+public class SybaseDBRestore
+ extends DBRestore
+{
+ private String restoreConstraint = null;
+
+ public SybaseDBRestore(File storageDir, Connection jdbcConn, Map<String,
RestoreTableRule> tables,
+ WorkspaceEntry wsConfig, FileCleaner fileCleaner) throws NamingException,
SQLException,
+ RepositoryConfigurationException
+ {
+ super(storageDir, jdbcConn, tables, wsConfig, fileCleaner);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void clean() throws BackupException
+ {
+
+ try
+ {
+ // the Sybase is not allowed DDL query (CREATE TABLE, DROP TABLE, etc. ) within
a multi-statement transaction
+ jdbcConn.setAutoCommit(true);
+
+ for (Entry<String, RestoreTableRule> entry : tables.entrySet())
+ {
+ String tableName = entry.getKey();
+ RestoreTableRule restoreRule = entry.getValue();
+
+ super.preRestoreTable(tableName, restoreRule);
+ }
+ }
+ catch (SQLException e)
+ {
+ throw new
BackupException(ExceptionManagementHelper.getFullSQLExceptionMessage(e), e);
+ }
+ finally
+ {
+ try
+ {
+ jdbcConn.setAutoCommit(false);
+ }
+ catch (SQLException e)
+ {
+ LOG.warn("Can't set auto commit to \"false\"", e);
+ }
+ }
+
+ super.clean();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void commit() throws BackupException
+ {
+ super.commit();
+
+ restoreConstraint();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void rollback() throws BackupException
+ {
+ BackupException rollbackException = null;
+
+ try
+ {
+ super.rollback();
+ }
+ catch (BackupException e)
+ {
+ rollbackException = e;
+ throw rollbackException;
+ }
+ finally
+ {
+ try
+ {
+ restoreConstraint();
+ }
+ catch (BackupException e)
+ {
+ if (rollbackException != null)
+ {
+ LOG.error("Can not restore constraint", e);
+ throw rollbackException;
+ }
+ else
+ {
+ throw e;
+ }
+ }
+ }
+ }
+
+ /**
+ * Restore constraint.
+ *
+ * @throws BackupException
+ * Will throw BackupException if fail.
+ */
+ private void restoreConstraint() throws BackupException
+ {
+ try
+ {
+ // restore constraint
+ jdbcConn.setAutoCommit(true);
+
+ for (Entry<String, RestoreTableRule> entry : tables.entrySet())
+ {
+ String tableName = entry.getKey();
+ RestoreTableRule restoreRule = entry.getValue();
+
+ super.postRestoreTable(tableName, restoreRule);
+ }
+ }
+ catch (SQLException e)
+ {
+ throw new BackupException(e);
+ }
+ finally
+ {
+ try
+ {
+ jdbcConn.setAutoCommit(false);
+ }
+ catch (SQLException e)
+ {
+ LOG.warn("Can't set auto commit to \"false\"", e);
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void preRestoreTable(String tableName, RestoreTableRule restoreRule) throws
SQLException
+ {
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void postRestoreTable(String tableName, RestoreTableRule restoreRule) throws
SQLException
+ {
+ }
+}
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCWorkspaceDataContainer.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCWorkspaceDataContainer.java 2011-07-26
11:42:01 UTC (rev 4679)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCWorkspaceDataContainer.java 2011-07-26
11:44:20 UTC (rev 4680)
@@ -36,6 +36,7 @@
import org.exoplatform.services.jcr.impl.backup.rdbms.DBRestore;
import org.exoplatform.services.jcr.impl.backup.rdbms.DirectoryRestore;
import org.exoplatform.services.jcr.impl.backup.rdbms.RestoreTableRule;
+import org.exoplatform.services.jcr.impl.backup.rdbms.SybaseDBRestore;
import org.exoplatform.services.jcr.impl.clean.rdbms.DBCleanService;
import org.exoplatform.services.jcr.impl.core.query.NodeDataIndexingIterator;
import org.exoplatform.services.jcr.impl.core.query.Reindexable;
@@ -1083,7 +1084,7 @@
if (multiDb)
{
- scripts.put("JCR_MITEM", "select * from JCR_MITEM where
JCR_MITEM.name <> '" + Constants.ROOT_PARENT_NAME
+ scripts.put("JCR_MITEM", "select * from JCR_MITEM where
JCR_MITEM.NAME <> '" + Constants.ROOT_PARENT_NAME
+ "'");
scripts.put("JCR_MVALUE", "select * from JCR_MVALUE");
scripts.put("JCR_MREF", "select * from JCR_MREF");
@@ -1272,7 +1273,14 @@
}
tables.put(dstTableName, restoreTableRule);
- restorers.add(new DBRestore(storageDir, jdbcConn, tables, wsConfig,
swapCleaner));
+ if (dbDialect == DBConstants.DB_DIALECT_SYBASE)
+ {
+ restorers.add(new SybaseDBRestore(storageDir, jdbcConn, tables, wsConfig,
swapCleaner));
+ }
+ else
+ {
+ restorers.add(new DBRestore(storageDir, jdbcConn, tables, wsConfig,
swapCleaner));
+ }
// prepare value storage restorer
File backupValueStorageDir = new File(storageDir, "values");
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBInitializer.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBInitializer.java 2011-07-26
11:42:01 UTC (rev 4679)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/jdbc/DBInitializer.java 2011-07-26
11:44:20 UTC (rev 4680)
@@ -19,6 +19,7 @@
package org.exoplatform.services.jcr.impl.util.jdbc;
import org.exoplatform.commons.utils.SecurityHelper;
+import org.exoplatform.services.database.utils.ExceptionManagementHelper;
import org.exoplatform.services.jcr.impl.storage.jdbc.JDBCUtils;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
@@ -409,18 +410,9 @@
}
else
{
- SQLException next = e.getNextException();
- String errorTrace = "";
- while (next != null)
- {
- errorTrace += next.getMessage() + "; ";
- next = next.getNextException();
- }
- Throwable cause = e.getCause();
String msg =
- "Could not create db schema of DataSource: '" +
containerName + "'. Reason: " + e.getMessage() + "; "
- + errorTrace + (cause != null ? " (Cause: " +
cause.getMessage() + ")" : "") + ". Last command: "
- + sql;
+ "Could not create db schema of DataSource: '" +
containerName + "'. Reason: " + e.getMessage() + "; "
+ + ExceptionManagementHelper.getFullSQLExceptionMessage(e) +
". Last command: " + sql;
throw new DBInitializerException(msg, e);
}