How do you create multiple DB connections using Singleton pattern? Or maybe there's better approach, to share the same class but multiple connections?
How about using a Factory pattern to return the same instance for each connection, e.g.
ConnectionFactory::getInstance(ConnectionFactory::DEVELOPMENT);
Returns a Connection instance for a connection to the development database.
Instantiation of the Connection should only be performed by the ConnectionFactory, which can keep references to those instances in a static array, keyed by the connection type. This avoid the singleton pattern, but ensures you only maintain a single instance of each Connection.
Connection Pooling.
In case of java:
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/Code/JDCConnectionPool.java
This example just shows the way, you can implement it in still better way.
How about dropping the Singleton pattern if what you want is multiple? Lately, Singleton has become an anti-pattern so you should probably drop it even if you need a single connection. Not to mention that extending Singleton in PHP is pretty hard for the moment.
As others have said, drop the singleton, then, I'd probably do something like this:
interface Connection_Interface
{
public function connect();
public function disconnect();
public function exec($sql);
// etc...
}
class Connection implements Connection_Interface
{
public function __construct($host, $username, $password, $database);
public function connect();
public function disconnect();
public function exec($sql);
// etc...
}
Then, another class that takes multiple connections:
class Connection_Multiple implements Connection_Interface
{
protected $_connections = array();
public function __construct();
public function add(Connection $connection);
public function connect();
public function disconnect();
public function exec($sql)
{
// decide here which connection you want to use, then...
return $connection->exec($sql);
}
// etc...
}
As both the single connection and the multiple connection classes implement the same interface you can use either in exactly the same way.
I've come up with this solution:
class Database {
private static $instances = array();
public static function getInstance($connection='default') {
if (!array_key_exists($connection,self::$instances)) {
self::$instances[$connection] = new Database($connection);
}
return self::$instances[$connection];
}
private function __construct($connection) {
$this->credentials = // getting credentials from config by $connection...
$this->connect(); // connect using credentials
}
}
$DB1 = Database::getInstance('development');
$DB2 = Database::getInstance('production');
Seems like works for me. Is this a Singleton pattern or something mixed?
Related
I created a simple database connection class:
class db {
private $db_host = "host";
private $db_user = "user";
private $db_pass = "pass";
private $db_name = "db_name";
public $con;
public function __construct() {
$this->con = mysqli_connect($this->db_host, ... );
...
}
public function query($query) {
return mysqli_query($this->con,$query);
}
}
Now, I would like to use $db->query() within another class instance:
class display {
public function menu() {
this function creates a list based on a result of $db->query('SQL query')
}
plus various other functions to display different things, all using $db->query()
So what is the problem?
First, I got this working by adding 'global $db' to $disply->menu() but this is obviously a big NO NO and needs to be specified within each method.
It is probably clear now that I would like to use $db->query(SQL query) wherever and whenever I need an SQL result, not just within the global scope.
I spent the last couple of hours looking this up but to my surprise most people ask how to use something like $instance->instance->method in global.
Many thanks for your time.
Tom
A simple solution is to inject the $db object when you instance your classes
class Display {
protected $db;
public function __construct($db) {
$this->db = $db;
}
public function menu() {
// here you can use $this->db as if it was the global $db;
}
}
You need to add $db as a parameter everytime you instance your classes
$mydisplay = new Display($db);
There are other ways to achieve the same purpose. For example you can make the $db class a singleton, and add a static method to retrieve the instance from wherever you need. And there are more sophisticated ways such as using pimple to manage your dependency injections as a provider instead of doing it manually.
I used to extend the Db connection class in every class that needs to connect to the database. I think that this is the most common way. But, by following this way, you open and close a new connection in every class instance that extends the db connection class. Lately, i thought that i could create a pdo object and pass it into class' constructor. So, every class instance that needs access to database use the same connection. It works but i am not sure if this is an efficient way to do it. Also, i have a function called closeCon that i call at the end of the script so as to close the connection via null and unset. I would like to know your opinion about that too. Thank you in advance : )
METHOD 1: New class extends dbConnection class.
class Db {
public $pdo;
public function __construct($usr, $pwd, $db) {
$this->pdo = new PDO("mysql:host=localhost;dbname=".$db.";charset=utf8", $usr, $pwd);
}
}
class Users extends Db{
public function __construct(){
parent::__construct($usr, $pwd, $db);
}
}
METHOD 2: Connect to db by passing PDO dbConnection variable into new class' constructor.
class Db {
public $pdo;
public function __construct($usr, $pwd, $db) {
$this->pdo = new PDO("mysql:host=localhost;dbname=".$db.";charset=utf8", $usr, $pwd);
}
public function closeCon(){
$this->pdo = null;
unset($this->pdo);
}
}
class Users {
protected $pdo;
public function __construct($con){
$this->pdo = $con;
}
}
$db = new Db($usr, $pwd, $db);
$posts = new Users($db->pdo);
$db->closeCon();
There is absolutely no point in extending application classes from DB class. So - Plan B.
The only case Method 1 might be acceptable is if Users were a model. However, this approach is typically bad practice due to coupling, multiple inheritance limitations, etc. And in your case creates multiple database connections.
Method 2, is a better approach of the two. It is a form of dependency injection.
Good work. This is much more efficient than opening a connection every time, and you are properly disposing of your connections when you are done.
I am switching my MVC to use PDO (I know, overdue). My application has, in the past, used the following class hierarchy:
Database.class>Main.class>User.class
(each one extending the other). But before any object is created, the mysql connection was made (mysql_connect). Once the connection was open I could use Database.class as a wrapper class through which all my queries were performed. Through extention, a query in the User.class could be made simply by calling the "query" function ($this->query).
Using PDO, I've tried to imitate the process but find errors. I created a singleton function in the Database.class:
function __construct()
{
$this->db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASSWORD);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
public static function getInstance()
{
if (!isset(self::$instance)){
$object = __CLASS__;
self::$instance = new $object;
}
return self::$instance;
}
function query($qry,$params=NULL){
$qry = $this->db->prepare('SELECT * FROM users WHERE userID = :userID');
$qry->execute(array(':userID' => 1));
$results = $qry->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
Then in Main.class I get the instance:
function __construct()
{
$this->db = parent::getInstance();
}
So in User.class I try to call
function __construct(){
parent::__construct();
}
function test(){
return $this->db->query("test");
}
So I can run any queries fine from the Main.class object. But if I try to run queries from User.class object I get the error: "Call to a member function query() on a non-object" In other words, if User.class extends main I should be able to access the variable $db in Main from User.class (I call the constructor for Main when the User object is created). Part of the issue is that Main.class is created earlier in the application as it's own object, I believe causing two instances of PDO to be created - which is why it doesn't work through extension (through a second object that also extends the database.class)
So my question is: is there a way to make this happen? Or is my best option to use injection for every object I create (because some scripts incorporate multiple objects that extend Main.class - which try to create an instance of PDO each time) and pass the pdo object to the constructor? I'd rather not have to do that (the less markup the better) So another option would be to use a STATIC variable that all classes use? What's the best method? (let me know if this is confusing)
I've seen people using injection for this, and I've seen examples of extending the pdo wrapper class (but only once).
Thanks! (I love stack overflow!)
You dont want any of these to extend the database class because that will essentially make them all singletons of which you can only have one instance... you Want to make them USE the database class instead. So you would put you most abstract db methods on the Database and then methods that create queries for specific things would be on the User or what have you. This means your Database actually wraps PDO and is what all other classes work with for db operations. The Main or Base class may not even be needed unless you are trying to implement active record or something.
class Database {
static protected $instance;
/**
* #var PDO
*/
protected $connection;
protected function __construct($dsn, $user, $pass, $attrs = array()) {
// create pdo instance and assign to $this->pdo
}
public static function getInstance() {
if(!self::$instance) {
// get the arguments to the constructor from configuration somewhere
self::$instance = new self($dsn, $user, $pass);
}
return self::$instance;
}
// proxy calls to non-existant methods on this class to PDO instance
public function __call($method, $args) {
$callable = array($this->pdo, $method);
if(is_callable($callable)) {
return call_user_func_array($callable, $args);
}
}
}
class Main {
protected $db;
public function __construct() {
$this->db = Database::getInstance();
}
}
class User extends Main{
public function __construct() {
parent::__construct();
}
public function findById($id) {
$qry = $this->db->prepare('SELECT * FROM users WHERE userID = :userID');
$qry->execute(array(':userID' => $id));
$results = $qry->fetchAll(PDO::FETCH_ASSOC);
return $results
}
}
I'm new to this level of PHP programming and I've been reading a post about singleton and static classes. I'm in the process of writing a class that would facilitate my DB connections.
I came across the following code by Jon Raphaelson (here) :
class ConnectionFactory
{
private static $factory;
public static function getFactory()
{
if (!self::$factory)
self::$factory = new ConnectionFactory(...);
return self::$factory;
}
private $db;
public function getConnection() {
if (!$this->db) // this line was modified due to comment
$this->db = new PDO(...); // this line was modified due to comment
return $db;
}
}
function getSomething()
{
$conn = ConnectionFactory::getFactory()->getConnection();
.
.
.
}
It seemed like I had found what I was looking for, however I have a few questions about it.
self::$factory = new ConnectionFactory(...); - I don't see a constructor in this class. Do I just create this constructor and pass in the db details ('dbname', 'user', 'pass', etc)?
the getSomething() function, I'm assuming that the intent was to put all of the actual functions that would retrieve data within the ConnectionFactory class - and this is the reason for this function being in this class. Otherwise, I would have expected this function to be within another class. [edit] SKIP THIS QUESTION, I didn't see the one bracket.
What happens when two users are logged into the site and are requesting the DB connection (both are doing updates, etc)? Will it be an issue that this is a singleton?
Thanks!
This is mostly to expand on my comments under the question...
The better "singleton" pattern would be this:
class ConnectionFactory {
protected static $connection;
public function getConnection() {
if (!self::$connection) {
self::$connection = new PDO(...);
}
return self::$connection;
}
}
The users of this factory should expect an instance of it, not call it themselves:
class Foo {
protected $connectionFactory;
public function __construct(ConnectionFactory $factory) {
$this->connectionFactory = $factory;
}
public function somethingThatNeedsAConnection() {
$connection = $this->connectionFactory->getConnection();
...
}
}
$foo = new Foo(new ConnectionFactory);
This allows Foo to get a connection itself when needed, but also allows you to inject some alternative connection into Foo, for example for testing purposes.
As a convenience measure and to cut down on instantiation complexity, this pattern is also good:
class Foo {
protected $connectionFactory;
public function __construct(ConnectionFactory $factory = null) {
if (!$factory) {
$factory = new ConnectionFactory;
}
$this->connectionFactory = $factory;
}
...
}
This still allows the dependency to be injected and overridden, but it doesn't require you to inject a factory every time you instantiate Foo.
The factory is a singleton because you're not want more than one factory.
You don't need a constructor, you can setup whatever state you need for the factory the getFactory() method. The getFactory() function doesn't get a connection, so don't do any connection related setup here, but not the connection object itself. Factories 'build instances' of classes for you to use, so you'd use the getConnection() method to set the state and construct the connection object the factory is supposed to generate.
getSomething() isn't a method in the ConnectionFactory class, it's just a example of using the factory class to get a factory, then get a connection.
I personally hate method chaining in PHP. What's really happening in getSomething() is:
function getSomething()
{
$factory = ConnectionFactory::getFactory();
$conn = $factory->getConnection();
.
.
.
}
A class doesn't need an explicit constructor in order to be instantiated with new ClassName(). However, if the class is supposed to be a singleton (and it looks like it in this case), the entire point of the pattern is making such instantiations impossible from outside of the class which is why I believe there should be a constructor declared with private keyword.
The get getSomething() is a "standalone" function with an example of using your class.
No. Multiple users use multiple instances of PHP processing your script and nothing is shared among them.
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;
}
}
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.
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.
Use a database layer. Dibi is a great library in this case. http://dibiphp.com