
niedz., 7 wrz 2025 o 22:59 Maximilian Riemensberger via Boost < boost@lists.boost.org> napisaĆ(a):
* Transaction scoping rules: The scoping rules around transations, statements and resultset are suprising. The following example demonstrates that:
``` { sqlite::transaction t{conn}; // use a transaction to speed this up
auto st1 = conn.prepare(R"(insert into libitems (length, content_type) values ($length, $content_type) returning id)");
auto st2 = conn.prepare(R"(insert into libpaths (path, libitem) values ($path, $libitem))");
//{ // uncomment this scope to make it work auto res = st1.execute<rowid>({{"length", 123}, {"content_type", "application/octet-stream"}});
rowid id = res.current();
st2.execute({{"path", "lib/path/to/file"}, {"libitem", id.id}}); //}
t.commit(); } ```
The requirement of this extra scope around the resultset was quite surprising to me. Nowhere in the library documentation is this behavior explained. I would much rather prefer the scope of the transaction itself sort out the order of destruction to make sure it commits correctly without extra nested scopes. If that's not possible, at least the library documentation needs to make the user aware of those subtleties.
Would it make sense to offer an alternative interface for the transactions in this library? conn.transaction([&](sqlite::connection& conn) { auto st1 = conn.prepare(R"(insert into libitems (length, content_type) values ($length, $content_type) returning id)"); auto st2 = conn.prepare(R"(insert into libpaths (path, libitem) values ($path, $libitem))"); { auto res = st1.execute<rowid>({{"length", 123}, {"content_type", "application/octet-stream"}}); rowid id = res.current(); st2.execute({{"path", "lib/path/to/file"}, {"libitem", id.id}}); } }); This way, no explicit `commit` instruction is necessary: the implementation of member function transaction() can have it at the end of the scope. There is no way to forget the commit upon successful execution of the instructions. This would also correctly roll back a transaction upon any exception. Regards, &rzej;