17

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?

3 Answers 3

9

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.
Sign up to request clarification or add additional context in comments.

1 Comment

ran into this using ninja framework + flyway. Had to append /postgres when connecting to AWS RDS instance
2

I had this same issue when working on a Java application in Debian 10 using Tomcat Application server.

I defined the connection strings for the database in the context.xml file, however, when I start out the application and try to log into the application, I get the error:

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)

Here's what I figured out:

I finally realized that the application was using internally defined database connection strings that were packaged with it. The internally defined database connection strings were different from my own database connection strings defined in the context.xml file.

The solution for me was to either modify the internally defined database connection strings that were packaged with the application or use the same internally defined database connection strings that were packaged with application in my context.xml file.

That's all.

I hope this helps.

Comments

1

Ran into this same issue. Apparently, the problem was with my .properties file. The jar was using the one packaged with it and not the external one. So I moved my external properties file out of the resources folder and into the root directory of the jar and problem solved! Hope this helps someone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.