Hi Erik,

On Wed, 28 Oct 2015 16:28:59 +0300, Erik van Velzen <e...@evanv.nl> wrote:

Thanks for your input.

I did not try-catch because it doesn't really work, you either
duplicate rollback code or get excessive nesting. Real code will
obviously be more complicated than the examples.
Also in both cases the handlers are many lines apart from the actual
transaction call.

Duplicate rollback code:

    try {
        transactionOne();
    }
    catch (\Throwable $e) {
       rollbackTransactionOne();
       throw $e;
    }

    try {
        transactionTwo();
    }
    catch (\Throwable $e) {
        rollbackTransactionOne();
        rollbackTransactionTwo();
        throw $e;
    }
    finally {
        cleanupTransactionTwo();
    }

    try {
       transactionThree();
    }
    catch (\Throwable $e) {
        rollbackTransactionOne();
        rollbackTransactionTwo();
        rollbackTransactionThree();
        throw $e;
    }

    logTransactionOne();
    logTransactionTwo();
    logTransactionThree();

In C goto is usually used for this kind of problem, this is how it's usually done:

https://gist.github.com/nikita2206/dc48c9f14db61420751e

here's a first article I could find about it http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c

What you could also use though is a generator function:

https://gist.github.com/nikita2206/64959e63ebb49d6ce0c8

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to