Search in sources :

Example 6 with FlywaySqlException

use of org.flywaydb.core.internal.dbsupport.FlywaySqlException in project flyway by flyway.

the class DriverDataSource method getConnectionFromDriver.

/**
     * Build properties for the Driver, including the given user and password (if any),
     * and obtain a corresponding Connection.
     *
     * @param username the name of the user
     * @param password the password to use
     * @return the obtained Connection
     * @throws SQLException in case of failure
     * @see java.sql.Driver#connect(String, java.util.Properties)
     */
protected Connection getConnectionFromDriver(String username, String password) throws SQLException {
    Properties props = new Properties(this.defaultProps);
    if (username != null) {
        props.setProperty("user", username);
    }
    if (password != null) {
        props.setProperty("password", password);
    }
    Connection connection;
    try {
        connection = driver.connect(url, props);
    } catch (SQLException e) {
        throw new FlywaySqlException("Unable to obtain Jdbc connection from DataSource (" + url + ") for user '" + user + "': " + e.getMessage(), e);
    }
    for (String initSql : initSqls) {
        Statement statement = null;
        try {
            statement = connection.createStatement();
            statement.execute(initSql);
        } finally {
            JdbcUtils.closeStatement(statement);
        }
    }
    connection.setAutoCommit(autoCommit);
    return connection;
}
Also used : FlywaySqlException(org.flywaydb.core.internal.dbsupport.FlywaySqlException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) Properties(java.util.Properties)

Aggregations

SQLException (java.sql.SQLException)6 FlywaySqlException (org.flywaydb.core.internal.dbsupport.FlywaySqlException)6 HashMap (java.util.HashMap)2 FlywayException (org.flywaydb.core.api.FlywayException)2 MigrationVersion (org.flywaydb.core.api.MigrationVersion)2 SqlScript (org.flywaydb.core.internal.dbsupport.SqlScript)2 PlaceholderReplacer (org.flywaydb.core.internal.util.PlaceholderReplacer)2 ClassPathResource (org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 Savepoint (java.sql.Savepoint)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 MigrationExecutor (org.flywaydb.core.api.resolver.MigrationExecutor)1 AppliedMigration (org.flywaydb.core.internal.metadatatable.AppliedMigration)1 StopWatch (org.flywaydb.core.internal.util.StopWatch)1 RowMapper (org.flywaydb.core.internal.util.jdbc.RowMapper)1 TransactionTemplate (org.flywaydb.core.internal.util.jdbc.TransactionTemplate)1