tursodatabase/turso · error · SQLException

Cannot commit in autocommit mode.

Error message

Cannot commit in autocommit mode.

What it means

commit() requires autoCommit to be false; calling it while the connection is still in the default autocommit mode throws immediately. The driver starts transactions lazily: after setAutoCommit(false), the first prepared non-control statement issues BEGIN, and commit() then runs COMMIT. Committing in autocommit mode is meaningless, hence the guard.

Solutions

  1. Call conn.setAutoCommit(false) before the work and the commit
  2. Guard the commit: `if (!conn.getAutoCommit()) conn.commit();`
  3. With pools, ensure the borrowed connection's autoCommit state matches what the code expects

Example fix

// before
PreparedStatement ps = conn.prepareStatement("UPDATE ...");
ps.executeUpdate();
conn.commit(); // throws: still autocommit

// after
conn.setAutoCommit(false);
PreparedStatement ps = conn.prepareStatement("UPDATE ...");
ps.executeUpdate();
conn.commit();
Defensive patterns

Strategy: validation

Validate before calling

// only commit connections actually taken out of autocommit
if (!conn.getAutoCommit()) {
  conn.commit();
}

Try / catch

try {
  conn.commit();
} catch (SQLException e) {
  if ("Cannot commit in autocommit mode.".equals(e.getMessage())) return; // nothing to commit
  throw e;
}

Prevention

When it happens

Trigger: Calling connection.commit() without a prior setAutoCommit(false); toggling setAutoCommit(true) (which commits any pending work) and then calling commit() anyway; code that assumes an implicit transaction is always open.

Common situations: Copy-pasted transaction boilerplate missing the setAutoCommit(false) line; connection pools that reset autoCommit to true on return; branching code where one path re-enables autocommit before a shared commit() call.

Related errors


AI-assisted analysis of tursodatabase/turso@244cde92a7 (2026-08-20). Data as JSON: /api/errors/e742ecfddafbb0ee. Report an issue: GitHub.

Appendix: source

Thrown at bindings/java/src/main/java/tech/turso/core/TursoConnection.java:220

   *
   * @return true if auto-commit mode is enabled; false otherwise
   * @throws SQLException if a database access error occurs or the connection is closed
   */
  public boolean getAutoCommit() throws SQLException {
    checkOpen();
    return autoCommit;
  }

  /**
   * Commits the current transaction.
   *
   * @throws SQLException if in auto-commit mode, closed, or database error occurs.
   */
  public void commit() throws SQLException {
    synchronized (transactionLock) {
      checkOpen();
      if (autoCommit) {
        throw new SQLException("Cannot commit in autocommit mode.");
      }

      if (inTransaction) {
        executeInternal("COMMIT");
        inTransaction = false;
      }
    }
  }

  /**
   * Rolls back the current transaction.
   *
   * @throws SQLException if in auto-commit mode, closed, or database error occurs.
   */
  public void rollback() throws SQLException {
    synchronized (transactionLock) {
      checkOpen();
      if (autoCommit) {

View on GitHub (pinned to 244cde92a7)