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

External mongoclients can only be injected into MongoFlyway in a way that leads to NullPointerException #6

Open
jchu59 opened this issue Feb 3, 2017 · 3 comments

Comments

@jchu59
Copy link

jchu59 commented Feb 3, 2017

What version of Flyway are you using?

release-4.0.3-mongodb

Which client are you using? (Command-line, Java API, Maven plugin, Gradle plugin, SBT plugin, ANT tasks)

Java API

What database are you using (type & version)?

MongoDB 3.3

What operating system are you using?

OSX El Capitan

What did you do?

(Please include the content causing the issue, any relevant configuration settings, and the command you ran)
Run the info() and the migrate() commands on MongoFlyway.

What did you expect to see?

info and migrate

What did you see instead?

So the internal mongoclient closes its connection after any single command. So running info and migrate are two commands and that won't work. An external mongoclient will work. However when setting a mongoclient into MongoFlyway, you do not capture a databaseName. Therefore, when constructing the MongoMetaDataTable, an NPE is thrown and migration fails. Our workaround is to add an overloaded setMongoClient that adds a databaseName as a second argument.

@khatkar
Copy link

khatkar commented Feb 10, 2017

This is an expected behavior. If the MongoClient is not explicitly created by the user and provided to mongoFlyway using setMongoClient method, then mongoFlyway will create its own MongoClient which it will terminate after running a flyway command. If you want to run more than one flyway command, then you must provide your own MongoClient instance and flyway won't close it. However, in such a case, the user must take precaution to close the MongoClient instance to release mongo connections.

@jchu59
Copy link
Author

jchu59 commented Feb 14, 2017

I see now. If I set the 'flyway.mongoUri' config property, the configuration routine will internally call another overloaded setMongoClient method which will then instantiate the 'databaseName' instance variable which will be needed later on. It didn't make sense to me to set this property because I am supplying the MongoClient. There are problems with the way the code works. First, when I add my external MongoClient, it will replace the internal mongoClient, and that first-created internal MongoClient's connection will not close. Second, the 'createMongoClient' boolean used to keep track of whether to close the MongoClient after a command is executed will be true or false based on the order in which the user calls flyway.configure() and flyway.setMongoClient. This is fragile and will lead to frustrated users.

My workaround was to create in mongo-flyway an overloaded setMongoClient command that accepts a databaseName arg. Looks like a few lines of additional code might be wise here.

@khatkar
Copy link

khatkar commented Feb 14, 2017

Do only one of the things, either supply the 'flyway.mongoUri' property OR provide a MongoClient. You do not have to do both.

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

No branches or pull requests

2 participants