Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No "current" schema in flyway.url causes a Flyway exception #598

Closed
tomsquest opened this issue Sep 9, 2013 · 22 comments
Closed

No "current" schema in flyway.url causes a Flyway exception #598

tomsquest opened this issue Sep 9, 2013 · 22 comments

Comments

@tomsquest
Copy link

@tomsquest tomsquest commented Sep 9, 2013

Using the Maven plugin, with a Mysql JDBC url containing no schema, Flyway migrates the database, but fails just after complaining with "Error setting current schema to ``: No database selected".

Failing Jdbc url : jdbc:mysql://localhost:3306
Valid Jdbc url : jdbc:mysql://localhost:3306/myDatabase

This occurs even with some schemas specified (in the pom.xml),

Not reproduced with Flyway 2.1.1 (maven 3.0.5)
Failing with Flyway plugin : 2.2.1 (maven 3.1)
Driver : org.gjt.mm.mysql.Driver

It seems that this line in Flyway.java (https://github.com/flyway/flyway/blob/master/flyway-core/src/main/java/com/googlecode/flyway/core/Flyway.java#L875) assumes that dbSupport.getCurrentSchema() will return something meaningful but it returns an empty string the failing jdbc url.

@axelfontaine
Copy link
Contributor

@axelfontaine axelfontaine commented Sep 9, 2013

Could you try using the newer driver (http://stackoverflow.com/questions/5808160/what-is-the-jdbc-driver-org-gjt-mm-mysql-driver-for) and let me know what the behavior is?

@tomsquest
Copy link
Author

@tomsquest tomsquest commented Sep 10, 2013

Same behavior using com.mysql.jdbc.Driver and the latest version (5.1.26).

Extract from the pom.xml :

                   <plugin>
                        <groupId>com.googlecode.flyway</groupId>
                        <artifactId>flyway-maven-plugin</artifactId>
                        <configuration>
                            <driver>com.mysql.jdbc.Driver</driver>
                            <url>jdbc:mysql://localhost:3306</url> <!-- fix: jdbc:mysql://localhost:3306/dba -->
                            <user>dbuser</user>
                            <password>dbpass</password>
                            <schemas>
                                <schema>dba</schema>
                                <schema>dbb</schema>
                            </schemas>
                            <placeholders>
                                <customer>someone</customer>
                            </placeholders>
                            <locations>
                                <location>classpath:db.migration.mysql</location>
                            </locations>
                            <initOnMigrate>true</initOnMigrate>
                        </configuration>
                    </plugin>

Here is the stacktrace :

[ERROR] Failed to execute goal com.googlecode.flyway:flyway-maven-plugin:2.2.1:migrate (default-cli) on project webapp: com.googlecode.flyway.core.api.FlywayException: Error setting current schema to ``: No database selected -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.googlecode.flyway:flyway-maven-plugin:2.2.1:migrate (default-cli) on project webapp: com.googlecode.flyway.core.api.FlywayException: Error setting current schema to ``
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.apache.maven.plugin.MojoExecutionException: com.googlecode.flyway.core.api.FlywayException: Error setting current schema to ``
        at com.googlecode.flyway.maven.AbstractFlywayMojo.execute(AbstractFlywayMojo.java:253)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
        ... 19 more
Caused by: java.sql.SQLException: No database selected
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2818)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2157)
        at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
        at com.googlecode.flyway.core.dbsupport.JdbcTemplate.execute(JdbcTemplate.java:214)
        at com.googlecode.flyway.core.dbsupport.mysql.MySQLDbSupport.doSetCurrentSchema(MySQLDbSupport.java:55)
        at com.googlecode.flyway.core.dbsupport.DbSupport.setCurrentSchema(DbSupport.java:102)
        at com.googlecode.flyway.core.Flyway$1.execute(Flyway.java:875)
        at com.googlecode.flyway.core.Flyway$1.execute(Flyway.java:819)
        at com.googlecode.flyway.core.Flyway.execute(Flyway.java:1200)
        at com.googlecode.flyway.core.Flyway.migrate(Flyway.java:819)
        at com.googlecode.flyway.maven.MigrateMojo.doExecuteWithMigrationConfig(MigrateMojo.java:159)
        at com.googlecode.flyway.maven.AbstractMigrationLoadingMojo.doExecute(AbstractMigrationLoadingMojo.java:151)
        at com.googlecode.flyway.maven.AbstractFlywayMojo.execute(AbstractFlywayMojo.java:251)
        ... 21 more

@bigslack
Copy link

@bigslack bigslack commented Sep 10, 2013

I have had this issue as well. It seems that setting the schema doesn't always have the behavior one might expect. I've found the following behavior to currently exist (I'm using the API but it be the same for the maven plugin).

For the following scenarios the following will apply:

  • The schema is: flyway-schema-test-db
  • The sql migration script is:

DROP TABLE IF EXISTS bug_test;
CREATE TABLE bug_test (
  id varchar(36) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO bug_test VALUES ('hello');

  • All properties are default unless set otherwise in the example.

Case 1 - Running .migrate() with schema set in datasource url (schema not physically created yet)

Code run:

Flyway flyway = new Flyway();
flyway.setDataSource("jdbc:mysql://localhost:3306/flyway-schema-test-db","root","");
flyway.setLocations("filesystem:" + getProp("test.data.sql.dir") + "/test");
flyway.migrate();

Log output:

Exception in thread "main" com.googlecode.flyway.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
    at com.googlecode.flyway.core.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56)
    at com.googlecode.flyway.core.Flyway.execute(Flyway.java:1193)
    at com.googlecode.flyway.core.Flyway.migrate(Flyway.java:834)
    at com.cmt.test.utils.ConfigUtils.flywaySchemaTest(ConfigUtils.java:177)
    at com.cmt.test.utils.ConfigUtils.main(ConfigUtils.java:182)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'flyway-schema-test-db'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    ...

The jdbc driver is unable to connect to the datasource localhost:3306/flyway-schema-test-db because it doesn't exist.

Case 2 - Running .migrate() with schema set by property (schema not physically created yet)

Code run:

    Flyway flyway = new Flyway();
    flyway.setDataSource("jdbc:mysql://localhost:3306/","root","");
    flyway.setSchemas("flyway-schema-test-db");
    flyway.setLocations("filesystem:" + getProp("test.data.sql.dir") + "/test");
    flyway.migrate();

Log output:

[2013-09-10 17:04:07,449] INFO  DbSchemas : Creating schema `flyway-schema-test-db` ...
[2013-09-10 17:04:07,454] INFO  MetaDataTableImpl : Creating Metadata table: `flyway-schema-test-db`.`schema_version`
[2013-09-10 17:04:08,170] INFO  DbMigrate : Current version of schema `flyway-schema-test-db`: 0
[2013-09-10 17:04:08,170] INFO  DbMigrate : Migrating schema `flyway-schema-test-db` to version 1
[2013-09-10 17:04:08,299] INFO  DbMigrate : Successfully applied 1 migration to schema `flyway-schema-test-db` (execution time 00:00.150s).
Exception in thread "main" com.googlecode.flyway.core.api.FlywayException: Error setting current schema to ``
    at com.googlecode.flyway.core.dbsupport.DbSupport.setCurrentSchema(DbSupport.java:104)
    at com.googlecode.flyway.core.Flyway$1.execute(Flyway.java:890)
    at com.googlecode.flyway.core.Flyway$1.execute(Flyway.java:1)
    at com.googlecode.flyway.core.Flyway.execute(Flyway.java:1219)
    at com.googlecode.flyway.core.Flyway.migrate(Flyway.java:834)
    at com.cmt.test.utils.ConfigUtils.flywaySchemaTest(ConfigUtils.java:178)
    at com.cmt.test.utils.ConfigUtils.main(ConfigUtils.java:183)
Caused by: java.sql.SQLException: No database selected
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    ...

You can successfully create the flyway-schema-test-db and the flyway schema_version table. However, you are unable to run any migrate scripts because a FlywayException: Error setting current schema to '' is thrown.

Case 3 - Running .migrate() with schema set by property (empty schema already exists)

Code run:

    Flyway flyway = new Flyway();
    flyway.setDataSource("jdbc:mysql://localhost:3306/","root","");
    flyway.setSchemas("flyway-schema-test-db");
    flyway.setLocations("filesystem:" + getProp("test.data.sql.dir") + "/test");
    flyway.migrate();

Log output:

[2013-09-10 17:15:58,316] INFO  MetaDataTableImpl : Creating Metadata table: `flyway-schema-test-db`.`schema_version`
[2013-09-10 17:15:58,852] INFO  DbMigrate : Current version of schema `flyway-schema-test-db`: << Empty Schema >>
[2013-09-10 17:15:58,852] INFO  DbMigrate : Migrating schema `flyway-schema-test-db` to version 1
[2013-09-10 17:15:58,964] INFO  DbMigrate : Successfully applied 1 migration to schema `flyway-schema-test-db` (execution time 00:00.649s).
Exception in thread "main" com.googlecode.flyway.core.api.FlywayException: Error setting current schema to ``
    at com.googlecode.flyway.core.dbsupport.DbSupport.setCurrentSchema(DbSupport.java:104)
    at com.googlecode.flyway.core.Flyway$1.execute(Flyway.java:890)
    at com.googlecode.flyway.core.Flyway$1.execute(Flyway.java:1)
    at com.googlecode.flyway.core.Flyway.execute(Flyway.java:1219)
    at com.googlecode.flyway.core.Flyway.migrate(Flyway.java:834)
    at com.cmt.test.utils.ConfigUtils.flywaySchemaTest(ConfigUtils.java:178)
    at com.cmt.test.utils.ConfigUtils.main(ConfigUtils.java:183)
Caused by: java.sql.SQLException: No database selected
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    ...

The bug_test table was successfully created but flyway still exited with an exception.

Case 4 - Running .migrate() with schema set in datasource url (empty schema already exists)

Code run:

    Flyway flyway = new Flyway();
    flyway.setDataSource("jdbc:mysql://localhost:3306/flyway-schema-test-db","root","");
    flyway.setLocations("filesystem:" + getProp("test.data.sql.dir") + "/test");
    flyway.migrate();

Log output:

[2013-09-10 17:20:36,209] INFO  MetaDataTableImpl : Creating Metadata table: `flyway-schema-test-db`.`schema_version`
[2013-09-10 17:20:36,831] INFO  DbMigrate : Current version of schema `flyway-schema-test-db`: << Empty Schema >>
[2013-09-10 17:20:36,831] INFO  DbMigrate : Migrating schema `flyway-schema-test-db` to version 1
[2013-09-10 17:20:36,954] INFO  DbMigrate : Successfully applied 1 migration to schema `flyway-schema-test-db` (execution time 00:00.747s).

Flyway exits w/o incident.

My thoughts on each case....

Case 1. - (Possible Feature) Should flyway be smart enough to check and see if the schema is available in the datasource then create it if not?
Case 2. - (Bug) The documentation for setSchemas(String...) states that the first entry will be the default schema. Flyway is not properly setting the default schema in this case.
Case 3. - (Bug) This looks like a bug as well.

@pfarland
Copy link

@pfarland pfarland commented Sep 19, 2013

I also see these issues. I was able to work around it by adding ?createDatabaseIfNotExist=true to the mysql jdbc connection url

@axelfontaine
Copy link
Contributor

@axelfontaine axelfontaine commented Dec 9, 2013

This is now fixed.

  1. & 4. No change. Workaround is fine.
  2. & 3. Fix committed.

Cheers
Axel

@ghost
Copy link

@ghost ghost commented Dec 22, 2014

Hello Flyway friends!
Hello @axelfontaine!

Today I installed Flyway v.3.1 on my MacBook after I download it from here [1].
After some reading I found out how to get executed my SQL statements... yeah! Unfortunately Flyway ended always with a "ERROR: Unexpected error java.lang.NullPointerException".

After some research and execution of Flyway with the verbose/debug switch (./flyway migrant -X), I ended here at GitHub. And sorry, I have to say, that it seams that the issue #598 a) isn't fixed or b) not merged/distributed to the official and downloadable version:

@flyway.properties

  • NOK : flyway.url=jdbc:mysql://localhost:3306/ (< results in the NullPointerException)
  • OK : flyway.url=jdbc:mysql://localhost:3306/flyway (< everything works fine)

Any suggestion? Please give me some advice if I have missed something or I'm doing something wrong! Thank you :)!

Greetings --- Adrian

[1] http://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/3.1/flyway-commandline-3.1.zip

@axelfontaine
Copy link
Contributor

@axelfontaine axelfontaine commented Dec 23, 2014

Hmmm a regression is never good. It seems we must be missing a test to cover this.

Could you post a stacktrace? Reopening for now.

@axelfontaine axelfontaine reopened this Dec 23, 2014
@axelfontaine axelfontaine added this to the Flyway 3.2 milestone Dec 23, 2014
@g0t4
Copy link

@g0t4 g0t4 commented Feb 5, 2015

Without a schema in the connection string (ie: flyway.url=jdbc:mysql://localhost), but with flyway.schemas set, the following exception comes back from the flyway CLI:

java.lang.NullPointerException
    at org.flywaydb.core.internal.dbsupport.mysql.MySQLDbSupport.doSetCurrentSchema(MySQLDbSupport.java:60)
    at org.flywaydb.core.internal.dbsupport.DbSupport.setCurrentSchema(DbSupport.java:101)
    at org.flywaydb.core.Flyway$1.execute(Flyway.java:975)
    at org.flywaydb.core.Flyway$1.execute(Flyway.java:919)
    at org.flywaydb.core.Flyway.execute(Flyway.java:1320)
    at org.flywaydb.core.Flyway.migrate(Flyway.java:919)
    at org.flywaydb.commandline.Main.executeOperation(Main.java:118)
    at org.flywaydb.commandline.Main.main(Main.java:87)

With a schema specified in the connection string (ie: flyway.url=jdbc:mysql://localhost/newdb) and flyway.schemas set, everything is fine if the database already exists, but if the database doesn't exist then the following exception happens:

org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource (jdbc:mysql://localhost/newdb) for user 'dev': Could not connect: Unknown database 'newdb'
    at org.flywaydb.core.internal.util.jdbc.DriverDataSource.getConnectionFromDriver(DriverDataSource.java:298)
    at org.flywaydb.core.internal.util.jdbc.DriverDataSource.getConnection(DriverDataSource.java:257)
    at org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:50)
    at org.flywaydb.core.Flyway.execute(Flyway.java:1287)
    at org.flywaydb.core.Flyway.migrate(Flyway.java:919)
    at org.flywaydb.commandline.Main.executeOperation(Main.java:118)
    at org.flywaydb.commandline.Main.main(Main.java:87)
Caused by: java.sql.SQLException: Could not connect: Unknown database 'newdb'
    at org.mariadb.jdbc.internal.SQLExceptionMapper.get(SQLExceptionMapper.java:149)
    at org.mariadb.jdbc.internal.SQLExceptionMapper.throwException(SQLExceptionMapper.java:106)
    at org.mariadb.jdbc.Driver.connect(Driver.java:114)
    at org.flywaydb.core.internal.util.jdbc.DriverDataSource.getConnectionFromDriver(DriverDataSource.java:295)
    ... 6 more
Caused by: org.mariadb.jdbc.internal.common.QueryException: Could not connect: Unknown database 'newdb'
    at org.mariadb.jdbc.internal.mysql.MySQLProtocol.checkErrorPacket(MySQLProtocol.java:526)
    at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connect(MySQLProtocol.java:480)
    at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connect(MySQLProtocol.java:673)
    at org.mariadb.jdbc.internal.mysql.MySQLProtocol.<init>(MySQLProtocol.java:266)
    at org.mariadb.jdbc.Driver.connect(Driver.java:110)
    ... 7 more

Not sure what way flyway configuration is intended to be used. if I want flyway creating my mysql database for me, do I or don't I, add the database name to the connection string?

Also this appears to be an issue specific to the mariadb driver, at least as far as the first exception.

@g0t4
Copy link

@g0t4 g0t4 commented Feb 6, 2015

Just an update, if I use the platform independent mysql driver (mysql-connector-java-5.1.34-bin.jar), I don't get an error when not specifying a schema in the connection string, and instead specifying the schema with flyway.schemas. Flyway successfully creates the database.

However, if I do specify the schema and the database doesn't exist, I do still get an exception (and perhaps this is intended and I should leave the schema out of the connection string if I want flyway to create my database for me):

org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource (jdbc:mysql://localhost/newdb) for user 'dev': Unknown database 'newdb'
    at org.flywaydb.core.internal.util.jdbc.DriverDataSource.getConnectionFromDriver(DriverDataSource.java:298)
    at org.flywaydb.core.internal.util.jdbc.DriverDataSource.getConnection(DriverDataSource.java:257)
    at org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:50)
    at org.flywaydb.core.Flyway.execute(Flyway.java:1287)
    at org.flywaydb.core.Flyway.migrate(Flyway.java:919)
    at org.flywaydb.commandline.Main.executeOperation(Main.java:118)
    at org.flywaydb.commandline.Main.main(Main.java:87)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'newdb'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
    at com.mysql.jdbc.Util.getInstance(Util.java:360)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:870)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1659)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1206)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2234)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
    at org.flywaydb.core.internal.util.jdbc.DriverDataSource.getConnectionFromDriver(DriverDataSource.java:295)
    ... 6 more

