Search in sources :

Example 66 with DatabaseException

use of liquibase.exception.DatabaseException in project liquibase by liquibase.

the class DropAllCommand method run.

@Override
protected CommandResult run() throws Exception {
    try {
        LockServiceFactory.getInstance().getLockService(database).waitForLock();
        for (CatalogAndSchema schema : schemas) {
            log.info("Dropping Database Objects in schema: " + schema);
            checkLiquibaseTables(false, null, new Contexts(), new LabelExpression());
            database.dropDatabaseObjects(schema);
        }
    } catch (DatabaseException e) {
        throw e;
    } catch (Exception e) {
        throw new DatabaseException(e);
    } finally {
        LockServiceFactory.getInstance().getLockService(database).destroy();
        resetServices();
    }
    return new CommandResult("All objects dropped from " + database.getConnection().getConnectionUserName() + "@" + database.getConnection().getURL());
}
Also used : LabelExpression(liquibase.LabelExpression) CatalogAndSchema(liquibase.CatalogAndSchema) Contexts(liquibase.Contexts) DatabaseException(liquibase.exception.DatabaseException) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException) CommandResult(liquibase.command.CommandResult)

Example 67 with DatabaseException

use of liquibase.exception.DatabaseException in project liquibase by liquibase.

the class DatabaseFactory method openConnection.

public DatabaseConnection openConnection(String url, String username, String password, String driver, String databaseClass, String driverPropertiesFile, String propertyProviderClass, ResourceAccessor resourceAccessor) throws DatabaseException {
    if (url.startsWith("offline:")) {
        return new OfflineConnection(url, resourceAccessor);
    }
    driver = StringUtils.trimToNull(driver);
    if (driver == null) {
        driver = DatabaseFactory.getInstance().findDefaultDriver(url);
    }
    try {
        Driver driverObject;
        DatabaseFactory databaseFactory = DatabaseFactory.getInstance();
        if (databaseClass != null) {
            databaseFactory.clearRegistry();
            databaseFactory.register((Database) Class.forName(databaseClass, true, resourceAccessor.toClassLoader()).newInstance());
        }
        try {
            if (driver == null) {
                driver = databaseFactory.findDefaultDriver(url);
            }
            if (driver == null) {
                throw new RuntimeException("Driver class was not specified and could not be determined from the url (" + url + ")");
            }
            driverObject = (Driver) Class.forName(driver, true, resourceAccessor.toClassLoader()).newInstance();
        } catch (Exception e) {
            throw new RuntimeException("Cannot find database driver: " + e.getMessage());
        }
        Properties driverProperties;
        if (propertyProviderClass == null) {
            driverProperties = new Properties();
        } else {
            driverProperties = (Properties) Class.forName(propertyProviderClass, true, resourceAccessor.toClassLoader()).newInstance();
        }
        if (username != null) {
            driverProperties.put("user", username);
        }
        if (password != null) {
            driverProperties.put("password", password);
        }
        if (null != driverPropertiesFile) {
            File propertiesFile = new File(driverPropertiesFile);
            if (propertiesFile.exists()) {
                //                    System.out.println("Loading properties from the file:'" + driverPropertiesFile + "'");
                FileInputStream inputStream = new FileInputStream(propertiesFile);
                try {
                    driverProperties.load(inputStream);
                } finally {
                    inputStream.close();
                }
            } else {
                throw new RuntimeException("Can't open JDBC Driver specific properties from the file: '" + driverPropertiesFile + "'");
            }
        }
        //            System.out.println("Properties:");
        //            for (Map.Entry entry : driverProperties.entrySet()) {
        //                System.out.println("Key:'"+entry.getKey().toString()+"' Value:'"+entry.getValue().toString()+"'");
        //            }
        //            System.out.println("Connecting to the URL:'"+url+"' using driver:'"+driverObject.getClass().getName()+"'");
        Connection connection = driverObject.connect(url, driverProperties);
        //            System.out.println("Connection has been created");
        if (connection == null) {
            throw new DatabaseException("Connection could not be created to " + url + " with driver " + driverObject.getClass().getName() + ".  Possibly the wrong driver for the given database URL");
        }
        return new JdbcConnection(connection);
    } catch (Exception e) {
        throw new DatabaseException(e);
    }
}
Also used : Connection(java.sql.Connection) JdbcConnection(liquibase.database.jvm.JdbcConnection) Driver(java.sql.Driver) JdbcConnection(liquibase.database.jvm.JdbcConnection) File(java.io.File) DatabaseException(liquibase.exception.DatabaseException) DatabaseException(liquibase.exception.DatabaseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) FileInputStream(java.io.FileInputStream)

Example 68 with DatabaseException

use of liquibase.exception.DatabaseException in project liquibase by liquibase.

the class DerbyDatabase method close.

@Override
public void close() throws DatabaseException {
    String url = getConnection().getURL();
    String driverName = getDefaultDriver(url);
    super.close();
    if (driverName != null && driverName.toLowerCase().contains("embedded")) {
        try {
            if (url.contains(";")) {
                url = url.substring(0, url.indexOf(";")) + ";shutdown=true";
            } else {
                url += ";shutdown=true";
            }
            LogFactory.getLogger().info("Shutting down derby connection: " + url);
            // this cleans up the lock files in the embedded derby database folder
            ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
            Driver driver = (Driver) contextClassLoader.loadClass(driverName).newInstance();
            driver.connect(url, null);
        } catch (Exception e) {
            if (e instanceof SQLException) {
                String state = ((SQLException) e).getSQLState();
                if ("XJ015".equals(state) || "08006".equals(state)) {
                    // See http://db.apache.org/derby/docs/dev/getstart/rwwdactivity3.html
                    return;
                }
            }
            throw new DatabaseException("Error closing derby cleanly", e);
        }
    }
}
Also used : Driver(java.sql.Driver) DatabaseException(liquibase.exception.DatabaseException) DatabaseException(liquibase.exception.DatabaseException)

Example 69 with DatabaseException

use of liquibase.exception.DatabaseException in project liquibase by liquibase.

the class DatabaseTestContext method openDatabaseConnection.

public DatabaseConnection openDatabaseConnection(String url) throws Exception {
    String username = getUsername(url);
    String password = getPassword(url);
    JUnitJDBCDriverClassLoader jdbcDriverLoader = JUnitJDBCDriverClassLoader.getInstance();
    final Driver driver;
    try {
        driver = (Driver) Class.forName(DatabaseFactory.getInstance().findDefaultDriver(url), true, jdbcDriverLoader).newInstance();
    } catch (Exception e) {
        System.out.println("Could not connect to " + url + ": Will not test against.  " + e.getMessage());
        //could not connect
        return null;
    }
    Properties info = new Properties();
    info.put("user", username);
    if (password != null) {
        info.put("password", password);
    }
    //for db2
    info.put("retrieveMessagesFromServerOnGetMessage", "true");
    Connection connection;
    try {
        connection = driver.connect(url, info);
    } catch (SQLException e) {
        System.out.println("Could not connect to " + url + ": Will not test against.  " + e.getMessage());
        //could not connect
        return null;
    }
    if (connection == null) {
        throw new DatabaseException("Connection could not be created to " + url + " with driver " + driver.getClass().getName() + ".  Possibly the wrong driver for the given database URL");
    }
    return new JdbcConnection(connection);
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcConnection(liquibase.database.jvm.JdbcConnection) Driver(java.sql.Driver) JdbcConnection(liquibase.database.jvm.JdbcConnection) DatabaseException(liquibase.exception.DatabaseException) DatabaseException(liquibase.exception.DatabaseException) SQLException(java.sql.SQLException)

Example 70 with DatabaseException

use of liquibase.exception.DatabaseException in project liquibase by liquibase.

the class Liquibase method executeRollbackScript.

protected void executeRollbackScript(String rollbackScript, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
    final Executor executor = ExecutorService.getInstance().getExecutor(database);
    String rollbackScriptContents;
    try {
        Set<InputStream> streams = resourceAccessor.getResourcesAsStream(rollbackScript);
        if (streams == null || streams.size() == 0) {
            throw new LiquibaseException("Cannot find rollbackScript " + rollbackScript);
        } else if (streams.size() > 1) {
            throw new LiquibaseException("Found multiple rollbackScripts named " + rollbackScript);
        }
        rollbackScriptContents = StreamUtil.getStreamContents(streams.iterator().next());
    } catch (IOException e) {
        throw new LiquibaseException("Error reading rollbackScript " + executor + ": " + e.getMessage());
    }
    RawSQLChange rollbackChange = new RawSQLChange(rollbackScriptContents);
    rollbackChange.setSplitStatements(true);
    rollbackChange.setStripComments(true);
    try {
        executor.execute(rollbackChange);
    } catch (DatabaseException e) {
        e = new DatabaseException("Error executing rollback script. ChangeSets will still be marked as rolled back: " + e.getMessage(), e);
        System.err.println(e.getMessage());
        log.severe("Error executing rollback script", e);
        if (changeExecListener != null) {
            changeExecListener.runFailed(null, databaseChangeLog, database, e);
        }
    }
    database.commit();
}
Also used : RawSQLChange(liquibase.change.core.RawSQLChange) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

DatabaseException (liquibase.exception.DatabaseException)139 SQLException (java.sql.SQLException)65 JdbcConnection (liquibase.database.jvm.JdbcConnection)34 PreparedStatement (java.sql.PreparedStatement)33 ResultSet (java.sql.ResultSet)28 Statement (java.sql.Statement)24 Database (liquibase.database.Database)24 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)24 CustomChangeException (liquibase.exception.CustomChangeException)22 CatalogAndSchema (liquibase.CatalogAndSchema)17 LiquibaseException (liquibase.exception.LiquibaseException)16 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)14 InvalidExampleException (liquibase.snapshot.InvalidExampleException)14 RawSqlStatement (liquibase.statement.core.RawSqlStatement)14 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)13 CachedRow (liquibase.snapshot.CachedRow)13 SqlStatement (liquibase.statement.SqlStatement)13 DatabaseConnection (liquibase.database.DatabaseConnection)12 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)12 ArrayList (java.util.ArrayList)11