Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added JDatabaseDriver::disconnect() to support disconnecting from the database cleanly. #1189

Merged
merged 1 commit into from

3 participants

@robschley

This is useful when building applications with JApplicationDaemon because the database resource that gets copied after forking is shared between parent and child and if either of the processes close the connect, it is closed for both. With this, you can use the onFork event to disconnect and reconnect the database to get a new, unshared resource.

@mahagr

There's a related feature I've really would like to see in Joomla platform: reconnect/retry if database query fails because of the connection was cut by the server timeout. The most common reason is JInstaller extracting a large zip and trying to insert component without checking if the connection is still there.

@pasamio pasamio merged commit 1ef2ad6 into joomla:staging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 6, 2012
  1. @robschley
This page is out of date. Refresh to see the latest.
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.
Something went wrong with that request. Please try again.