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

[5.2][Database][Question] Commit method can cause negative transactionLevel #12382

Closed
marella opened this issue Feb 19, 2016 · 2 comments
Closed

Comments

@marella
Copy link

marella commented Feb 19, 2016

Similar to #6360 but issue is because of commit method.
Calling commit method first will make transactionLevel go below 0.

DB::commit();
echo DB::transactionLevel(); // -1

Now calling DB::transaction() will not beginTransaction as transactionLevel is 0 but all changes are updated in database and rollBack is not possible.

DB::transaction(function() {
    echo DB::transactionLevel(); // 0
    DB::delete('DELETE FROM table_name');
    throw new Exception('foo'); // no rollBack happens
});

Shouldn't the commit method have a check on transactions like rollBack method?

--$this->transactions; // instead of this
$this->transactions = max(0, $this->transactions - 1); // use this

Or throw exception when trying to commit when there is no transaction?

if ($this->transactions === 0) {
    throw new RuntimeException('There is no active transaction');
}
@GrahamCampbell
Copy link
Member

$this->transactions = max(0, $this->transactions - 1); // use this

No, that's just a hack that's not actually fixing the real issue.

@marella
Copy link
Author

marella commented Feb 20, 2016

Wouldn't it be better to throw an exception when commit and rollBack methods are called with 0 transactionLevel?

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

2 participants