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