I have the following code within a PDO database class
/**
* Class DB
*/
class DB{
/**
* @var DB The one and only instance of this class;
*/
private static $theOneAndOnly = null;
/**
* @var array
*/
private static $transactionalOptions = array( PDO::ATTR_AUTOCOMMIT => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC );
/**
* @var array
*/
private static $nonTransactionalOptions = array( PDO::ATTR_AUTOCOMMIT => true, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC );
/**
* @var PDO
*/
private $transactionalDatabase = null;
/**
* @var PDO
*/
private $nonTransactionalDatabase = null;
/**
* @var bool
*/
private $isConnected = false;
/**
* Initializes the connections to the database. One connection for transaction based operations and one connection
* for non-transactional operations;
*/
private function __construct(){
if($this->isConnected() !== true){
//connect
if($this->connect() !== true){
//connection failed
exit( 'An internal error occurred. Please contact Keyfort support:' );
}
}
}
/**
* @return bool
*/
public function isConnected(){
return $this->isConnected;
}
/**
* @return bool
*/
public function connect(){
try{
$this->transactionalDatabase = new PDO(Config::DB_TYPE . ':host=' . Config::DB_HOST . ';dbname=' . Config::$DB_NAME, Config::$DB_USER, Config::$DB_PASS, self::$transactionalOptions);
$this->nonTransactionalDatabase = new PDO(Config::DB_TYPE . ':host=' . Config::DB_HOST . ';dbname=' . Config::$DB_NAME, Config::$DB_USER, Config::$DB_PASS, self::$nonTransactionalOptions);
$this->isConnected = true;
return true;
} catch(PDOException $exception){
Log::error('Initializing the database connections failed:' . $exception->getTraceAsString());
die();
//return false;
}
}
/**
* @return DB
*/
public static function &getInstance(){
if(self::$theOneAndOnly === null){
self::$theOneAndOnly = new self();
}
return self::$theOneAndOnly;
}
public static function ParamMultiple($key){
return self::Param($key) . ', ';
}
public static function Param($key){
return ':' . $key;
}
/**
* Close all connections;
*/
public function __destruct(){
$this->disconnect();
}
/**
* @return void
*/
public function disconnect(){
$this->transactionalDatabase = null;
$this->nonTransactionalDatabase = null;
$this->isConnected = false;
}
/**
* @return PDO
*/
public function &getTransactional(){
if($this->isConnected() !== true){
$this->connect();
}
return $this->transactionalDatabase;
}
/**
* @return PDO
*/
public function &getBasic(){
if($this->isConnected() !== true){
$this->connect();
}
return $this->nonTransactionalDatabase;
}
}
It gets called via DB::getInstance();
The first time it gets called, it does not "die" if it errors.
I eventually get
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in C:\path\to\class\db.php on line 64
Line 64 is the $this->transactionalDatabase line.
It does log the exception in my error log (as in the catch).
I'm drawing a blank as to why its not "die"ing, I've tried alot of things, and I'm wondering if it could be starting 2 PDOs in the try?
[edit] I'm running through the code with the wrong database user password currently. So I know what the error is caused by (it works ok when i put the correct password in), I just don't understand why it isn't processing the die() in the catch.
Use Fragment:
public static function GetByUsername($username){
$db = DB::getInstance();
$dbh = $db->getBasic();
$sql = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE ' . self::KEY_USERNAME . ' = ' . DB::Param(self::KEY_USERNAME);
$statement = $dbh->prepare($sql);
$statement->bindValue(DB::Param(self::KEY_USERNAME), $username);
if($statement->execute() === true){
$rawUser = $statement->fetch();
if($rawUser === null){
return null;
} else {
return self::CreateFromSelectResult($rawUser);
}
} else {
Log::error("Fetching user by username failed:" . print_r($dbh->errorInfo(), true));
return null;
}
}