I am currently using a combination of PHP, ORACLE, PDO, and JNDI to connect my application to the database. I am having trouble understanding the best approach to managing connection pools in an Object-Oriented approach. As a result, i’m getting max-connection warnings when I attempt to do bulk inserts (anything above 32 inserts (which my max-pool size is set to)).
Consider this example:
Main File:
//User uploads excel document which I parse into an array
$car = array();
foreach($array as $index => $data){
$car[$index] = new Car(null,$data["make"],$data["model"]);
$car[$index]->insert();
}
//return car array of objects
On the Class:
//Car Class
class Car{
protected $pkey;
protected $make;
protected $model;
protected $db;
public function __construct($pkey,$make,$model){
$this->pkey = $pkey;
if(isset($make) && ($make != '')){
$this->make = $make;
}else{
throw new Exception("Car must have make");
}
if(isset($model) && ($model != '')){
$this->model = $model;
}else{
throw new Exception("Car must have model");
}
$this->db = new Database();
}
public function insert(){
$sql = "INSERT INTO TABLE (...) VALUES (..)";
$data = array(
":make"=>$this->make,
":model"=>$this->model,
);
try{
$this->pkey = $this->db->insert($sql,$data);
return true;
}catch(Exception $err){
//catch errors
return false;
}
}
}
In this example, assuming max-pool is set to 32, any array greater than 32 will cause me to exceed the max-pool-size because each car object is stored with an active db connection. To fix this I tried implementing the following fixes to the class.
//Car Class
class Car{
protected $pkey;
protected $make;
protected $model;
protected $db;
public function __construct($pkey,$make,$model){
$this->pkey = $pkey;
if(isset($make) && ($make != '')){
$this->make = $make;
}else{
throw new Exception("Car must have make");
}
if(isset($model) && ($model != '')){
$this->model = $model;
}else{
throw new Exception("Car must have model");
}
//$this->db = new Database(); //Moved out of the constructor
}
public function insert(){
$this->establishDBConn();
$sql = "INSERT INTO TABLE (...) VALUES (...)";
$data = array(
":make"=>$this->make,
":model"=>$this->model,
);
try{
$this->pkey = $this->db->insert($sql,$data);
$this->closeDBConn();
return true;
}catch(Exception $err){
//catch errors
$this->closeDBConn();
return false;
}
}
protected function establishDBConn(){
if(!$this->db){
$this->db = new Database();
}
}
public function closeDBConn(){
if($this->db){
$this->db->close();
$this->db = null;
}
}
}
In theory this change should have enforced only maintaining an active connection during the actual insert process. However, with this change I continue to hit my max connection pool limit. As a last-ditch effort I moved all the insert logic out of the car class and created a bulk insert function. This function ignores the concept of an object, instead it just receives a data array which it loops through and inserts on a single data connection. This works, but I would love to find a way to fix my problem with in the constraints of Object-Oriented Programming.
Any suggestions on how I can improve my code in order to make more efficient use of objects and database connections?
For reference this is what my database class looks like:
class Database {
protected $conn;
protected $dbstr;
public function __construct() {
$this->conn = null;
$this->dbstr = "jndi connection string";
$this->connect();
}
public function connect(){
try{
$this->conn = new PDO($this->dbstr); // Used with jndi string
} catch (PDOException $e){
// print $e->getMessage();
}
return "";
}
public function insert($query, $data){
try{
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/* Execute a prepared statement by passing an array of values */
$sth = $this->conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$count = $sth->execute($data);
return $this->oracleLastInsertId($query);
}catch(PDOException $e){
throw new Exception($e->getMessage());
}
}
public function oracleLastInsertId($sqlQuery){
// Checks if query is an insert and gets table name
if( preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $sqlQuery, $tablename) ){
// Gets this table's last sequence value
$query = "select ".$tablename[1]."_SEQ.currval AS last_value from dual";
try{
$temp_q_id = $this->conn->prepare($query);
$temp_q_id->execute();
if($temp_q_id){
$temp_result = $temp_q_id->fetch(PDO::FETCH_ASSOC);
return ( $temp_result ) ? $temp_result['LAST_VALUE'] : false;
}
}catch(Exception $err){
throw new Exception($err->getMessage());
}
}
return false;
}
public function close(){
$this->conn = null;
}
}
The correct approach seems to be to use a singleton-based database class as such: