Advertisement
Guest User

Untitled

a guest
May 7th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.72 KB | None | 0 0
  1. <?php
  2. class PDOConnectionFactory{
  3.    
  4.     public static $instance = null;
  5.    
  6.     private $debug=1;
  7.  
  8.     public $conn = array();
  9.     public $persistent = true;
  10.     public $connlimit=5;
  11.     public $connleases=0;
  12.     public $connqty=0;
  13.  
  14.     public $dbType = "mysql";
  15.     public $host = "localhost";
  16.     public $user = "root";
  17.     public $password = "tyuiopas";
  18.     public $db = "test";
  19.    
  20.     private function __construct() {
  21.     }
  22.  
  23.     public function log($message) {
  24.         if ($debug > 0) {
  25.             $timestamp="## NOW ##";
  26.             echo "$timestamp - $message\n";
  27.         }
  28.     }
  29.    
  30.     public static function getInstance() {
  31.         if (!isset(self::$instance)) {
  32.             // Você deve informar os dados para conexão com o banco de dados.
  33.             self::$instance = new PDOConnectionFactory();
  34.         }
  35.         return self::$instance;
  36.     }
  37.    
  38.     public function getConnection(){
  39.         $retconn=null;
  40.         if ($this->connleases < $this->connlimit) {
  41.             log("Navega o Pool para achar Conexões existentes\n");
  42.             for ($i=1; $i<=$this->connlimit; $i++) {
  43.                 if ($this->conn[$i] != null) {
  44.                     //echo "Fornecendo: $i\n";
  45.                     $retconn = $this->conn[$i];
  46.                     $this->connleases++;
  47.                     unset($this->conn[$i]); // Remove Efetivamente a Connection do Array
  48.                     break;
  49.                 }
  50.             }
  51.             log("Caso não exista no pool, criar uma nova\n");
  52.             if ($retconn == null) {
  53.                 try{
  54.                     //$newconn = rand(1,1000000);
  55.                     $newconn = new PDO($this->dbType.":host=".$this->host.";dbname=".$this->db, $this->user, $this->password, array( PDO::ATTR_PERSISTENT => $this->persistent ) );
  56.                     $this->connqty++;
  57.                     $this->connleases++;
  58.                     $retconn = $newconn;
  59.                 } catch ( PDOException $ex ) {
  60.                     log("Erro: ".$ex->getMessage());
  61.                     return -1;
  62.                 }
  63.             }
  64.         } else {
  65.             log("Todas conexões alocadas");
  66.             return -1;
  67.         }
  68.         return $retconn;
  69.     }
  70.    
  71.     public function returnConnection($leasedconn) {
  72.         //echo "Retornando: $leasedconn\n";
  73.         for ($i=1; $i <= $this->connlimit; $i++) {
  74.            // echo "i = : $i   conn-i = " . $this->conn[$i] . "\n";
  75.            
  76.             if ($this->conn[$i] == null) {
  77.                 //echo "Guardando em $i\n";
  78.                 $this->conn[$i]=$leasedconn;
  79.                 $this->connleases--;
  80.              
  81.                 break; // Parar o Loop - foi a melhor idéia que tive :(
  82.             }
  83.         }
  84.     }
  85. }
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement