Search in sources :

Example 41 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 42 with DatabaseException

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

the class DatabaseFactory method findCorrectDatabaseImplementation.

public Database findCorrectDatabaseImplementation(DatabaseConnection connection) throws DatabaseException {
    SortedSet<Database> foundDatabases = new TreeSet<Database>(new DatabaseComparator());
    for (Database implementedDatabase : getImplementedDatabases()) {
        if (connection instanceof OfflineConnection) {
            if (((OfflineConnection) connection).isCorrectDatabaseImplementation(implementedDatabase)) {
                foundDatabases.add(implementedDatabase);
            }
        } else {
            if (implementedDatabase.isCorrectDatabaseImplementation(connection)) {
                foundDatabases.add(implementedDatabase);
            }
        }
    }
    if (foundDatabases.size() == 0) {
        log.warning("Unknown database: " + connection.getDatabaseProductName());
        UnsupportedDatabase unsupportedDB = new UnsupportedDatabase();
        unsupportedDB.setConnection(connection);
        return unsupportedDB;
    }
    Database returnDatabase;
    try {
        returnDatabase = foundDatabases.iterator().next().getClass().newInstance();
    } catch (Exception e) {
        throw new UnexpectedLiquibaseException(e);
    }
    returnDatabase.setConnection(connection);
    return returnDatabase;
}
Also used : UnsupportedDatabase(liquibase.database.core.UnsupportedDatabase) UnsupportedDatabase(liquibase.database.core.UnsupportedDatabase) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 43 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 44 with DatabaseException

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

the class ClobType method toDatabaseDataType.

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
    String originalDefinition = StringUtils.trimToEmpty(getRawDefinition());
    if (database instanceof MSSQLDatabase) {
        if (!LiquibaseConfiguration.getInstance().getProperty(GlobalConfiguration.class, GlobalConfiguration.CONVERT_DATA_TYPES).getValue(Boolean.class) && originalDefinition.toLowerCase().startsWith("text")) {
            DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("text"));
            type.addAdditionalInformation(getAdditionalInformation());
            return type;
        }
    }
    if (database instanceof FirebirdDatabase) {
        return new DatabaseDataType("BLOB SUB_TYPE TEXT");
    } else if (database instanceof SybaseASADatabase) {
        return new DatabaseDataType("LONG VARCHAR");
    } else if (database instanceof MSSQLDatabase) {
        if (originalDefinition.equalsIgnoreCase("text") || originalDefinition.equals("[text]") || originalDefinition.matches("(?i)text .+") || originalDefinition.matches("\\[text\\] .+")) {
            DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("text"));
            type.addAdditionalInformation(getAdditionalInformation());
            return type;
        }
        if (originalDefinition.toLowerCase().startsWith("ntext") || originalDefinition.toLowerCase().startsWith("[ntext]")) {
            DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("ntext"));
            type.addAdditionalInformation(getAdditionalInformation());
            return type;
        }
        if (originalDefinition.equalsIgnoreCase("nclob")) {
            try {
                if (database.getDatabaseMajorVersion() <= 8) {
                    //2000 or earlier
                    DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("ntext"));
                    type.addAdditionalInformation(getAdditionalInformation());
                    return type;
                }
            } catch (DatabaseException ignore) {
            }
            return new DatabaseDataType(database.escapeDataTypeName("nvarchar"), "MAX");
        }
        try {
            if (database.getDatabaseMajorVersion() <= 8) {
                //2000 or earlier
                DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("text"));
                type.addAdditionalInformation(getAdditionalInformation());
                return type;
            }
        } catch (DatabaseException ignore) {
        }
        return new DatabaseDataType(database.escapeDataTypeName("varchar"), "MAX");
    } else if (database instanceof MySQLDatabase) {
        if (originalDefinition.toLowerCase().startsWith("text")) {
            return new DatabaseDataType("TEXT");
        } else if (originalDefinition.toLowerCase().startsWith("tinytext")) {
            return new DatabaseDataType("TINYTEXT");
        } else if (originalDefinition.toLowerCase().startsWith("mediumtext")) {
            return new DatabaseDataType("MEDIUMTEXT");
        } else if (originalDefinition.toLowerCase().startsWith("nclob")) {
            DatabaseDataType type = new DatabaseDataType("LONGTEXT");
            type.addAdditionalInformation("CHARACTER SET utf8");
            return type;
        } else {
            return new DatabaseDataType("LONGTEXT");
        }
    } else if (database instanceof H2Database || database instanceof HsqlDatabase) {
        if (originalDefinition.toLowerCase().startsWith("longvarchar") || originalDefinition.startsWith("java.sql.Types.LONGVARCHAR")) {
            return new DatabaseDataType("LONGVARCHAR");
        } else {
            return new DatabaseDataType("CLOB");
        }
    } else if (database instanceof PostgresDatabase || database instanceof SQLiteDatabase || database instanceof SybaseDatabase) {
        return new DatabaseDataType("TEXT");
    } else if (database instanceof OracleDatabase) {
        if (originalDefinition.equalsIgnoreCase("nclob")) {
            return new DatabaseDataType("NCLOB");
        }
        return new DatabaseDataType("CLOB");
    } else if (database instanceof InformixDatabase) {
        if (originalDefinition.toLowerCase().startsWith("text")) {
            return new DatabaseDataType("TEXT");
        }
    }
    return super.toDatabaseDataType(database);
}
Also used : GlobalConfiguration(liquibase.configuration.GlobalConfiguration) DatabaseDataType(liquibase.datatype.DatabaseDataType) DatabaseException(liquibase.exception.DatabaseException)

Example 45 with DatabaseException

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

the class DerbyConnection method checkPoint.

private void checkPoint() throws DatabaseException {
    Statement st = null;
    try {
        st = createStatement();
        st.execute("CALL SYSCS_UTIL.SYSCS_CHECKPOINT_DATABASE()");
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        JdbcUtils.closeStatement(st);
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

DatabaseException (liquibase.exception.DatabaseException)73 SQLException (java.sql.SQLException)25 Database (liquibase.database.Database)24 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)16 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)14 CatalogAndSchema (liquibase.CatalogAndSchema)13 LiquibaseException (liquibase.exception.LiquibaseException)13 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)12 JdbcConnection (liquibase.database.jvm.JdbcConnection)12 CachedRow (liquibase.snapshot.CachedRow)10 InvalidExampleException (liquibase.snapshot.InvalidExampleException)10 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)10 Executor (liquibase.executor.Executor)9 DatabaseConnection (liquibase.database.DatabaseConnection)7 OfflineConnection (liquibase.database.OfflineConnection)7 Connection (java.sql.Connection)6 Statement (java.sql.Statement)6 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)6 RawSqlStatement (liquibase.statement.core.RawSqlStatement)6 IOException (java.io.IOException)5