private function receiveQueueMessages(Queue $queue, $max, $timeout)
    {
        $messages      = array();
        $microtime = microtime(true); // cache microtime
        $db        = $this->getDb();
        $qid       = $this->getQueueId($queue->getName());
        
        // start transaction handling
        try {
            if ( $max > 0 ) {
                $db->beginTransaction();

                $sql = "SELECT *
                        FROM " . $this->messageTable . "
                        WHERE queue_id = :queue_id
                        AND (handle IS NULL OR timeout+" . (int)$timeout . " < " . (int)$microtime .")
                        LIMIT ".$max;
                $stmt = $db->prepare($sql);
                $stmt->execute(array('queue_id'=>$qid));

                foreach ($stmt->fetchAll() as $data) {
                    $data['handle'] = md5(uniqid(rand(), true));

                    $sql = "UPDATE " . $this->messageTable . "
                            SET
                                handle = :handle,
                                timeout = :timeout
                            WHERE
                                message_id = :id
                                AND (handle IS NULL OR timeout+" . (int)$timeout . " < " . (int)$microtime.")";

                    $stmt = $db->prepare($sql);
                    $stmt->bindParam(':handle', $data['handle'], \PDO::PARAM_STR);
                    $stmt->bindParam(':id', $data['message_id'], \PDO::PARAM_STR);
                    $stmt->bindValue(':timeout', $microtime);
                    $updated = $stmt->execute();
                    
                    if ($updated) {
                        $messages[] = $data;
                    }
                }
                $db->commit();
            }
        } catch (\Exception $e) {
            $db->rollBack();
            throw $e;
        }
        
        $m = array();
        foreach($messages as $msg) {
            $message = unserialize(base64_decode($msg['body']));
            if($message instanceof Message) {
                $message->message_id = $msg['message_id'];
                $m[] = $message;
            }
        }

        return new \ArrayObject($m);
    }