Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6829299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:29:03+00:00 2026-05-26T22:29:03+00:00

I am currently using a combination of PHP, ORACLE, PDO, and JNDI to connect

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:29:04+00:00Added an answer on May 26, 2026 at 10:29 pm

    The correct approach seems to be to use a singleton-based database class as such:

    class Database { 
    
        protected $conn; 
        protected $dbstr; 
    
        // keep the one and only instance of the Database object in this variable
        protected static $instance;
    
        // visibility changed from public to private to disallow dynamic instances
        private function __construct() { 
            $this->conn = null; 
            $this->dbstr = "jndi connection string"; 
            $this->connect(); 
        } 
    
        // added this method
        public static function getInstance() {
          if (!isset(self::$instance)) {
            self::$instance = new Database();
          }
          return self::$instance;
        }
    
        // everything below this comment is as it was; I made no changes here
        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; 
        } 
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using a combination of CSS and Div tags to achieve rounded corners
Currently we're using Web Application project, so we have a gain with compilation. But
I'm currently learning Java, using a combination of the Head First Java book and
Currently i am using a combination of switch operations to generate a linq query,
How can you send control key combination to an element when using watir-webdriver? Currently
I'm currently working on a quote retrieval system for my employer's website using mysql/php/html.
I'm currently using <?php echo get_the_term_list( $post->ID, 'tag', '', '', '' ); ?> to
I'm currently building a web application that uses a combination of OpenID and uname/pw
Currently using System.Web.UI.WebControls.FileUpload wrapped in our own control. We have licenses for Telerik. I
Currently using the HTTPServletRequest class and specifically the .getQueryString method to retrieve an inputted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.