Search in sources :

Example 11 with DatabaseEnvironmentCleanupException

use of com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException in project ats-framework by Axway.

the class OracleEnvironmentHandler method restore.

/**
 * @see com.axway.ats.environment.database.model.RestoreHandler#restore(java.lang.String)
 */
public void restore(String backupFileName) throws DatabaseEnvironmentCleanupException {
    BufferedReader backupReader = null;
    Connection connection = null;
    // we need to preserve the auto commit option, as
    // the connections are pooled
    boolean isAutoCommit = true;
    try {
        log.info("Started restore of database backup from file '" + backupFileName + "'");
        backupReader = new BufferedReader(new FileReader(new File(backupFileName)));
        connection = ConnectionPool.getConnection(dbConnection);
        isAutoCommit = connection.getAutoCommit();
        connection.setAutoCommit(false);
        StringBuilder sql = new StringBuilder();
        String line = backupReader.readLine();
        List<TableConstraints> tablesConstraints = new ArrayList<>();
        while (line != null) {
            sql.append(line);
            if (line.startsWith(DROP_TABLE_MARKER)) {
                String table = line.substring(DROP_TABLE_MARKER.length()).trim();
                String owner = table.substring(0, table.indexOf("."));
                String simpleTableName = table.substring(table.indexOf(".") + 1);
                TableConstraints tbConst = dropAndRecreateTable(connection, simpleTableName, owner);
                if (tbConst != null) {
                    tablesConstraints.add(tbConst);
                }
                sql = new StringBuilder();
            }
            if (line.endsWith(EOL_MARKER)) {
                // does not require it, as opposing to any other, excluding blocks ([DECLARE]BEGIN-END;)
                if (line.contains("END;")) {
                    // in this case semicolon is mandatory
                    sql.delete(sql.length() - EOL_MARKER.length(), sql.length());
                } else {
                    sql.setLength(sql.length() - EOL_MARKER.length());
                    if (sql.toString().endsWith(";")) {
                        sql.setLength(sql.length() - 1);
                    }
                // sql.delete(sql.length() - EOL_MARKER.length() - 1, sql.length());
                }
                PreparedStatement updateStatement = connection.prepareStatement(sql.toString());
                if (log.isTraceEnabled()) {
                    log.trace("Executing SQL query: " + sql.toString());
                }
                // catch the exception and rollback, otherwise we are locked
                try {
                    updateStatement.execute();
                } catch (SQLException sqle) {
                    // we have to roll back the transaction and re throw the exception
                    connection.rollback();
                    throw new SQLException("Error invoking restore satement: " + sql.toString(), sqle);
                } finally {
                    try {
                        updateStatement.close();
                    } catch (SQLException sqle) {
                        log.error("Unable to close prepared statement", sqle);
                    }
                }
                sql = new StringBuilder();
            } else {
                // add a new line
                // FIXME: this code will add the system line ending - it
                // is not guaranteed that this was the actual line ending
                sql.append(AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
            }
            line = backupReader.readLine();
        }
        try {
            // commit the transaction
            connection.commit();
        } catch (SQLException sqle) {
            // we have to roll back the transaction and re throw the exception
            connection.rollback();
            throw sqle;
        }
        log.info("Completed restore of database backup from file '" + backupFileName + "'");
    } catch (IOException ioe) {
        throw new DatabaseEnvironmentCleanupException(ERROR_RESTORING_BACKUP + backupFileName, ioe);
    } catch (SQLException sqle) {
        throw new DatabaseEnvironmentCleanupException(ERROR_RESTORING_BACKUP + backupFileName, sqle);
    } catch (DbException dbe) {
        throw new DatabaseEnvironmentCleanupException(ERROR_RESTORING_BACKUP + backupFileName, dbe);
    } finally {
        try {
            IoUtils.closeStream(backupReader, "Could not close reader for backup file " + backupFileName);
            if (connection != null) {
                connection.setAutoCommit(isAutoCommit);
                connection.close();
            }
        } catch (SQLException sqle) {
            log.error(ERROR_RESTORING_BACKUP + backupFileName, sqle);
        }
    }
}
Also used : DatabaseEnvironmentCleanupException(com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Aggregations

DatabaseEnvironmentCleanupException (com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException)11 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)8 File (java.io.File)8 IOException (java.io.IOException)8 BufferedReader (java.io.BufferedReader)6 FileReader (java.io.FileReader)6 Connection (java.sql.Connection)6 SQLException (java.sql.SQLException)6 PreparedStatement (java.sql.PreparedStatement)5 DbQuery (com.axway.ats.common.dbaccess.DbQuery)3 ColumnDescription (com.axway.ats.core.dbaccess.ColumnDescription)3 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)3 DbTable (com.axway.ats.environment.database.model.DbTable)3 ColumnHasNoDefaultValueException (com.axway.ats.environment.database.exceptions.ColumnHasNoDefaultValueException)2 BufferedWriter (java.io.BufferedWriter)2 FileWriter (java.io.FileWriter)2 ParseException (java.text.ParseException)2 CassandraDbProvider (com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)1 PostgreSqlColumnDescription (com.axway.ats.core.dbaccess.postgresql.PostgreSqlColumnDescription)1 ResultSet (java.sql.ResultSet)1