@g0t4
Copy link

@g0t4 g0t4 commented Feb 6, 2015

After finding your comments in this SOF question (https://stackoverflow.com/questions/22963186/flywayunable-to-create-schema-incorrect-database-name/22973660#22973660), I now understand that flyway expects the database to be created in advance. Creating it in advance was how I've been doing it all along but because mysql has a loophole (SCHEMA=DB) that allows flyway to create a database if the database name isn't in the connection string, I was led down a path of wondering what the intended behavior was in flyway :)... thanks! Is there somewhere this exists in the documentation already or that we could add this if not?

axelfontaine added a commit to flyway/flywaydb.org that referenced this issue Mar 4, 2015
@axelfontaine
Copy link
Contributor

@axelfontaine axelfontaine commented Mar 4, 2015

I wasn't able to reproduce this. We actually already had a test that covers this case and it was green. My suspicion is that some versions of the JDBC driver, including the one I use for testing returned "" for a connection with no schema, whereas other like the one that was used here return null.

I've added an extra null check to be on the safe side.

Cheers
Axel

@g0t4
Copy link

@g0t4 g0t4 commented Mar 4, 2015

Sounds good, thanks!

On Wed, Mar 4, 2015 at 10:13 AM, Axel Fontaine notifications@github.com
wrote:

I wasn't able to reproduce this. We actually already had a test that
covers this case and it was green. My suspicion is that some versions of
the JDBC driver, including the one I use for testing returned "" for a
connection with no schema, whereas other like the one that was used here
return null.

I've added an extra null check to be on the safe side.

Cheers
Axel


Reply to this email directly or view it on GitHub
#598 (comment).

jmahonin pushed a commit to jmahonin/flyway that referenced this issue Jul 15, 2015
@CyborTronik
Copy link

@CyborTronik CyborTronik commented Sep 1, 2015

Hi I have the next issue: org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource (jdbc:mysql:...

[ERROR] The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

Using flyway maven plugin 3.2.1 and MySQL driver 5.1.36.
The mysql server is used via docker container 5.6.26

For configuration :

  <configuration>
                    <driver>com.mysql.jdbc.Driver</driver>
                    <url>jdbc:mysql://127.0.0.1:3307/?createDatabaseIfNotExist=true</url>
                    <user>root</user>
                    <password>xxx</password>
                    <schemas>
                        <schema>xxx</schema>
                    </schemas>
                </configuration>

Using same details I'm connecting with success via Workbench.

Any workarounds for that?

@g0t4
Copy link

@g0t4 g0t4 commented Sep 2, 2015

Create your mysql database with the mysql command line tool, then you'll be
able to connect. Flyway isn't mean to create non-existent databases, as
there are a plethora of nuances to every database platform (when creating a
database) and for the most part there are good command line tools to create
new databases.

Here's what I use in a shell script along with flyway when I want to create
databases (by the way this drops and recreates, say a testing db):

mysql -u dev -e "DROP DATABASE IF EXISTS database_name; CREATE
DATABASE database_name;"

On Wed, Sep 2, 2015 at 3:20 AM, CyborTronik notifications@github.com
wrote:

Hi I have the next issue: org.flywaydb.core.api.FlywayException: Unable to
obtain Jdbc connection from DataSource (jdbc:mysql:...

Using flyway maven plugin 3.2.1 and MySQL driver 5.1.36.
The mysql server is used via docker container 5.6.26

For configuration :

com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1:3307/?createDatabaseIfNotExist=true
root
xxx

xxx

Using same details I'm connecting with success via Workbench.

Any workarounds for that?


Reply to this email directly or view it on GitHub
#598 (comment).

@diogogmt
Copy link

@diogogmt diogogmt commented Sep 16, 2015

Having similar issues:

org.flywaydb.core.api.FlywayException: Unable to check whether schema `sphereds` is empty
    at org.flywaydb.core.internal.dbsupport.Schema.empty(Schema.java:95)
    at org.flywaydb.core.Flyway$1.execute(Flyway.java:1021)
    at org.flywaydb.core.Flyway$1.execute(Flyway.java:1006)
    at org.flywaydb.core.Flyway.execute(Flyway.java:1418)
    at org.flywaydb.core.Flyway.migrate(Flyway.java:1006)
    at com.clouddynamics.exosphere.platform.flyway.FlywayService.migrate(FlywayService.java:81)
    at com.clouddynamics.exosphere.platform.common.init.ServiceContainerStartupManager.initializeResources(ServiceContainerStartupManager.java:34)
    at com.clouddynamics.exosphere.platform.common.init.ExosphereServiceContainerListener.contextInitialized(ExosphereServiceContainerListener.java:35)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1738)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: Cannot proceed because system tables used by Event Scheduler were found damaged at server start
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1094)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4226)
    at com.mysql.jdbc.MysqlIO.nextRowFast(MysqlIO.java:2188)
    at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:2044)
    at com.mysql.jdbc.MysqlIO.readSingleRowSet(MysqlIO.java:3538)
    at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:489)
    at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:3240)
    at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:2411)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2834)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2840)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2212)
    at org.flywaydb.core.internal.dbsupport.JdbcTemplate.queryForInt(JdbcTemplate.java:156)
    at org.flywaydb.core.internal.dbsupport.mysql.MySQLSchema.doEmpty(MySQLSchema.java:49)
    at org.flywaydb.core.internal.dbsupport.Schema.empty(Schema.java:93)

Running mysql Ver 14.14 Distrib 5.6.26, for Linux (x86_64) on CentOS 7

@g0t4
Copy link

@g0t4 g0t4 commented Sep 17, 2015

Cannot proceed because system tables used by Event Scheduler were
found damaged at server start - seems like a different issue?

On Wed, Sep 16, 2015 at 5:07 PM, Diogo Monteiro notifications@github.com
wrote:

Having similar issues:

org.flywaydb.core.api.FlywayException: Unable to check whether schema sphereds is empty
at org.flywaydb.core.internal.dbsupport.Schema.empty(Schema.java:95)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:1021)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:1006)
at org.flywaydb.core.Flyway.execute(Flyway.java:1418)
at org.flywaydb.core.Flyway.migrate(Flyway.java:1006)
at com.clouddynamics.exosphere.platform.flyway.FlywayService.migrate(FlywayService.java:81)
at com.clouddynamics.exosphere.platform.common.init.ServiceContainerStartupManager.initializeResources(ServiceContainerStartupManager.java:34)
at com.clouddynamics.exosphere.platform.common.init.ExosphereServiceContainerListener.contextInitialized(ExosphereServiceContainerListener.java:35)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1738)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: Cannot proceed because system tables used by Event Scheduler were found damaged at server start
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1094)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4226)
at com.mysql.jdbc.MysqlIO.nextRowFast(MysqlIO.java:2188)
at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:2044)
at com.mysql.jdbc.MysqlIO.readSingleRowSet(MysqlIO.java:3538)
at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:489)
at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:3240)
at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:2411)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2834)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2840)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2212)
at org.flywaydb.core.internal.dbsupport.JdbcTemplate.queryForInt(JdbcTemplate.java:156)
at org.flywaydb.core.internal.dbsupport.mysql.MySQLSchema.doEmpty(MySQLSchema.java:49)
at org.flywaydb.core.internal.dbsupport.Schema.empty(Schema.java:93)

Running mysql Ver 14.14 Distrib 5.6.26, for Linux (x86_64) on CentOS 7


Reply to this email directly or view it on GitHub
#598 (comment).

@treaz
Copy link

@treaz treaz commented Oct 7, 2015

@g0t4 @diogogmt I don't think this is a flyway issue.
login to mysql on the server with the user that flyway uses and then do:
select count(*) form information_schema.events;

I bet that you'll be getting a

ERROR 1577 (HY000): Cannot proceed because system tables used by Event Scheduler were found damaged at server start

Then you need to fix your mysql server (I haven't found out how to do that yet). But there's a couple of links out there

@xiaofengli
Copy link

@xiaofengli xiaofengli commented Sep 3, 2016

I got the following exception, any idea? Is it database driver issue? thanks!

ERROR] Failed to execute goal org.flywaydb:flyway-maven-plugin:4.0.3:migrate (default-cli) on project app: org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource (jdbc:mysql://localhost:3306/) for user 'root': Could not create connection to database server. NullPointerException -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.flywaydb:flyway-maven-plugin:4.0.3:migrate (default-cli) on project app: org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource (jdbc:mysql://localhost:3306/) for user 'root': Could not create connection to database server.
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)

Here is what I have in my pom.xml,

org.flywaydb flyway-maven-plugin 4.0.3 jdbc:mysql://localhost:3306/ root 123456 db/migrations/words flyway_schema

I tried the following it works fine,

$ flyway migrate -url=jdbc:mysql://localhost -user=root -password=123456
Flyway 4.0.3 by Boxfuse

Database: jdbc:mysql://localhost:3306/ (MySQL 5.6)
Successfully validated 0 migrations (execution time 00:00.005s)
Creating schema flyway_schema ...
Creating Metadata table: flyway_schema.schema_version
Current version of schema flyway_schema: 0
Schema flyway_schema is up to date. No migration necessary.

@charneykaye
Copy link

@charneykaye charneykaye commented Feb 4, 2017

@treaz thanks that's a good observation.

@mauromol
Copy link

@mauromol mauromol commented Nov 13, 2017

I'm still having the "no database selected" problem with Flyway 4.2.0 and running Flyway through Gradle.
The flyway.url is set without the schema name, while flyway.schemas is set to the correct schema name.
This did not happen with Flyway 3.2.1.

I have to workaround this by putting the schema name in the URL, however I would prefer to avoid this.
Any suggestion?

@mauromol
Copy link

@mauromol mauromol commented Feb 9, 2018

I opened #1921 to track that the problem still exists for me.

@HenriquePereiraRosa
Copy link

@HenriquePereiraRosa HenriquePereiraRosa commented May 5, 2018

I'm having this problem. it's seems that the Flyway can't crate the schema in a new database. I don't create by my own because i don't remeber the default schema name of my project that Flyway creates.

-- POM:
<!-- FlayWay db Migrations --> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency>

-- Exception:

`

2018-05-05 16:03:07.660 ERROR 5172 --- [ restartedMain] o.s.boot.SpringApplication : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at com.example.BasicApplication.main(BasicApplication.java:10) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_162]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_162]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_162]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.7.RELEASE.jar:1.5.7.RELEASE]
Caused by: org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
at org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56) ~[flyway-core-3.2.1.jar:na]
at org.flywaydb.core.Flyway.execute(Flyway.java:1385) ~[flyway-core-3.2.1.jar:na]
at org.flywaydb.core.Flyway.migrate(Flyway.java:1006) ~[flyway-core-3.2.1.jar:na]
at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) ~[spring-boot-autoconfigure-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
... 23 common frames omitted
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
`
This is the first time that this problem has occurred (Windows 7). I've used in other two Windows 10 PCs and have worked fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet