Search in sources :

Example 1 with DatabaseEnvironmentCleanupException

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

the class PostgreSqlEnvironmentHandler method restore.

public void restore(String backupFileName) throws DatabaseEnvironmentCleanupException {
    BufferedReader backupReader = null;
    Connection connection = null;
    // used to preserve the initial 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();
        while (line != null) {
            if (line.startsWith("--")) {
                LOG.debug("Skipping commented line: " + line);
            } else {
                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);
                        dropAndRecreateTable(connection, simpleTableName, owner);
                    
                    }*/
                if (line.endsWith(EOL_MARKER)) {
                    // remove the EOL marker
                    sql.delete(sql.length() - EOL_MARKER.length(), sql.length());
                    PreparedStatement updateStatement = connection.prepareStatement(sql.toString());
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("About to execute restore SQL statement: " + sql.toString());
                    }
                    // catch the exception and roll back, 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 statement: " + sql.toString(), sqle);
                    } finally {
                        try {
                            updateStatement.close();
                        } catch (SQLException sqle) {
                            LOG.error("Unable to close prepared statement", sqle);
                        }
                    }
                    sql.delete(0, sql.length());
                } else {
                    // Add a new line.  Note: this code will add the current system 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 | DbException ex) {
        throw new DatabaseEnvironmentCleanupException("Could not restore backup from file " + backupFileName, ex);
    } catch (SQLException ex) {
        throw new DatabaseEnvironmentCleanupException("Could not restore backup from file " + backupFileName + ".\n Details of full SQL exception follow: " + DbUtils.getFullSqlException("SQLException", ex));
    } 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("Could not reset autocommit state and close DB connection", sqle);
        }
    }
}
Also used : DatabaseEnvironmentCleanupException(com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) 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)

Example 2 with DatabaseEnvironmentCleanupException

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

the class MssqlEnvironmentHandler 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();
        while (line != null) {
            sql.append(line);
            if (line.startsWith(DROP_TABLE_MARKER)) {
                String tableName = line.substring(DROP_TABLE_MARKER.length()).trim();
                dropAndRecreateTable(connection, tableName);
                // clear the sql query buffer
                sql = new StringBuilder();
            } else if (line.endsWith(EOL_MARKER)) {
                // remove the EOL marker
                sql.delete(sql.length() - EOL_MARKER.length(), sql.length());
                PreparedStatement updateStatement = connection.prepareStatement(sql.toString());
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Executing SQL query: " + sql);
                }
                // 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 executing restore satement: " + sql.toString(), sqle);
                } finally {
                    DbUtils.closeStatement(updateStatement);
                }
                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) BufferedReader(java.io.BufferedReader) Connection(java.sql.Connection) FileReader(java.io.FileReader) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) File(java.io.File) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 3 with DatabaseEnvironmentCleanupException

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

the class OracleEnvironmentHandler method createBackup.

/**
 * @see com.axway.ats.environment.database.model.BackupHandler#createBackup(java.lang.String)
 */
@Override
public void createBackup(String backupFileName) throws DatabaseEnvironmentCleanupException {
    super.createBackup(backupFileName);
    // In order to add and enable the foreign keys, that are related to the tables for backup
    // all of the columns, referenced in those foreign keys must have values in the referenced table
    // That's why we insert records into all of the tables, added to backup,
    // and then add and enable all of the foreign keys
    BufferedWriter fileWriter = null;
    try {
        fileWriter = new BufferedWriter(new FileWriter(new File(backupFileName), true));
        // create all of the foreign keys
        for (TableConstraints tbConst : tablesConstraints) {
            for (String fkQuery : tbConst.foreignKeyStatements) {
                fileWriter.append(fkQuery + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
            }
        }
        // enable the foreign keys
        for (TableConstraints tbConst : tablesConstraints) {
            for (String fkEnableQuery : tbConst.enableForeignKeyConstraintStatements) {
                fileWriter.append(fkEnableQuery + EOL_MARKER + AtsSystemProperties.SYSTEM_LINE_SEPARATOR);
            }
        }
        log.info("Completed backup of foreign keys in '" + backupFileName + "'");
    } catch (Exception pe) {
        markBackupFileAsDamaged(fileWriter, backupFileName);
        throw new DatabaseEnvironmentCleanupException(ERROR_CREATING_BACKUP + backupFileName, pe);
    } finally {
        IoUtils.closeStream(fileWriter, ERROR_CREATING_BACKUP + backupFileName);
    }
}
Also used : DatabaseEnvironmentCleanupException(com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException) FileWriter(java.io.FileWriter) File(java.io.File) SQLException(java.sql.SQLException) ColumnHasNoDefaultValueException(com.axway.ats.environment.database.exceptions.ColumnHasNoDefaultValueException) ParseException(java.text.ParseException) DatabaseEnvironmentCleanupException(com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException) IOException(java.io.IOException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) BufferedWriter(java.io.BufferedWriter)

Example 4 with DatabaseEnvironmentCleanupException

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

the class AbstractEnvironmentHandler method createBackup.

/**
 * Create the database backup for the selected tables
 *
 * @param backupFileName                        the name of the backup file
 * @throws DatabaseEnvironmentCleanupException  if the backup file cannot be created
 * @see com.axway.ats.environment.database.model.BackupHandler#createBackup(java.lang.String)
 */
public void createBackup(String backupFileName) throws DatabaseEnvironmentCleanupException {
    // reset flag, so delete statements will be inserted
    this.deleteStatementsInserted = false;
    BufferedWriter fileWriter = null;
    try {
        fileWriter = new BufferedWriter(new FileWriter(new File(backupFileName)));
        log.info("Started creation of database backup in file '" + backupFileName + "'");
        writeBackupToFile(fileWriter);
        log.info("Completed creation of database backup in file '" + backupFileName + "'");
    } catch (Exception pe) {
        markBackupFileAsDamaged(fileWriter, backupFileName);
        throw new DatabaseEnvironmentCleanupException(ERROR_CREATING_BACKUP + backupFileName, pe);
    } finally {
        IoUtils.closeStream(fileWriter, ERROR_CREATING_BACKUP + backupFileName);
    }
}
Also used : DatabaseEnvironmentCleanupException(com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException) FileWriter(java.io.FileWriter) File(java.io.File) ColumnHasNoDefaultValueException(com.axway.ats.environment.database.exceptions.ColumnHasNoDefaultValueException) ParseException(java.text.ParseException) DatabaseEnvironmentCleanupException(com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException) IOException(java.io.IOException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) BufferedWriter(java.io.BufferedWriter)

Example 5 with DatabaseEnvironmentCleanupException

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

the class MariaDbEnvironmentHandler method restore.

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();
        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);
                dropAndRecreateTable(connection, simpleTableName, owner);
            }
            if (line.endsWith(EOL_MARKER)) {
                PreparedStatement updateStatement = null;
                // remove the OEL marker
                sql.delete(sql.length() - EOL_MARKER.length(), sql.length());
                if (sql.toString().trim().startsWith("INSERT INTO")) {
                    // This line escapes non-printable string chars. Hex data is already escaped as 0xABC without backslash(\)
                    String insertQuery = sql.toString().replace("\\0x", "\\");
                    updateStatement = connection.prepareStatement(insertQuery);
                } else {
                    updateStatement = connection.prepareStatement(sql.toString());
                }
                // catch the exception and roll back, 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.delete(0, sql.length());
            } 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("Could not restore backup from file " + backupFileName, ioe);
    } catch (SQLException sqle) {
        throw new DatabaseEnvironmentCleanupException("Could not restore backup from file " + backupFileName, sqle);
    } catch (DbException dbe) {
        throw new DatabaseEnvironmentCleanupException("Could not restore backup from file " + 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("Could close DB connection");
        }
    }
}
Also used : DatabaseEnvironmentCleanupException(com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException) SQLException(java.sql.SQLException) BufferedReader(java.io.BufferedReader) Connection(java.sql.Connection) FileReader(java.io.FileReader) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) File(java.io.File) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

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