Skip to content

Add auto-transaction support? #156

Open
@schveiguy

Description

@schveiguy

When writing to the database, I'm finding myself copy-pasting these 3 lines:

conn.exec("START TRANSACTION");
scope(failure) conn.exec("ROLLBACK");
scope(success) conn.exec("COMMIT");

I'm wondering if there's a way to do this in a one-liner, something like:

auto transaction = conn.scopedTransaction(); // please bikeshed this

Which automatically does the right thing on destruction. What I don't know, exactly, is how to deal with the scope statements. I don't think there's a way to see if an exception is in-flight in the destructor.

It may be possible to do this:

auto transaction = conn.scopedTransaction();
scope(failure) transaction.rollback(); //  also disables the commit on destructor.

But I don't love it -- if you have a scope failure, you likely always want to rollback, which won't happen if you forget the scope statement.

Possibly a mixin could help, but that's a bit ugly.

Another nice feature we could provide with such a construct -- reentrant transactions. In other words, you can nest transaction statements, and only the outer scope will start and commit/rollback.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions