Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworking database connection checking and adding a test script.
MySQL PDO extension throws an error instead of an exception when trying to query from a connection that has gone away, it fucked everything up. Hopefully the other PDO extensions don't do the same. Also added the ability to manually check the connection and force reconnect yourself.
- Loading branch information
Showing
4 changed files
with
101 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
spl_autoload_register( | ||
function ($className) | ||
{ | ||
$ds = DIRECTORY_SEPARATOR; | ||
$className = str_replace('js\\tools\\dbhandler', '', $className); | ||
$className = str_replace('\\', $ds, $className); | ||
$className = trim($className, $ds); | ||
|
||
$path = __DIR__ . $ds . 'src' . $ds . $className . '.php'; | ||
|
||
if (!is_readable($path)) | ||
{ | ||
return false; | ||
} | ||
|
||
require $path; | ||
return true; | ||
}, | ||
true | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
require 'autoloader.php'; | ||
|
||
use js\tools\dbhandler\exceptions\DbException; | ||
use js\tools\dbhandler\Handler; | ||
|
||
try | ||
{ | ||
// TODO set your own test connection parameters! | ||
$handler = Handler::getConnection('test', [ | ||
'driver' => 'mysql', | ||
'username' => 'test', | ||
'password' => 'test', | ||
'database' => 'test', | ||
]); | ||
// this is the mysql.ini variable that should be adjusted according to needs | ||
// but it works via query as well | ||
$handler->exec('SET SESSION wait_timeout = 1'); | ||
|
||
echo 'connection timeout set to 1 second, sleeping', PHP_EOL; | ||
sleep(3); | ||
|
||
if ($handler->checkConnection()) | ||
{ | ||
echo 'connection is good', PHP_EOL; | ||
} | ||
else | ||
{ | ||
echo 'connection is down, reconnecting', PHP_EOL; | ||
$handler->connect(); | ||
} | ||
|
||
if ($handler->checkConnection()) | ||
{ | ||
echo 'connection is now good', PHP_EOL; | ||
} | ||
else | ||
{ | ||
echo 'connection is still down, failed to reconnect', PHP_EOL; | ||
} | ||
} | ||
catch (DbException $e) | ||
{ | ||
echo $e->getMessage(), PHP_EOL; | ||
die(); | ||
} |