Skip to content

Doom transactions on critical errors in Repository as fail-safe #1244

@enkore

Description

@enkore

Doom(able|ed) transactions are a concept a few databases have to guard against wrong error handling but also allow easy transaction control / abortion. We would only use it as a fail-safe against incorrect error handling and also "strange" issues with networking should they occur.

It could look like this:

# Sketch code, not fleshed out, certainly not tested.

class Repository:
    @staticmethod
    def exception_dooms(whitelist=(,)):
        """All exceptions not on *whitelist* raised in the decorated method will doom the current transaction."""
        def decorator(method):
            def meth_wrapper(self, *args, **kwargs):
                try:
                    return method(self, ...)
                except Exception as exc:
                    if type(exc) not in whitelist:
                        self.doom_transaction(exc)
                    raise
            return meth_wrapper
        return decorator

    def doom_transaction(self, exception=None):
        self.transaction_doomed = True
        self.transaction_doomed_by = exception

    def begin_transaction(self):
        ...
        self.transaction_doomed = False

    @exception_dooms()
    def write_stuff(self, ...):
        # do something
        if bad:
            raise OSError

    @exception_dooms(whitelist=(ObjectNotFound,))
    def get(self, id):
        ...

    def commit(self):
        if self.transaction_doomed:
            log_exception(self.transaction_doomed_by)
            assert not self.transaction_doomed, 'Tried to commit doomed transaction'
         ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions