From 07b81ebd2ddb6d0d42b047b93f3c4f23549cc9ea Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sun, 8 Nov 2009 21:19:11 +0000 Subject: [PATCH] MDL-20625 coding help - better detection of missing commit or rollback --- lib/dml/moodle_database.php | 7 ++++++- lib/dml/moodle_transaction.php | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 0e178d080e..0da533897f 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -285,10 +285,15 @@ abstract class moodle_database { */ public function dispose() { if ($this->transactions) { + // this should not happen, it isually indicates wrong catching of exceptions, + // because all transactions should be finished manually or in default exception hadnler. // unfortunately we can not access global $CFG any more and can not print debug, // the diagnostic info should be printed in footer instead + $lowesttransaction = end($this->transactions); + $backtrace = $lowesttransaction->get_backtrace(); + + error_log('Potential coding error - active database transaction detected when disposing database:'."\n".format_backtrace($backtrace, true)); $this->force_transaction_rollback(); - error_log('Active database transaction detected when disposing database!'); } if ($this->used_for_db_sessions) { // this is needed because we need to save session to db before closing it diff --git a/lib/dml/moodle_transaction.php b/lib/dml/moodle_transaction.php index 37d252e290..75fc132b9e 100644 --- a/lib/dml/moodle_transaction.php +++ b/lib/dml/moodle_transaction.php @@ -41,6 +41,15 @@ class moodle_transaction { public function __construct($database) { $this->database = $database; $this->start_backtrace = debug_backtrace(); + array_shift($this->start_backtrace); + } + + /** + * Returns backtrace of the code starting exception. + * @return array + */ + public function get_backtrace() { + return $this->start_backtrace; } /** -- 2.39.5