Keep connection open in webservice php

4

I am building a simple webservice and PHP + JSON where I have 2 tables and intend to carry out CRUD operations. It is possible that the medium-term webservice consumed by many devices at the same time, which in my opinion can lead to many concurrent connections. The server has a limit of active connections determined by the personnel of the infra (I think they are 30). My question is, I use the PDO to open the connection and the connection is called by a static method, in each request I get the user ID that makes the query, it is better to change the way the connection is always made opening and closing every query or this will not affect the number of open connections? Here is an excerpt from my connection class:

class Database extends PDO {

    /**
     * @var array Array of saved databases for reusing
     */
    protected static $instances = array();

    /**
     * Static method get 
     * 
     * @param  array $group
     * @return \helpers\database
     */
    public static function get ($group = false) {
        // Determining if exists or it's not empty, then use default group defined in config
        $group = !$group ? array (
            'type' => 'mysql',
            'host' => 'localhost',
            'name' => 'banco',
            'user' => 'root',
            'pass' => 'toor'
        ) : $group;

        // Group information
        $type = $group['type'];
        $host = $group['host'];
        $name = $group['name'];
        $user = $group['user'];
        $pass = $group['pass'];

        // ID for database based on the group information
        $id = "$type.$host.$name.$user.$pass";

        // Checking if the same 
        if(isset(self::$instances[$id])) {
            return self::$instances[$id];
        }

        try {
            // I've run into problem where
            // SET NAMES "UTF8" not working on some hostings.
            // Specifiying charset in DSN fixes the charset problem perfectly!
            $instance = new Database("$type:host=$host;dbname=$name", $user, $pass);
            $instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            // Setting Database into $instances to avoid duplication
            self::$instances[$id] = $instance;

            return $instance;
        } catch(PDOException $e){
            //in the event of an error record the error to errorlog.html
            Logger::newMessage($e);
            logger::customErrorMsg();
        }
    }
    
asked by anonymous 03.02.2015 / 13:18

1 answer

1

Relevant question. Part of the answer is in my answer in the following link here in SO ...

Multiple Bank Connections

I would add that your implementation contemplates very well static array with the connections established in a request. Good solution when a request uses the Database class more than once.

In case of successive requests there are techniques with reference to cache server but not always available. I use the memcached . However I think optimizing the server to deal with the problem will be the best. As I said, read in my answer that I put the link here.

    
04.02.2015 / 15:14