Skip to content

Failed transaction commit can return a connection with an open native transaction to the pool #101

Open

Description

@virtanAnton

Description

When an outer managed transaction's COMMIT fails, SQLCipher Android can release its connection to the pool while SQLite still has the native transaction open. A later operation using that connection can observe changes from the failed transaction even though the managed session reports that it is no longer in a transaction.

Reproduction

Using a fresh, file-backed SQLiteDatabase with foreign keys enabled:

db.setForeignKeyConstraintsEnabled(true);
db.execSQL("CREATE TABLE parent(id INTEGER PRIMARY KEY)");
db.execSQL("CREATE TABLE child(id INTEGER REFERENCES parent(id) " +
        "DEFERRABLE INITIALLY DEFERRED)");

db.beginTransaction();
db.execSQL("INSERT INTO child VALUES (1)");
db.setTransactionSuccessful();
try {
    db.endTransaction(); // COMMIT fails: parent 1 does not exist
    throw new AssertionError("Expected a constraint exception");
} catch (android.database.sqlite.SQLiteConstraintException expected) {
    // db.inTransaction() is false here.
}

try (android.database.Cursor cursor = db.rawQuery(
        "SELECT count(*) FROM child", (String[]) null)) {
    cursor.moveToFirst();
    int count = cursor.getInt(0); // observed 1; expected 0
}

The issue can also affect a subsequent operation on another thread when it acquires the same pooled connection. Observing the row on that connection does not establish that the failed commit was durable.

Expected behavior

After the commit failure, a connection returned for reuse should not still expose an unfinished transaction. The original failure should be reported, and the connection or pool should be made safe before reuse.

Environment and evidence

  • SQLCipher Android net.zetetic:sqlcipher-android:4.17.0.
  • Reproduced on Pixel 8 and Pixel 9 API 36 ARM64 emulators with a deferred foreign-key violation.
  • A regression test and proposed recovery are in Recover pooled connections after failed transaction completion #100. The PR also covers cancellation, automatically rolled-back transactions, in-memory and temporary databases, and pool waiters.

A failed COMMIT does not by itself determine whether changes became durable; applications may need to reconcile the transaction's outcome separately.

Activity

  1. developernotes commented on Nov 4, 2026

    @developernotes
    Member

    Hello @virtanAnton,

    Thanks for bringing this to our attention, along with the pull request. We will investigate this further and reach back out when we have more to share.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions