From 25d854c67c3ef31d9d9fbbb0549f73d470857825 Mon Sep 17 00:00:00 2001 From: skodak Date: Sun, 25 May 2008 19:56:34 +0000 Subject: [PATCH] MDL-14910 added option to dispose existing database instance (closes connection and releases all resources) --- lib/ddl/database_manager.php | 11 +++++++++++ lib/ddl/sql_generator.php | 7 +++++++ lib/dml/adodb_moodle_database.php | 20 +++++++++++++------- lib/dml/moodle_database.php | 13 +++++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/ddl/database_manager.php b/lib/ddl/database_manager.php index 67451235e7..1b2d140be7 100644 --- a/lib/ddl/database_manager.php +++ b/lib/ddl/database_manager.php @@ -46,6 +46,17 @@ class database_manager { $this->generator = $generator; } + /** + * Release all resources + */ + public function dispose() { + if ($this->generator) { + $this->generator->dispose(); + $this->generator = null; + } + $this->mdb = null; + } + /** * This function will execute an array of SQL commands, returning * true/false if any error is found and stopping/continue as desired. diff --git a/lib/ddl/sql_generator.php b/lib/ddl/sql_generator.php index 67b2ef29f3..c2b37404a9 100644 --- a/lib/ddl/sql_generator.php +++ b/lib/ddl/sql_generator.php @@ -135,6 +135,13 @@ abstract class sql_generator { $this->mdb = $mdb; // this creates circular reference - the other link must be unset when closing db } + /** + * Release all resources + */ + public function dispose() { + $this->mdb = null; + } + /** * Given one string (or one array), ends it with statement_end */ diff --git a/lib/dml/adodb_moodle_database.php b/lib/dml/adodb_moodle_database.php index ff2b5532c3..49359ced51 100644 --- a/lib/dml/adodb_moodle_database.php +++ b/lib/dml/adodb_moodle_database.php @@ -39,13 +39,10 @@ abstract class adodb_moodle_database extends moodle_database { return $cfg; } - //TODO: preconfigure_dbconnection(): Decide if this should be declared as abstract because all adodb drivers will need it /** * Adodb preconnection routines, ususally sets up needed defines; */ - protected function preconfigure_dbconnection() { - // empty - } + protected abstract function preconfigure_dbconnection(); public function connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null) { $this->dbhost = $dbhost; @@ -83,12 +80,21 @@ abstract class adodb_moodle_database extends moodle_database { return true; } - //TODO: configure_dbconnection(): Decide if this should be declared as abstract because all adodb drivers will need it /** * Adodb post connection routines, usually sets up encoding,e tc. */ - protected function configure_dbconnection() { - // empty + protected abstract function configure_dbconnection(); + + /** + * Close database connection and release all resources + * and memory (especially circular memory references). + * Do NOT use connect() again, create a new instance if needed. + */ + public function dispose() { + if ($this->db) { + $this->db->Close(); + } + parent::dispose(); } //TODO: make all dblibraries return this info in a structured way (new server_info class or so, like database_column_info class) diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index cbcd8d877f..9df73d679c 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -106,6 +106,19 @@ abstract class moodle_database { */ public abstract function connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null); + /** + * Close database connection and release all resources + * and memory (especially circular memory references). + * Do NOT use connect() again, create a new instance if needed. + */ + public function dispose() { + if ($this->database_manager) { + $this->database_manager->dispose(); + $this->database_manager = null; + } + $this->columns = array(); + } + /** * Returns database server info array * @return array -- 2.39.5