note 85211 added to pdo.begintransaction

From: Date: Wed, 20 Aug 2008 16:21:42 +0000
Subject: note 85211 added to pdo.begintransaction
Groups: php.notes 
Request: Send a blank email to php-notes+get-143681@lists.php.net to get a copy of this message
// If you need to set an ISOLATION level or LOCK MODE it needs to be done BEFORE you make the BeginTransaction() call... // // **note** you should always check result codes on operations and do error handling. This sample code // assumes all the calls work so that the order of operations is accurate and easy to see // // THIS IS using the PECL PDO::INFORMIX module, running on fedora core 6, php 5.2.4 // // This is the correct way to address an informix -243 error (could not position within table) when there // is no ISAM error indicating a table corruption. A -243 can happen (if the table/indexes, etc., are ok) // if a row is locked. The code below sets the LOCK MODE to wait 2 minutes (120 seconds) before // giving up. In this example you get READ COMMITTED rows, if you don't need read committed // but just need to get whatever data is there (ignoring locked rows, etc.) instead of // "SET LOCK MODE TO WAIT 120" you could "SET ISOLATION TO DIRTY READ". // // In informix you *must* manage how you do reads because it is very easy to trigger a // lock table overflow (which downs the instance) if you have lots of rows, are using joins // and have many updates happening. // // e.g., $sql= "SELECT FIRST 50 * FROM mytable WHERE mystuff=1 ORDER BY myid"; /* define SQL query */ try /* create an exception handler */ { $dbh = new PDO("informix:host=......"); if ($dbh) /* did we connect? */ { $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->query("SET LOCK MODE TO WAIT 120") # ---------------- # open transaction cursor # ---------------- if ( $dbh->beginTransaction() ) # explicitly open cursor { try /* open exception handler */ { $stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL)); $stmt->execute(); while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) { $data = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\t" . $row[3] . "\t" . $row[4] . "\t" . $row[5] . "\t" . $row[6] . "\t" . $row[7] . "\n" . $row[8] ; //print $data; print_r($row); }; $stmt = null; } catch (PDOException $e) { print "Query Failed!\n\n"; print "DBA FAIL:" . $e->getMessage(); }; $dbh->rollback(); # abort any changes (ie. $dbh->commit() $dbh = null; # close connection } else { # we should never get here, it should go to the exception handler print "Unable to establish connection...\n\n"; }; }; } catch (Exception $e) { $dbh->rollback(); echo "Failed: " . $e->getMessage(); }; ---- Server IP: 208.69.120.35 Probable Submitter: 67.191.208.219 ---- Manual Page -- http://www.php.net/manual/en/pdo.begintransaction.php Edit -- https://master.php.net/note/edit/85211 Del: integrated -- https://master.php.net/note/delete/85211/integrated Del: useless -- https://master.php.net/note/delete/85211/useless Del: bad code -- https://master.php.net/note/delete/85211/bad+code Del: spam -- https://master.php.net/note/delete/85211/spam Del: non-english -- https://master.php.net/note/delete/85211/non-english Del: in docs -- https://master.php.net/note/delete/85211/in+docs Del: other reasons-- https://master.php.net/note/delete/85211 Reject -- https://master.php.net/note/reject/85211 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143681) next »