Skip to content
Browse files

Merge pull request #1189 from robschley/disconnect

Added JDatabaseDriver::disconnect() to support disconnecting from the database cleanly.
  • Loading branch information...
2 parents e27be38 + 836bb46 commit 1ef2ad6b6d2b230105e4a3aa5ca043e122f2d275 @pasamio pasamio committed
View
9 libraries/joomla/database/driver.php
@@ -397,6 +397,15 @@ public function __construct($options)
abstract public function connected();
/**
+ * Disconnects the database.
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ abstract public function disconnect();
+
+ /**
* Drops a table from the database.
*
* @param string $table The name of the database table to drop.
View
29 libraries/joomla/database/driver/mysql.php
@@ -74,6 +74,19 @@ public function __construct($options)
}
/**
+ * Destructor.
+ *
+ * @since 12.1
+ */
+ public function __destruct()
+ {
+ if (is_resource($this->connection))
+ {
+ mysql_close($this->connection);
+ }
+ }
+
+ /**
* Connects to the database if needed.
*
* @return void Returns void if the database connected successfully.
@@ -114,16 +127,18 @@ public function connect()
}
/**
- * Destructor.
+ * Disconnects the database.
+ *
+ * @return void
*
* @since 12.1
*/
- public function __destruct()
+ public function disconnect()
{
- if (is_resource($this->connection))
- {
- mysql_close($this->connection);
- }
+ // Close the connection.
+ mysql_close($this->connection);
+
+ $this->connection = null;
}
/**
@@ -173,7 +188,7 @@ public function connected()
{
if (is_resource($this->connection))
{
- return mysql_ping($this->connection);
+ return @mysql_ping($this->connection);
}
return false;
View
22 libraries/joomla/database/driver/mysqli.php
@@ -50,6 +50,19 @@ public function __construct($options)
}
/**
+ * Destructor.
+ *
+ * @since 12.1
+ */
+ public function __destruct()
+ {
+ if (is_callable($this->connection, 'close'))
+ {
+ mysqli_close($this->connection);
+ }
+ }
+
+ /**
* Connects to the database if needed.
*
* @return void Returns void if the database connected successfully.
@@ -121,16 +134,21 @@ public function connect()
}
/**
- * Destructor.
+ * Disconnects the database.
+ *
+ * @return void
*
* @since 12.1
*/
- public function __destruct()
+ public function disconnect()
{
+ // Close the connection.
if (is_callable($this->connection, 'close'))
{
mysqli_close($this->connection);
}
+
+ $this->connection = null;
}
/**
View
18 libraries/joomla/database/driver/oracle.php
@@ -64,6 +64,17 @@ public function __construct($options)
}
/**
+ * Destructor.
+ *
+ * @since 12.1
+ */
+ public function __destruct()
+ {
+ $this->freeResult();
+ unset($this->connection);
+ }
+
+ /**
* Connects to the database if needed.
*
* @return void Returns void if the database connected successfully.
@@ -84,12 +95,15 @@ public function connect()
}
/**
- * Destructor.
+ * Disconnects the database.
+ *
+ * @return void
*
* @since 12.1
*/
- public function __destruct()
+ public function disconnect()
{
+ // Close the connection.
$this->freeResult();
unset($this->connection);
}
View
17 libraries/joomla/database/driver/pdo.php
@@ -84,6 +84,17 @@ public function __construct($options)
}
/**
+ * Destructor.
+ *
+ * @since 12.1
+ */
+ public function __destruct()
+ {
+ $this->freeResult();
+ unset($this->connection);
+ }
+
+ /**
* Connects to the database if needed.
*
* @return void Returns void if the database connected successfully.
@@ -290,11 +301,13 @@ public function connect()
}
/**
- * Destructor.
+ * Disconnects the database.
+ *
+ * @return void
*
* @since 12.1
*/
- public function __destruct()
+ public function disconnect()
{
$this->freeResult();
unset($this->connection);
View
24 libraries/joomla/database/driver/postgresql.php
@@ -78,6 +78,19 @@ public function __construct( $options )
}
/**
+ * Database object destructor
+ *
+ * @since 12.1
+ */
+ public function __destruct()
+ {
+ if (is_resource($this->connection))
+ {
+ pg_close($this->connection);
+ }
+ }
+
+ /**
* Connects to the database if needed.
*
* @return void Returns void if the database connected successfully.
@@ -112,16 +125,21 @@ public function connect()
}
/**
- * Database object destructor
+ * Disconnects the database.
*
- * @since 12.1
+ * @return void
+ *
+ * @since 12.1
*/
- public function __destruct()
+ public function disconnect()
{
+ // Close the connection.
if (is_resource($this->connection))
{
pg_close($this->connection);
}
+
+ $this->connection = null;
}
/**
View
13 libraries/joomla/database/driver/sqlite.php
@@ -63,6 +63,19 @@ public function __destruct()
}
/**
+ * Disconnects the database.
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ public function disconnect()
+ {
+ $this->freeResult();
+ unset($this->connection);
+ }
+
+ /**
* Drops a table from the database.
*
* @param string $tableName The name of the database table to drop.
View
22 libraries/joomla/database/driver/sqlsrv.php
@@ -86,6 +86,19 @@ public function __construct($options)
}
/**
+ * Destructor.
+ *
+ * @since 12.1
+ */
+ public function __destruct()
+ {
+ if (is_resource($this->connection))
+ {
+ sqlsrv_close($this->connection);
+ }
+ }
+
+ /**
* Connects to the database if needed.
*
* @return void Returns void if the database connected successfully.
@@ -131,16 +144,21 @@ public function connect()
}
/**
- * Destructor.
+ * Disconnects the database.
+ *
+ * @return void
*
* @since 12.1
*/
- public function __destruct()
+ public function disconnect()
{
+ // Close the connection.
if (is_resource($this->connection))
{
sqlsrv_close($this->connection);
}
+
+ $this->connection = null;
}
/**
View
1 tests/core/mock/database/driver.php
@@ -37,6 +37,7 @@ public static function create($test, $nullDate = '0000-00-00 00:00:00', $dateFor
$methods = array(
'connect',
'connected',
+ 'disconnect',
'dropTable',
'escape',
'execute',
View
22 tests/suites/unit/joomla/database/stubs/nosqldriver.php
@@ -63,29 +63,29 @@ public function connect()
return true;
}
-
/**
-
* Determines if the connection to the server is active.
-
*
-
* @return boolean True if connected to the database engine.
-
*
-
* @since 11.4
-
*/
-
public function connected()
-
{
-
return true;
-
}
+ /**
+ * Disconnects the database.
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ public function disconnect()
+ {
+ return;
+ }
/**
* Drops a table from the database.

0 comments on commit 1ef2ad6

Please sign in to comment.
Something went wrong with that request. Please try again.