Skip to content

Writes outside a transaction commit on their own: auto-transaction is now the default, set_auto_transaction(False) is the strict mode - #479

Merged
tae898 merged 2 commits into
mainfrom
feat/478-python-auto-transaction
Nov 24, 2026
Merged

tae898 merged 2 commits into
mainfrom
feat/478-python-auto-transaction

Conversation

@tae898

@tae898 tae898 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

A statement outside a transaction now runs in a transaction of its own and commits when it ends, in both packages. Every database that the bindings hand out (create_database, open_database, DatabaseFactory, and the database of an embedded server) has the engine flag autoTransaction on. db.set_auto_transaction(False) brings back the strict mode that raised Transaction not begun, and the new db.is_auto_transaction() reads the flag. begin(), with db.transaction():, run_in_transaction, insert_many and graph_batch keep their rules.

The quickstart now writes with plain db.command calls, shows a transaction for statements that belong together, and shows the strict mode as the way to get the error. The transactions page has a new section "What a commit costs" with the numbers, and the pages that said "writes need a transaction" (concurrency, asyncio, values and records, README) are updated.

Related issues

Closes #478

The gaps I found while auditing 23 libraries are in #474, #475, #476 and #477. The pre-release risk list is in the issue.

Performance changes

The change claims no speed difference. I measured the batch calls with the flag off against on, on both builds, 11 interleaved pairs for each of insert_many (50,000 documents) and graph_batch (20,000 vertices and 40,000 edges), and an off against off control: the median ratio on over off is between 0.97 and 1.01, the control between 0.984 and 1.041 (laptop, hot, noisy, not a claim). On the native build save() of a saved record no longer asks for the transaction state, one call into the library less.

The cost of the default is a loop of single writes: one commit per row, the same as a loop with a transaction around each statement. The transactions page gives the table (default WAL level 3 to 5 times slower than one transaction, hundreds of times with a flush level that waits for the disk).

Docs

README.md, quickstart.adoc, how-to/transactions.adoc (new sections "Outside a transaction", "Strict mode" and "What a commit costs"), how-to/concurrency.adoc, how-to/asyncio.adoc, how-to/values-and-records.adoc, and the docstrings of set_auto_transaction, is_auto_transaction, AsyncDatabase.command and the native save(). The Antora build has no warning, and both quickstart scripts print what the page shows on both builds. There is no CHANGELOG file: the release notes come from this title and the enhancement label.

Release note (draft)

Writes outside a transaction commit on their own: auto-transaction is now on by default in both packages. Code that put every write in with db.transaction(): is unchanged. To get the old strict behavior, call db.set_auto_transaction(False) after opening a database. A loop of many single writes pays one commit per row, so keep it inside one transaction or use insert_many and graph_batch.

Tests

Nine existing tests asserted the strict default and now set the strict mode. The full suite found three more that used the bare-write error as a proxy (two graph_batch rollback tests and one callback test); they set the strict mode too and check the new default as well. New tests are in tests/test_auto_transaction_default.py (19 tests, JVM and native). Local runs against the main wheels of 24 November with these Python files over them, on the final commit: the whole suite on the JVM wheel (4088 passed, 2 warnings) and on the native wheel (4144 passed, 113 skipped, 3 warnings; the skips are the counted NOTYET and NA-JVM kinds). CI builds its own wheels and runs the parity jobs.

Checklist

  • A regression test fails on the old code and passes on the new
  • The test suite that the change can affect passes (locally on both builds, CI to confirm)
  • uvx pre-commit run --files <changed files> passes

… the default (closes #478)

Every database that the bindings hand out (create_database, open_database,
DatabaseFactory and the database of an embedded server, JVM and native) now has
the engine flag autoTransaction on. set_auto_transaction(False) is the strict
mode that raised "Transaction not begun"; is_auto_transaction() reads the flag.

Why: of 23 Python database libraries that were run or read, none raises on a
bare write (15 commit at once, 7 open a transaction on the first statement,
LMDB has no write outside a transaction). The first write of the quickstart
failed.

Every bare write raised before, so no working program changes. Checked on both
builds: begin() is not committed for you, nested blocks, a failed statement
(whole statement undone), SQLScript with and without BEGIN, query(), save() and
delete(), insert_many and graph_batch (ratio on/off 0.97 to 1.01 over 11
interleaved pairs, control 0.98 to 1.04), threads, the asyncio facade, a
read-only database, the embedded server.

Tests that asserted the strict default now set strict mode (9 tests); three
more were found only by the full run (two graph_batch rollback tests and a
callback test that used the bare-write error as proof that no transaction was
left open). New tests in tests/test_auto_transaction_default.py.

The cost is a loop of single writes: one commit per row, the same as a
transaction around each row. Numbers and the way out are on the transactions
page.
…uto-transaction on (#478)

The stale-record check of save() is a value comparison (native) or a version check (JVM) and does not depend on a transaction, so a
bare save() by writer B commits alone and writer A, who read the record earlier, must still get ConcurrentModificationException and
write nothing. The test runs on both builds; the window between the check and the write of a bare native save() is not closed by
auto-transaction and stays documented in concurrency.adoc.
@tae898 tae898 added the ci:full Run the full platform matrix on this pull request (the legs of a push to main) label Nov 24, 2026
@tae898
tae898 force-pushed the feat/478-python-auto-transaction branch from af5f9be to 81bb9cf Compare November 24, 2026 17:33
@tae898
tae898 merged commit edf6786 into main Nov 24, 2026
71 checks passed
@tae898
tae898 deleted the feat/478-python-auto-transaction branch November 24, 2026 20:20
@tae898

tae898 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

I rebased this onto main (head 81bb9cf). Three conflicts: README.md (kept the open_or_create_database sentence from #480 and this PR's transaction sentence), quickstart.adoc (the strict-mode output is now the #483 TransactionError line with its code and hint, after the Total people: 4 line of this PR, and both explanatory paragraphs stay), and src/arcadedb/core.py (_opened and __enter__ both stay, with the type hints of #481 on _opened).

I checked every place where #483 describes the old strict default. The transaction_not_begun hint is unchanged on purpose: #483 already pins it as true with auto-transaction on and off (test_the_hint_for_a_missing_transaction_is_true_with_auto_transaction_on_and_off), and it is what a user sees in strict mode or after a commit() with nothing open. The #483 tests already start that situation with set_auto_transaction(False) or a bare commit(), so errors.adoc, test_error_codes_*.py, test_native_tx_crossings.py and test_docs_errors_page.py needed no change.

New test: test_two_writers_with_a_bare_save_the_stale_one_is_refused_with_the_default_on in tests/test_auto_transaction_default.py (both builds). Writer B saves a record with no transaction of its own from a second thread and commits alone, then writer A, who read the record earlier, gets ConcurrentModificationException from a bare save() and nothing is written.

CI on the new head, with ci:full added: 72 of 72 checks succeeded, including the macOS and Windows legs (tests, parity, examples, wheel builds).

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

Labels

ci:full Run the full platform matrix on this pull request (the legs of a push to main) concurrency Threads, transactions and conflicts enhancement New feature or request jvm The arcadedb-jvm package (JPype build) native The arcadedb package (native library build)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A write outside a transaction should commit on its own by default, with strict mode as an opt-in

1 participant