From ab497c6bbe5b978c0882fd7d1208ba8755ad3616 Mon Sep 17 00:00:00 2001 From: Magnus Persson <1431685+magnusp@users.noreply.github.com> Date: Sat, 24 Oct 2026 20:31:37 +0200 Subject: [PATCH] fix(jdbc): read updateCount under the statement lock --- .../libsql/jdbc/LibsqlPreparedStatement.java | 2 +- .../github/magnusp/libsql/jdbc/LibsqlStatement.java | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/libsql-jdbc/src/main/java/com/github/magnusp/libsql/jdbc/LibsqlPreparedStatement.java b/libsql-jdbc/src/main/java/com/github/magnusp/libsql/jdbc/LibsqlPreparedStatement.java index 4496eac..2176622 100644 --- a/libsql-jdbc/src/main/java/com/github/magnusp/libsql/jdbc/LibsqlPreparedStatement.java +++ b/libsql-jdbc/src/main/java/com/github/magnusp/libsql/jdbc/LibsqlPreparedStatement.java @@ -100,7 +100,7 @@ public int executeUpdate() throws SQLException { throw new SQLException("Query returns results"); } execute(); - return (int) updateCount; + return snapshotUpdateCount(); } private static boolean returnsResultSet(String sql) { diff --git a/libsql-jdbc/src/main/java/com/github/magnusp/libsql/jdbc/LibsqlStatement.java b/libsql-jdbc/src/main/java/com/github/magnusp/libsql/jdbc/LibsqlStatement.java index 7fad62c..a479628 100644 --- a/libsql-jdbc/src/main/java/com/github/magnusp/libsql/jdbc/LibsqlStatement.java +++ b/libsql-jdbc/src/main/java/com/github/magnusp/libsql/jdbc/LibsqlStatement.java @@ -135,7 +135,18 @@ protected void assignResultSet( @Override public int executeUpdate(String sql) throws SQLException { execute(sql); - return (int) updateCount; + return snapshotUpdateCount(); + } + + /** + * Reads the update count under {@link #stateLock}, the lock every write to the field holds. + * The execute paths that return a count use this instead of reading the field directly, so the + * value they hand back has the same happens-before edge as {@link #getUpdateCount()}. + */ + protected int snapshotUpdateCount() { + synchronized (stateLock) { + return (int) updateCount; + } } @Override