Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction exception handling should rollback and throw exception #155

Open
dlmorais-13 opened this issue Nov 16, 2016 · 2 comments
Open

Comments

@dlmorais-13
Copy link

Hi everyone!

I was peeking at pixie code and stumbled on transaction code.
I think it should re-throw caught exceptions.
That's very important in order to do a decent error handling at most applications.

public function transaction(\Closure $callback)
{
        try {
            // Begin the PDO transaction
            $this->pdo->beginTransaction();
            // Get the Transaction class
            $transaction = $this->container->build('\\Pixie\\QueryBuilder\\Transaction', array($this->connection));
            // Call closure
            $callback($transaction);
            // If no errors have been thrown or the transaction wasn't completed within
            // the closure, commit the changes
            $this->pdo->commit();
            return $this;
        } catch (TransactionHaltException $e) {
            // Commit or rollback behavior has been handled in the closure, so exit
            return $this;
        } catch (\Exception $e) {
            // something happened, rollback changes
            $this->pdo->rollBack();
            
            // CHANGE THIS
            // return $this;
            // FOR THAT
            throw $e;
        }
}
@MarkoNV
Copy link

MarkoNV commented Mar 9, 2017

I agree, rolling back is not handling Exception and it should be rethrown so other code could handle it.
Alternative solution is to handle Exceptions inside the closure. If Exception occurs, rollback, handle it and throw TransactionHaltException to signal that transaction is already finished.

@TCB13
Copy link

TCB13 commented Nov 21, 2017

+1 on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants