Flyway migration, Unable to obtain Jdbc connection from DataSource

TurboNerd picture TurboNerd · Aug 7, 2014 · Viewed 45.1k times · Source

I am trying to use flyway to create and manage a MySQL database. Here is the code i have got so far.

FlywayMigration.java : Class that applys the migration

public class FlywayMigration
{
    public FlywayMigration(DatabaseConfiguration configuration, Flyway flyway)
    {
        flyway.setDataSource(configuration.getDataSource());
        flyway.migrate();
    }

    public static void main(String[] args)
    {
        new FlywayMigration(new DatabaseConfiguration("database.properties"), new Flyway());
    }
}

DatabaseConfiguration.java : Configuration class, this class will configure the datasource to be applyed to the Flyway.setDataSource method

public class DatabaseConfiguration
{
    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    private PropertiesUtil prop = null;

    public DatabaseConfiguration(String file)
    {
        prop = new PropertiesUtil(file);
    }

    public String getDataSourceClass()
    {
        return prop.getProperty("mysql.data.source.class");
    }

    public String getURL ()
    {
        return prop.getProperty("mysql.url");
    }

    public String getHostName()
    {
        return prop.getProperty("mysql.host.name");
    }

    public String getDatabaseName()
    {
        return prop.getProperty("mysql.database.name");
    }

    public DataSource getDataSource()
    {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setURL(getURL());
        dataSource.setUser(prop.getProperty("mysql.user.name"));
        dataSource.setPassword(null);
        return dataSource;
    }
}

database.properties is the file where i store the database information, password can be null

mysql.data.source.class=com.mysql.jdbc.Driver    
mysql.url=jdbc:mysql://localhost:3306/vmrDB    
mysql.host.name=localhost    
mysql.database.name=vmrDB    
mysql.user.name=root

And i get the folowing error in my trace

Exception in thread "main" org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
    at org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56)
    at org.flywaydb.core.Flyway.execute(Flyway.java:1144)
    at org.flywaydb.core.Flyway.migrate(Flyway.java:811)
    at com.bt.sitb.vmr.migration.FlywayMigration.<init>(FlywayMigration.java:10)
    at com.bt.sitb.vmr.migration.FlywayMigration.main(FlywayMigration.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Can someone please tell me why the DataSource from MySQL is not connecting. Thanks!

Answer

florian picture florian · Aug 26, 2014

It looks like Flyway cannot connect to the database.

One reason for this is that the database in the database URL does not exist.

Question: does your database schema exist?

If your answer is no, then:

  • connect to jdbc:mysql://localhost:3306/mysql
  • also specify the schema to use for migration with flyway.setSchemas(configuration.getDatabaseName())
  • you also need flyway.init() before you can initialize migration of your database.