Note that there are some explanatory texts on larger screens.

plurals
  1. POProper PDO way to handle freeing resources after stored procedure with multiple queries?
    primarykey
    data
    text
    <p>I am working on replacing usage of <code>mysqli</code> with <code>PDO</code> in the database connector file of <a href="http://dbapi.scientiamobile.com/wiki/index.php/Main_Page" rel="nofollow">Tera-WURFL</a>, because our server has PHP 5.2.6 and mysqli does not support persistent connections until 5.3.0. </p> <p>So, I am replacing all mysqli code with PDO code. Having done with the basic data retrieval and processing, I have come to this part of freeing up resources, where I am not sure how exactly to replace the following - </p> <pre><code>protected function cleanConnection(){ while($this-&gt;dbcon-&gt;more_results()){ $this-&gt;dbcon-&gt;next_result(); $res = $this-&gt;dbcon-&gt;use_result(); if ($res instanceof mysqli_result){$res-&gt;free();} } } </code></pre> <p>The above function is called every time after calling a stored procedure. I guess this function fetches any remaining results and cleans the pipe between the master and the slave. Can somebody answer if what I guess is correct and explain why is this done only in this case.</p> <p>Following is a sample stored proc call from the database connector file of Tera-WURFL which calls the above function afterwards - </p> <pre><code>$query = sprintf("CALL ".TeraWurflConfig::$TABLE_PREFIX."_RIS(%s,%s,%s)",$this-&gt;SQLPrep($userAgent),$tolerance,$this-&gt;SQLPrep($matcher-&gt;tableSuffix())); $res = $this-&gt;dbcon-&gt;query($query); //this calls TeraWurfl_RIS() stored proc if(!$res){ throw new Exception(sprintf("Error in DB RIS Query: %s. \nQuery: %s\n",$this-&gt;dbcon-&gt;error,$query)); exit(); } $data = $res-&gt;fetch_assoc(); $this-&gt;cleanConnection(); //calling cleanConnection() </code></pre> <p>Following is the stored procedure being called above - </p> <pre><code>CREATE PROCEDURE `TeraWurfl_RIS`(IN ua VARCHAR(255), IN tolerance INT, IN matcher VARCHAR(64)) BEGIN DECLARE curlen INT; DECLARE wurflid VARCHAR(64) DEFAULT NULL; DECLARE curua VARCHAR(255); SELECT CHAR_LENGTH(ua) INTO curlen; findua: WHILE ( curlen &gt;= tolerance ) DO SELECT CONCAT(LEFT(ua, curlen ),'%') INTO curua; SELECT idx.DeviceID INTO wurflid FROM TeraWurflIndex idx INNER JOIN TeraWurflMerge mrg ON idx.DeviceID = mrg.DeviceID WHERE mrg.match = 1 AND idx.matcher = matcher AND mrg.user_agent LIKE curua LIMIT 1; IF wurflid IS NOT NULL THEN LEAVE findua; END IF; SELECT curlen - 1 INTO curlen; END WHILE; SELECT wurflid as DeviceID; END </code></pre> <p><strong>What I have got so far</strong> </p> <pre><code>protected function cleanConnection($resultObj){ if($resultObj instanceof PDOStatement) { while($res = $resultObj-&gt;fetch()){ if ($res instanceof PDO){$res = null;} } } } </code></pre> <p>And this is how I am calling -</p> <pre><code>$query = sprintf("CALL ".TeraWurflConfig::$TABLE_PREFIX."_RIS(:userAgent,:tolerance,:tableSuffix)"); $queryHandle = $this-&gt;dbcon-&gt;prepare($query); $queryHandle-&gt;execute(array(':userAgent' =&gt; $userAgent,':tolerance' =&gt; $tolerance,':tableSuffix' =&gt; $matcher-&gt;tableSuffix())); $data = $queryHandle-&gt;fetch(); $this-&gt;cleanConnection($queryHandle); //calling here </code></pre> <p><strong>Update</strong><br> This piece of code is going to be put in our ad delivery servers which are multiple in number and serve high traffic. I guess under such conditions handling the freeing of resources becomes important which why this was done in the original file. Can somebody comment on this? Thanks...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload