Search in sources :

Example 26 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class PackageLoader method loadPackageFromDb.

private static InputStream loadPackageFromDb(int packageId, String messagesHost, String messagesDB, String messagestable, String messagesUser, String messagesPassword) throws PackageException {
    DbConnMySQL dbConnection = new DbConnMySQL(messagesHost, messagesDB, messagesUser, messagesPassword);
    MysqlDbProvider dbProvider = new MysqlDbProvider(dbConnection);
    try {
        InputStream packageContent = dbProvider.selectValue(messagestable, "message_id", Integer.toString(packageId), "data");
        log.info("Successfully extracted package with id '" + packageId + "' from '" + messagestable + "' DB");
        return packageContent;
    } catch (DbRecordsException dbre) {
        throw new PackageException("Package with id '" + packageId + "' does not exist in 'messages' DB");
    } catch (DbException dbe) {
        throw new PackageException("Could not get package with id '" + packageId + "' from the 'messages' DB");
    }
}
Also used : MysqlDbProvider(com.axway.ats.core.dbaccess.mysql.MysqlDbProvider) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DbConnMySQL(com.axway.ats.core.dbaccess.mysql.DbConnMySQL) PackageException(com.axway.ats.action.objects.model.PackageException) DbRecordsException(com.axway.ats.core.dbaccess.exceptions.DbRecordsException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 27 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class CassandraEnvironmentHandler method restore.

public void restore(String backupFileName) throws DatabaseEnvironmentCleanupException {
    BufferedReader backupReader = null;
    try {
        log.debug("Starting restoring db backup from file '" + backupFileName + "'");
        backupReader = new BufferedReader(new FileReader(new File(backupFileName)));
        StringBuilder sql = new StringBuilder();
        String line = backupReader.readLine();
        while (line != null) {
            sql.append(line);
            if (line.endsWith(EOL_MARKER)) {
                // remove the EOL marker
                sql.delete(sql.length() - EOL_MARKER.length(), sql.length());
                dbProvider.executeUpdate(sql.toString());
                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();
        }
        log.debug("Finished restoring db backup from file '" + backupFileName + "'");
    } catch (IOException ioe) {
        throw new DatabaseEnvironmentCleanupException(ERROR_RESTORING_BACKUP + backupFileName, ioe);
    } catch (DbException dbe) {
        throw new DatabaseEnvironmentCleanupException(ERROR_RESTORING_BACKUP + backupFileName, dbe);
    } finally {
        try {
            if (backupReader != null) {
                backupReader.close();
            }
        } catch (IOException ioe) {
            log.error(ERROR_RESTORING_BACKUP + backupFileName, ioe);
        }
        ((CassandraDbProvider) dbProvider).disconnect();
    }
}
Also used : DatabaseEnvironmentCleanupException(com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) CassandraDbProvider(com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)

Example 28 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException 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.debug("Starting restoring db 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.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.delete(sql.length() - EOL_MARKER.length() - 1, sql.length());
                }
                PreparedStatement updateStatement = connection.prepareStatement(sql.toString());
                //catch the exception and rollback, otherwise we are locked
                try {
                    updateStatement.execute();
                } catch (SQLException sqle) {
                    log.error("Error invoking restore satement: " + sql.toString());
                    //we have to roll back the transaction and re throw the exception
                    connection.rollback();
                    throw 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(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.debug("Finished restoring db 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 29 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class Test_OracleEnvironmentHandler method createBackupNegativeNoColumns.

@Test()
public void createBackupNegativeNoColumns() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException {
    DbTable table1 = new DbTable("table1");
    DbTable table2 = new DbTable("table2");
    OracleEnvironmentHandler envHandler = new OracleEnvironmentHandler(mockDbConnection, mockDbProvider);
    envHandler.addTable(table1);
    envHandler.addTable(table2);
    expect(mockDbConnection.getUser()).andReturn("myUserName");
    expect(mockDbProvider.select(isA(String.class))).andReturn(new DbRecordValuesList[] {});
    replay(mockDbConnection);
    replay(mockDbProvider);
    try {
        envHandler.writeBackupToFile(mockFileWriter);
    } catch (DbException e) {
        Assert.assertTrue(e.getMessage() != null && e.getMessage().startsWith("Could not get columns for table"));
    }
}
Also used : OracleEnvironmentHandler(com.axway.ats.environment.database.OracleEnvironmentHandler) DbTable(com.axway.ats.environment.database.model.DbTable) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 30 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class MysqlEnvironmentHandler method getColumnsToSelect.

@Override
protected List<ColumnDescription> getColumnsToSelect(DbTable table, String userName) throws DbException, ColumnHasNoDefaultValueException {
    // TODO Might be replaced with JDBC DatabaseMetaData.getColumns() but should be verified with default values
    ArrayList<ColumnDescription> columnsToSelect = new ArrayList<ColumnDescription>();
    DbRecordValuesList[] columnsMetaData = null;
    try {
        columnsMetaData = dbProvider.select("SHOW COLUMNS FROM " + table.getTableName());
    } catch (DbException e) {
        log.error("Could not get columns for table " + table.getTableName() + ". Check if the table is existing and that the user has permissions. See more details in the trace.");
        throw e;
    }
    for (DbRecordValuesList columnMetaData : columnsMetaData) {
        String columnName = (String) columnMetaData.get(MysqlColumnNames.COLUMN_NAME.getName(isJDBC4));
        //check if the column should be skipped in the backup
        if (!table.getColumnsToExclude().contains(columnName)) {
            ColumnDescription colDescription = new MysqlColumnDescription(columnName, (String) columnMetaData.get(MysqlColumnNames.COLUMN_TYPE.getName(isJDBC4)));
            columnsToSelect.add(colDescription);
        } else {
            //if this column has no default value, we cannot skip it in the backup
            if (columnMetaData.get(MysqlColumnNames.DEFAULT_COLUMN.getName(isJDBC4)) == null) {
                log.error("Cannot skip columns with no default values while creating backup");
                throw new ColumnHasNoDefaultValueException(table.getTableName(), columnName);
            }
        }
    }
    return columnsToSelect;
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) ColumnHasNoDefaultValueException(com.axway.ats.environment.database.exceptions.ColumnHasNoDefaultValueException) MysqlColumnDescription(com.axway.ats.core.dbaccess.MysqlColumnDescription) ColumnDescription(com.axway.ats.core.dbaccess.ColumnDescription) ArrayList(java.util.ArrayList) MysqlColumnDescription(com.axway.ats.core.dbaccess.MysqlColumnDescription) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Aggregations

DbException (com.axway.ats.core.dbaccess.exceptions.DbException)31 SQLException (java.sql.SQLException)11 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)8 DatabaseOperationsException (com.axway.ats.action.exceptions.DatabaseOperationsException)7 PublicAtsApi (com.axway.ats.common.PublicAtsApi)7 Connection (java.sql.Connection)6 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 PreparedStatement (java.sql.PreparedStatement)5 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)4 DatabaseEnvironmentCleanupException (com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException)4 BufferedReader (java.io.BufferedReader)4 File (java.io.File)4 FileReader (java.io.FileReader)4 ColumnDescription (com.axway.ats.core.dbaccess.ColumnDescription)3 CassandraDbProvider (com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)3 ColumnHasNoDefaultValueException (com.axway.ats.environment.database.exceptions.ColumnHasNoDefaultValueException)3 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 DatabaseCell (com.axway.ats.action.dbaccess.model.DatabaseCell)2