Home » Php » How to avoid opening multiple connections to the DB with OOP PHP

How to avoid opening multiple connections to the DB with OOP PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’ve been a procedural programmer for over 4 yrs and it’s time to start looking into OOP. With that said, let’s say I needed to call two methods in my class. Each method requires a connection to the DB so that’s two trips to the DB, which also opens multiple connections.
Can this be avoided by having some sort of code in the application layer (constructor?) or does a connection pool have to be setup on the DB side? And just for kicks, I’m not using mysql; I’m using mongodb with codeigniter.

Here’s what I have so far, not sure if it’s ideal to use?

Here’s where I setup my DB info:

database_conn.php

class Database_Conn extends Model {

    function _connect() {
        $m = new Mongo("localhost:27017", array("persist"=>"x"));
        $db = $m->selectDB( "foo" );
        return $db;
    }    
}     

sample model file

class Home_model extends Model {

    public function __construct() {
        // Establish connection to "profiles" table
        $this->db_conn = Database_Conn::_connect()->selectCollection( "profiles" );
    }

    function getMyProfile($username) {
        $data = $this->db_conn->findOne(array("username" => $username) );
        return $data;
    }

    function getAll() {
        $data = $this->db_conn->find(); 
        return $data;
    }
}
How to&Answers:

you should use singleton pattern

EDIT:
the way you did it, it is possible to call _connect multiple times, which means reconnecting.

singleton implementation usually means you have to make constructor private/protected and define a getInstance method which creates connection on first call and returns the created connection on later calls.

this is what i would do:

class Database_Conn extends Model {

    static protected $_instance;

    protected $db = null;

    final protected function __construct() {
        $m = new Mongo("localhost:27017", array("persist"=>"x"));
        $this->db = $m->selectDB( "foo" );
    }

    static public function getInstance() {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function getConnection() {
        return $this->db;
    }

    final protected function __clone() { }
}

and then use Database_Conn::getInstance()->getConnection() to get the connection object.

Answer:

You should look into a manager class for your db connections. Then you can have one central place where you request connections from. If there is already an open connection the manager can return that instead of returning a new connection.

This would be one approach. There are tons of examples out there about how to implement something like this. There already some for mysql and mssql. But you could surely extend for your db.

Answer:

Use a database layer. Dibi is a great library in this case. http://dibiphp.com