Skip to content

Instantly share code, notes, and snippets.

@phpdave
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpdave/b27af33c5c2454464082 to your computer and use it in GitHub Desktop.
Save phpdave/b27af33c5c2454464082 to your computer and use it in GitHub Desktop.
example of using commitment control in DB2. Not isolation levels don't appear to work.
<?PHP
//Add commitment control to the DB2 Connect by using i5_commit
//Apparently you can't set this using ini_set. Therefore this would have to be set in the PHP.ini
//ini_set('ibm_db2.i5_allow_commit',1);
ini_set('ibm_db2.autocommit',DB2_AUTOCOMMIT_OFF);
$options = array('i5_lib' => 'MYLIB','autocommit' => DB2_AUTOCOMMIT_OFF,'i5_commit' => DB2_I5_TXN_SERIALIZABLE);
//Connect to DB2 - Commitments wont take place until we call
//db2_commit($db2Connection) on the connection.
$db2Connection = db2_connect('', '', '', $options);
if (!$db2Connection) {die("false - Connection failed.");}
//Run queries that change the table
$sql = "INSERT INTO MYTABLE (COl1) VALUES(?)";
$insertstmt = db2_prepare($db2Connection, $sql);
$dataArray = array('1','2','3');
foreach ($dataArray as $value)
{
$result = db2_execute($insertstmt, $value);
//Note: below we use die which would stop any records from being inserted
//alternatively could just echo out the error and let the other record go through.
if($result==false) {die(db2_stmt_errormsg());}
}
//The 3 records that were created will now be pushed live
$commitResult = db2_commit($db2Connection);
if(!$commitResult) {die("Commit failed");}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment