Repository navigation
Description
Problem
The JDBC driver executes every auto-commit statement as a one-shot Hrana request: baton: null, execute, close. The connection never owns a session, so three SQLite behaviours break at once.
last_insert_rowid() reads 0. Hibernate's SQLite GenerationType.IDENTITY and jOOQ's identity reads issue select last_insert_rowid() as a separate statement after the insert. On a fresh stream sqld has no insert to report, so the query returns 0. The integration suites work around this by forcing transactions (HibernateSqldSupport sets hibernate.connection.autocommit=false) or by pinning a null identity (JooqIT.autoCommitInsertDoesNotExposeGeneratedIdentity).
Session state does not survive a statement. PRAGMA foreign_keys = OFF sent as one auto-commit statement is back on for the next statement. FlywayKnownGapsIT.autoCommitStatementsDoNotShareSessionState pins this.
Flyway clean() cannot run. sqld rejects a string containing more than one statement. Flyway's SQLite plugin sends each table drop as one string:
PRAGMA foreign_keys = OFF; DROP TABLE "main"."author"; PRAGMA foreign_keys = ONso clean stops at the first table. Splitting alone is not enough: the PRAGMA must apply to the DROP, which requires both to run on the same stream. FlywayKnownGapsIT.cleanIsBlockedUntilMultiStatementSupportLands pins the failure.
These are the same root cause. One-shot execution was a transport optimisation, not a JDBC semantic, and it made the connection less SQLite-like than the protocol requires.
Decision
Stop using one-shot execution for JDBC statements.
An auto-commit connection owns one lazily opened interactive Hrana stream and pipelines every statement on it, the same stream machinery explicit transactions already use. Multi-statement input is split into individual statements and sent in order on that stream as one batch, so the statements cannot land on different streams and a failure stops the script.
The goal is least surprise: a JDBC connection behaves like a SQLite connection. last_insert_rowid(), PRAGMAs, and other session state are answered by sqld on the connection's stream. The driver does not synthesise a rowid, cache one client-side, or special-case Flyway's script. Statement.getGeneratedKeys() keeps working from StmtResult.last_insert_rowid, unchanged.
This reverses the contract recorded under #83/#126. #83 stays closed because the limitation is removed, not because it was acceptable. The workarounds it produced are in scope to delete: the Hibernate suite should run with auto-commit on, and the jOOQ auto-commit identity pin should flip to the expected identity.
LibsqlClient.executeOneShot / executeBatchOneShot may remain for non-JDBC callers that explicitly want a throwaway stream. The JDBC driver must not call them; delete them in the same change if nothing else uses them.
Required behaviour
- Stream lifetime. The auto-commit stream opens lazily on the first statement and stays open between auto-commit statements.
Connection.close()closes it. Opening a connection or setting auto-commit sends nothing. - Transaction state must stop being derived from the stream. Today
beginPending()is!autoCommit && !client.hasOpenStream(). Once auto-commit holds a stream, that check skipsBEGIN. Track whether a transaction is active explicitly, or close and reopen the stream on auto-commit transitions. Do not let a transaction run withoutBEGIN. - Idle expiry is recoverable in auto-commit. sqld expires idle streams (documented around 10 s). If the server rejects the baton, drop it and retry the statement once on a fresh stream. Auto-commit has no transaction to lose. Session state set before the expiry is gone; document that instead of silently pretending it survived. This deliberately reopens part of Non-auto-commit connections hold an idle Hrana stream that sqld expires #99's tradeoff for auto-commit; manual transactions keep their current stale-baton handling and tests.
- Multi-statement splitting must follow SQLite lexical rules: semicolons inside
'...',"...",`...`,[...],--and/* */comments, and trigger bodies (CREATE TRIGGER ... BEGIN ...; ... END;) do not end a statement. Ignore empty statements and trailing semicolons. - Parameters. Flyway's clean script is parameterless. Multi-statement input containing
?placeholders cannot be bound unambiguously, so reject it with a clearSQLFeatureNotSupportedExceptionrather than binding the same argument list to every statement. Single-statement parameter behaviour does not change. - Updates and results. Execute the script in order and stop at the first failing statement, throwing that statement's mapped
SQLException; statements before it have run, later ones have not. Match xerial's aggregate update count:executeUpdate("insert ...; insert ...")returns the total affected rows (2), which is the expectation currently gated as control-only in the conformanceStatementTest. A script that produces a result set exposes the final statement's result; intermediate results are discarded, andDatabaseMetaData.supportsMultipleResultSets()staysfalse. - Single statements keep today's execution, error mapping, generated keys, and statement lifecycle behaviour.
Documentation
- Replace the "Auto-commit holds no stream" section in
libsql-conformance/README.mdwith the connection-scoped stream contract: session state survives auto-commit statements on the same connection, state does not cross connections, and idle expiry resets it. - Remove the
last_insert_rowid()workaround guidance that tells users to usegetGeneratedKeys()or an explicit transaction instead of the direct query. - Update the Hibernate and jOOQ comments that describe the old limitation.
- Document the pool tradeoff: each open JDBC connection now holds one stream. A pool larger than sqld's stream limit can hit that limit, just as idle non-auto-commit connections already can.
Acceptance
flyway.clean()drops every user table and the schema history. The known-gap test is replaced by a passing clean test.autoCommitStatementsDoNotShareSessionStateflips to a passing pin thatPRAGMA foreign_keysset on one auto-commit statement is visible to the next statement on the same connection.- New integration pin: an auto-commit
INSERTfollowed bySELECT last_insert_rowid()on the same connection returns the inserted rowid. - Hibernate identity generation passes with
hibernate.connection.autocommitleft at its default, and the jOOQ auto-commit identity test expects the generated value instead of null and 0. - The multi-statement
executeUpdatecase in the xerialStatementTestis un-gated and passes; splitter unit tests cover quotes, comments, trigger bodies, trailing semicolons, empty statements, and the placeholder rejection. - An idle-expiry integration test waits past the sqld stream timeout, then proves an auto-commit statement succeeds on a fresh stream and that the documented session-state reset is what the test observes.
./mvnw -pl libsql-jdbc -am verifyand./mvnw spotless:checkare green.
Why not the narrower options
- Special-casing
PRAGMA ...; DROP ...; PRAGMA ...fixes Flyway only and leaves the Hibernate, jOOQ, and general session-state surprises in place. - Caching or synthesising
last_insert_rowidin the driver would duplicate SQLite semantics across a remote connection and still be wrong after a rollback, a different connection, or an expired stream. - Keeping one-shot execution and asking every caller to wrap related statements in a transaction is exactly the surprise this issue removes.
Current behaviour pinned by
libsql-jdbc/src/test/java/io/github/magnusp/libsql/jdbc/flyway/FlywayKnownGapsIT.java:cleanIsBlockedUntilMultiStatementSupportLandsandautoCommitStatementsDoNotShareSessionState.libsql-jdbc/src/test/java/io/github/magnusp/libsql/jdbc/hibernate/HibernateSqldSupport.java:hibernate.connection.autocommit=falseand its issue last_insert_rowid() in auto-commit reads a fresh Hrana session #83 comment.libsql-jdbc/src/test/java/io/github/magnusp/libsql/jdbc/jooq/JooqIT.java:autoCommitInsertDoesNotExposeGeneratedIdentity.upstream-xerial_sqlite-jdbc/src/test/java/org/sqlite/StatementTest.java: theexecuteUpdatemulti-statement case gated behind!isLibsql().
Activity
test(jdbc): add Flyway integration smoke tests (#259)
[-]Support Flyway clean(): accept multi-statement PRAGMA/DROP scripts[/-][+]Keep a connection-scoped Hrana stream for auto-commit: fix last_insert_rowid() and Flyway clean()[/+]fix(jdbc): keep one connection-scoped Hrana stream for auto-commit (#270
docs: document the Flyway bootstrap for jdbc:libsql URLs (#271)