Page 1 of 1

Problem with closing a PDO-connection

Posted: Wed May 07, 2008 4:22 pm
by Lingultblad
Hi,

I'm having problems closing my database-connection. It's the first time I've been working using PDO. If I have understood everything right you just set the PDO-object to null to close it.
I have no problem opening the connection. It's just that it refuses to close which lead to me getting in trouble cause I reach max for connections.

Any help is greatly appreciated!!

this is the beginning of my database-class:

Code: Select all

<?php
// Class providing generic data access functionality
class DatabaseHandler
{
    // Hold an instance of the PDO class
    private static $_mHandler;
 
    // Private constructor to prevent direct creation of object
    private function __construct()
    {
    }
 
    // Return an initialized database handler 
    private static function GetHandler()
    {
        // Execute code catching potential exceptions
        try
        {
            // Create a database connection only if one doesn’t already exist
            if (!isset(self::$_mHandler))
            {
                // Create a new PDO class instance
                self::$_mHandler =
                    new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD,
                        array(PDO::ATTR_PERSISTENT => DB_PERSISTENCY));
 
                // Configure PDO to throw exceptions
                self::$_mHandler->setAttribute(PDO::ATTR_ERRMODE,
                                    PDO::ERRMODE_EXCEPTION);
                
                // Return the database handler
                return self::$_mHandler;
            }
        }
        catch (PDOException $e) 
        {
            // Close the database handler throw the errormessage
            self::Close();
            throw new Exception($e->getMessage());
        }
    }
 
    // Clear the PDO class instance
    public static function Close()
    {
        self::$_mHandler = null;
    }
 
    // Below follows functions for exeuting SQL-queries..
The functions all uses this code to open and close the connection:

Code: Select all

$database_handler = self::GetHandler();
self::Close();
Ps. PDO_DSN etc. comes from a config-file. which looks like this:

Code: Select all

define('DB_PERSISTENCY', 'true');
define('DB_SERVER', '*******');
define('DB_USERNAME', '********');
define('DB_PASSWORD', '*******');
define('DB_DATABASE', '********');
define('PDO_DSN', 'mysql:host=' . DB_SERVER . ';dbname=' . DB_DATABASE);