throws RestoreException
{
Connection jdbcConn = null;
Connection jdbcConn1 = null;
Statement st = null;
RestoreException exc = null;
try
{
final DataSource ds = (DataSource)new InitialContext().lookup(dsName);
if (ds == null)
{
throw new NameNotFoundException("Data source " + dsName + " not found");
}
jdbcConn = SecurityHelper.doPrivilegedSQLExceptionAction(new PrivilegedExceptionAction<Connection>()
{
public Connection run() throws Exception
{
return ds.getConnection();
}
});
jdbcConn.setAutoCommit(false);
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 != BackupTables.DB_DIALECT_MYSQL && dialect != BackupTables.DB_DIALECT_MYSQL_UTF8
&& dialect != BackupTables.DB_DIALECT_SYBASE)
{
// resolve constraint name depends on database
String constraintName;
if (dialect == BackupTables.DB_DIALECT_DB2 || dialect == BackupTables.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();
st.execute("ALTER TABLE " + tableName + " DROP CONSTRAINT " + constraintName);
}
}
restore(storageDir, jdbcConn, tableName, restoreRule);
if (constraint != null)
{
// add constraint
st.execute("ALTER TABLE " + tableName + " ADD " + constraint);
}
}
jdbcConn.commit();
}
catch (IOException e)
{
exc = new RestoreException(e);
throw exc;
}
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() + ")" : "");
exc = new RestoreException(msg, e);
throw exc;
}
catch (NamingException e)
{
exc = new RestoreException(e);
throw exc;
}
finally
{
if (st != null)
{
try
{
st.close();
}
catch (SQLException e)
{
LOG.warn("Can't close statemnt", e);
}
}
if (jdbcConn != null)
{
try
{
jdbcConn.rollback();
}
catch (SQLException e)
{
if (exc != null)
{
throw new RestoreException(e);
}
else
{
throw new RestoreException("Can't rollback connection", exc);
}
}
finally
{
try
{
jdbcConn.close();
}
catch (SQLException e)
{
if (exc != null)
{
throw new RestoreException(e);
}
else
{
throw new RestoreException("Can't close connection", exc);
}
}
}
}
}