Repository navigation
Writes outside a transaction commit on their own: auto-transaction is now the default, set_auto_transaction(False) is the strict mode - #479
Conversation
… 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.
af5f9be to
81bb9cf
Compare
|
I rebased this onto I checked every place where #483 describes the old strict default. The New test: CI on the new head, with |
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 flagautoTransactionon.db.set_auto_transaction(False)brings back the strict mode that raisedTransaction not begun, and the newdb.is_auto_transaction()reads the flag.begin(),with db.transaction():,run_in_transaction,insert_manyandgraph_batchkeep their rules.The quickstart now writes with plain
db.commandcalls, 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) andgraph_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 buildsave()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 ofset_auto_transaction,is_auto_transaction,AsyncDatabase.commandand the nativesave(). 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 theenhancementlabel.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, calldb.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 useinsert_manyandgraph_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_batchrollback tests and one callback test); they set the strict mode too and check the new default as well. New tests are intests/test_auto_transaction_default.py(19 tests, JVM and native). Local runs against themainwheels 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
uvx pre-commit run --files <changed files>passes