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

Sqlite locks when attempting a migration result in SQLException: [SQLITE_BUSY] #1499

taufiqkh opened this issue Dec 30, 2016 · 4 comments


Copy link

What version of Flyway are you using?


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)?

SQLite Xerial JDBC driver

What operating system are you using?

Windows 10 64-bit

What did you do?

(Please include the content causing the issue, any relevant configuration settings, and the command you ran)
Attempted a migration that included table creation, using a flyway object with DataSource set to preconfigured org.sqlite.SQLiteDataSource. No other processes using the database as far as I am aware:

        SQLiteConfig config = new SQLiteConfig();
        SQLiteDataSource dataSource = new SQLiteDataSource(config);
        dataSource.setUrl("jdbc:sqlite:" + DB_NAME);
        Flyway flyway = new Flyway();

Test migration script V2__test.sql:

What did you expect to see?

Successful migration to v2

What did you see instead?

The following exception:

Exception in thread "main" org.flywaydb.core.api.FlywayException: Unable to insert row for version '2' in metadata table "main"."schema_version"
	at org.flywaydb.core.internal.metadatatable.MetaDataTableImpl.addAppliedMigration(
	at org.flywaydb.core.internal.command.DbMigrate.applyMigration(
	at org.flywaydb.core.internal.command.DbMigrate.access$1000(
	at org.flywaydb.core.internal.command.DbMigrate$2.doInTransaction(
	at org.flywaydb.core.internal.command.DbMigrate$2.doInTransaction(
	at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(
	at org.flywaydb.core.internal.command.DbMigrate.migrate(
	at org.flywaydb.core.Flyway$1.execute(
	at org.flywaydb.core.Flyway$1.execute(
	at org.flywaydb.core.Flyway.execute(
	at org.flywaydb.core.Flyway.migrate(
	at com.quiptiq.flyway.ExampleFlyway.attemptMigration(
	at com.quiptiq.flyway.ExampleFlyway.main(
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at com.intellij.rt.execution.application.AppMain.main(
Caused by: java.sql.SQLException: [SQLITE_BUSY]  The database file is locked (database is locked)
	at org.sqlite.core.DB.newSQLException(
	at org.sqlite.core.DB.newSQLException(
	at org.sqlite.core.DB.execute(
	at org.sqlite.core.DB.executeUpdate(
	at org.sqlite.jdbc3.JDBC3PreparedStatement.executeUpdate(
	at org.flywaydb.core.internal.dbsupport.JdbcTemplate.update(
	at org.flywaydb.core.internal.metadatatable.MetaDataTableImpl.addAppliedMigration(
	... 17 more
Copy link

Created repository containing an example.

Copy link

Possibly related to #1318 and #1180. As a workaround, I tried creating a wrapper DataSource that uses an underlying SQLiteDataSource but returns the same connection each time getConnection() is called unless that connection is closed. The migration appears to proceed as expected when this wrapper is provided, though I'm not familiar enough with Flyway to know if it will have any side effects. This is not thread safe but seems fine otherwise. Sample:

public class WorkaroundDataSource implements DataSource {
    private final SQLiteDataSource dataSource;

    private Connection connection;

    public WorkaroundDataSource(SQLiteDataSource dataSource) {
        this.dataSource = dataSource;

    public Connection getConnection() throws SQLException {
        if (connection == null || connection.isClosed()) {
            this.connection = dataSource.getConnection();
        return connection;

    // other methods passed through to dataSource
    // ...

@axelfontaine axelfontaine added this to the Flyway 4.1 milestone Jan 25, 2017
axelfontaine pushed a commit to flyway/ that referenced this issue Jan 25, 2017
Copy link

Thanks for the investigation. Truly appreciated!

We used to only force the use of a single connection when using our own datasource via flyway.setDataSource(url, user, pwd).

We have now fixed this to work with any datasource.

Copy link

No worries, workaround will do us for now but looking forward to the next update.

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

No branches or pull requests

2 participants