Skip to content
Snippets Groups Projects
Verified Commit 94005a75 authored by Giovannoni Laurent's avatar Giovannoni Laurent
Browse files

FIX pb with persistant connection and static object

parent 81301fdd
No related branches found
No related tags found
No related merge requests found
......@@ -325,6 +325,7 @@ if ($_REQUEST['page'] && empty($_REQUEST['triggerAngular'])) {
$cookie = \SrcCore\models\AuthenticationModel::getCookieAuth();
if (empty($cookie)) {
header('location: index.php?display=true&page=logout&logout=true');
exit();
}
chdir('../..');
$user = \User\models\UserModel::getByUserId(['userId' => $cookie['userId'], 'select' => ['password_modification_date', 'change_password', 'status']]);
......
......@@ -51,12 +51,8 @@ class Database extends functions
/**
* Constructor. Connects to the database if connection parameters are available in the session config
*/
public function __construct($params=[])
public function __construct()
{
$persistent = true;
if ($params['persistent'] == false) {
$persistent = false;
}
$args = func_get_args();
if (count($args) < 1 || empty($args[0])) {
if (isset($_SESSION['config']['databaseserver'])) {
......@@ -129,7 +125,7 @@ class Database extends functions
if (!isset($args[0]['password'])) {
$this->password = 'postgres';
} else {
$this->password = $args[0]['pass'];
$this->password = $args[0]['password'];
}
if (! isset($args[0]['base'])) {
$this->database = '';
......@@ -190,11 +186,11 @@ class Database extends functions
// Set options
$options = array (
PDO::ATTR_PERSISTENT => $persistent,
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_CASE => PDO::CASE_LOWER
);
// Create a new PDO instanace
// Create a new PDO instance
try {
$this->pdo = new PDO($this->dsn, $this->user, $this->password, $options);
} catch (PDOException $PDOException) {
......@@ -316,7 +312,9 @@ class Database extends functions
*/
public function query($queryString, $parameters=null, $catchExceptions=false, $multi=false)
{
$originalQuery = $queryString;
if ($parameters) {
$originalData = $parameters;
foreach ($parameters as $key => $value) {
if (is_array($value)) {
//echo $key . $value. '<br />';
......@@ -395,11 +393,11 @@ class Database extends functions
} else {
if (strpos($PDOException->getMessage(), 'Admin shutdown: 7') !== false) {
//echo 'catch error:' . $PDOException->getMessage() . '<br />';
$db = self::Database(['persistent' => false]);
if ($withParams) {
$executed = $db->stmt->execute($parameters);
$db = new Database();
if ($originalData) {
$db->query($originalQuery, $originalData);
} else {
$executed = $db->stmt->execute();
$db->query($originalQuery);
}
} else {
if ($_SESSION['config']['debug'] == 'true') {
......
......@@ -17,22 +17,12 @@ namespace SrcCore\models;
class DatabasePDO
{
private static $pdo = null;
private $pdo;
private static $type = null;
private static $preparedQueries = [];
public function __construct(array $args = [])
{
if (!empty(self::$pdo)) {
return;
}
$persistent = true;
if ($args['persistent'] == false) {
$persistent = false;
}
$server = '';
$port = '';
$name = '';
......@@ -106,17 +96,17 @@ class DatabasePDO
}
$options = [
\PDO::ATTR_PERSISTENT => $persistent,
\PDO::ATTR_PERSISTENT => true,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_CASE => \PDO::CASE_LOWER
];
try {
self::$pdo = new \PDO($dsn, $user, $password, $options);
$this->pdo = new \PDO($dsn, $user, $password, $options);
} catch (\PDOException $PDOException) {
try {
$options[\PDO::ATTR_PERSISTENT] = false;
self::$pdo = new \PDO($dsn, $user, $password, $options);
$this->pdo = new \PDO($dsn, $user, $password, $options);
} catch (\PDOException $PDOException) {
throw new \Exception($PDOException->getMessage());
}
......@@ -129,11 +119,13 @@ class DatabasePDO
public function query($queryString, array $data = [])
{
$originalQuery = $queryString;
if (self::$type == 'ORACLE') {
$queryString = str_ireplace('CURRENT_TIMESTAMP', 'SYSDATE', $queryString);
}
if (!empty($data)) {
$originalData = $data;
$tmpData = [];
foreach ($data as $key => $value) {
if (is_array($value)) {
......@@ -151,7 +143,7 @@ class DatabasePDO
try {
if (empty(self::$preparedQueries[$queryString])) {
$query = self::$pdo->prepare($queryString);
$query = $this->pdo->prepare($queryString);
self::$preparedQueries[$queryString] = $query;
} else {
$query = self::$preparedQueries[$queryString];
......@@ -159,16 +151,16 @@ class DatabasePDO
$query->execute($data);
} catch (\PDOException $PDOException) {
if (strpos($PDOException->getMessage(), 'Admin shutdown: 7') !== false) {
$db = self::Database(['persistent' => false]);
if (empty($db::$preparedQueries[$queryString])) {
$query = $db::$pdo->prepare($queryString);
self::$preparedQueries[$queryString] = $query;
if (
strpos($PDOException->getMessage(), 'Admin shutdown: 7') !== false ||
strpos($PDOException->getMessage(), 'General error: 7') !== false
) {
$db = new DatabasePDO();
if ($originalData) {
$db->query($originalQuery, $originalData);
} else {
$query = $db::$preparedQueries[$queryString];
$db->query($originalQuery);
}
$query->execute($data);
} else {
$param = implode(', ', $data);
$file = fopen('queries_error.log', 'a');
......@@ -206,7 +198,7 @@ class DatabasePDO
public static function reset()
{
self::$pdo = null;
$this->pdo = null;
self::$preparedQueries = [];
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment