Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
project
drupal
Commits
4e614ace
Commit
4e614ace
authored
Aug 04, 2009
by
webchick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#434350
by cpliakas and Crell: Add a method to explicitly close a database connection.
parent
b405e613
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
0 deletions
+52
-0
includes/database/database.inc
includes/database/database.inc
+37
-0
modules/simpletest/tests/database_test.test
modules/simpletest/tests/database_test.test
+15
-0
No files found.
includes/database/database.inc
View file @
4e614ace
...
...
@@ -1363,6 +1363,29 @@ public static function addConnectionInfo($key, $target, $info) {
}
}
/**
* Closes a connection to the server specified by the given key and target.
*
* @param $target
* The database target name. Defaults to NULL meaning that all target
* connections will be closed.
* @param $key
* The database connection key. Defaults to NULL which means the active key.
*/
public
static
function
closeConnection
(
$target
=
NULL
,
$key
=
NULL
)
{
// Gets the active conection by default.
if
(
!
isset
(
$key
))
{
$key
=
self
::
$activeKey
;
}
// To close the connection, we need to unset the static variable.
if
(
isset
(
$target
))
{
unset
(
self
::
$connections
[
$key
][
$target
]);
}
else
{
unset
(
self
::
$connections
[
$key
]);
}
}
/**
* Instruct the system to temporarily ignore a given key/target.
*
...
...
@@ -2071,6 +2094,20 @@ function db_driver() {
return
Database
::
getConnection
()
->
driver
();
}
/**
* Closes the active database connection.
*
* @param $options
* An array of options to control which connection is closed. Only the
* target key has any meaning in this case.
*/
function
db_close
(
array
$options
=
array
())
{
if
(
empty
(
$options
[
'target'
]))
{
$options
[
'target'
]
=
NULL
;
}
Database
::
closeConnection
(
$options
[
'target'
]);
}
/**
* @} End of "defgroup database".
*/
...
...
modules/simpletest/tests/database_test.test
View file @
4e614ace
...
...
@@ -230,6 +230,21 @@ class DatabaseConnectionTestCase extends DatabaseTestCase {
$this
->
assertIdentical
(
$db1
,
$db2
,
t
(
'Both targets refer to the same connection.'
));
}
/**
* Tests the closing of a database connection.
*/
function
testConnectionClosing
()
{
// Open the default target so we have an object to compare.
$db1
=
Database
::
getConnection
(
'default'
,
'default'
);
// Try to close the the default connection, then open a new one.
Database
::
closeConnection
(
'default'
,
'default'
);
$db2
=
Database
::
getConnection
(
'default'
,
'default'
);
// Opening a connection after closing it should yield an object different than the original.
$this
->
assertNotIdentical
(
$db1
,
$db2
,
t
(
'Opening the default connection after it is closed returns a new object.'
));
}
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment