public function getConnection()
    {
        if (empty($this->connection)) {

            if (empty($this->connectionConfig) || !is_array($this->connectionConfig))
                throw new DatabaseException("Connection config is empty or not an array.");

            $connCount = count($this->connectionConfig);
            for($i = 0; $i < $connCount; ++$i)
            {
                $connectionInfo = $this->connectionConfig[$i];

                if (empty($connectionInfo['port']))
                    $connectionInfo['port'] = 3306;

                $dsn = "mysql:dbname={$connectionInfo['database']};host={$connectionInfo['host']};port={$connectionInfo['port']}";
                if (!empty($connectionInfo['unix_socket']))
                    $dsn .= ';unix_socket='.$connectionInfo['unix_socket'];

                try {

                    $trx_attempt = 0;
                    while ($trx_attempt < $this->deadlockRetries) {

                        try {

                            $this->connection = new PDO($dsn, $connectionInfo['username'], $connectionInfo['password'], array(
                                PDO::ATTR_PERSISTENT => (!empty($connectionInfo['persistent'])?StringUtils::strToBool($connectionInfo['persistent']):false),
                                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                                PDO::ATTR_CASE => PDO::CASE_NATURAL,
                                PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
                                PDO::MYSQL_ATTR_DIRECT_QUERY => true,
                                PDO::ATTR_TIMEOUT => (!empty($connectionInfo['timeout'])?$connectionInfo['timeout']:30) ));


                            $this->connDebugString = $connectionInfo['host'].':'.$connectionInfo['database'];
                            $this->currentDBName = $connectionInfo['database'];

                            $this->Logger->debug(array(
                                'SQL'                    => 'CONNECTION - DB: '.$this->connDebugString,
                            ));

                            return $this->connection;

                        } catch (PDOException $pe) {
                            if ((strpos($pe->getMessage(), 'Too many connections') !== FALSE) && ++$trx_attempt < $this->deadlockRetries)
                            {
                                usleep( pow(2,$trx_attempt) * 100000 );
                                continue;
                            }

                            throw $pe;
                        }
                    }

                } catch(PDOException $pe)
                {

                    // if the last connection fails
                    if($i == ($connCount-1))
                        throw new SQLConnectionException($pe->getMessage());
                }
            }
        }

        return $this->connection;
    }