1--TEST-- 2PDO_OCI: Attribute: beginTransaction and native transactions 3--EXTENSIONS-- 4pdo 5pdo_oci 6--SKIPIF-- 7<?php 8require(__DIR__.'/../../pdo/tests/pdo_test.inc'); 9PDOTest::skip(); 10?> 11--FILE-- 12<?php 13 14require(__DIR__ . '/../../pdo/tests/pdo_test.inc'); 15$dbh = PDOTest::factory(); 16 17$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); 18$dbh->exec("drop table pdo_ac_tab"); 19$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 20 21$dbh->exec("create table pdo_ac_tab (col1 varchar2(25))"); 22 23echo "Test 1 Check beginTransaction insertion\n"; 24 25$dbh->beginTransaction(); 26try { 27 $dbh->exec("insert into pdo_ac_tab (col1) values ('data 1')"); 28 $dbh->exec("insert into pdo_ac_tab (col1) values ('data 2')"); 29 $dbh->commit(); 30} 31catch (PDOException $e) { 32 echo "Caught unexpected exception at line " . __LINE__ . "\n"; 33 echo $e->getMessage() . "\n"; 34 $dbh->rollback(); 35} 36 37echo "Test 2 Cause an exception and test beginTransaction rollback\n"; 38 39$dbh->beginTransaction(); 40try { 41 $dbh->exec("insert into pdo_ac_tab (col1) values ('not committed #1')"); 42 $dbh->exec("insert into pdo_ac_tab (col1) values ('data that is too long to fit and will barf')"); 43 $dbh->commit(); 44} 45catch (PDOException $e) { 46 echo "Caught expected exception at line " . __LINE__ . "\n"; 47 echo $e->getMessage() . "\n"; 48 $dbh->rollback(); 49} 50 51echo "Test 3 Setting ATTR_AUTOCOMMIT to true will commit and end the transaction\n"; 52 53$dbh->exec("insert into pdo_ac_tab (col1) values ('data 3')"); 54$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT, true); 55print "PDO::ATTR_AUTOCOMMIT: "; 56var_dump($dbh->getAttribute(PDO::ATTR_AUTOCOMMIT)); 57try { 58 $dbh->rollback(); 59} 60catch (PDOException $e) { 61 echo "Caught expected exception at line " . __LINE__ . "\n"; 62 echo $e->getMessage() . "\n"; 63} 64 65echo "Test 4 Setting ATTR_AUTOCOMMIT to false will commit and end the transaction\n"; 66 67$dbh->beginTransaction(); 68$dbh->exec("insert into pdo_ac_tab (col1) values ('data 4')"); 69$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT, false); 70print "PDO::ATTR_AUTOCOMMIT: "; 71var_dump($dbh->getAttribute(PDO::ATTR_AUTOCOMMIT)); 72try { 73 $dbh->rollback(); 74} 75catch (PDOException $e) { 76 echo "Caught expected exception at line " . __LINE__ . "\n"; 77 echo $e->getMessage() . "\n"; 78} 79 80echo "Test 5 Handle transactions ourselves\n"; 81 82print "PDO::ATTR_AUTOCOMMIT: "; 83var_dump($dbh->getAttribute(PDO::ATTR_AUTOCOMMIT)); 84 85$dbh->exec("insert into pdo_ac_tab (col1) values ('not committed #2')"); 86$dbh->exec("rollback"); 87$dbh->exec("insert into pdo_ac_tab (col1) values ('data 5')"); 88$dbh->exec("insert into pdo_ac_tab (col1) values ('data 6')"); 89 90$dbh->exec("commit"); 91 92// Open new connection to really verify what was inserted 93 94$dbh2 = PDOTest::factory(); 95 96echo "Query Results are:\n"; 97$s = $dbh2->prepare("select col1 from pdo_ac_tab"); 98$s->execute(); 99while ($r = $s->fetch()) { 100 echo $r[0] . "\n"; 101} 102 103echo "Done\n"; 104 105?> 106--EXPECTF-- 107Test 1 Check beginTransaction insertion 108Test 2 Cause an exception and test beginTransaction rollback 109Caught expected exception at line 35 110SQLSTATE[HY000]: General error: 12899 OCIStmtExecute: ORA-12899: %s 111%s 112Test 3 Setting ATTR_AUTOCOMMIT to true will commit and end the transaction 113PDO::ATTR_AUTOCOMMIT: bool(true) 114Caught expected exception at line %d 115There is no active transaction 116Test 4 Setting ATTR_AUTOCOMMIT to false will commit and end the transaction 117PDO::ATTR_AUTOCOMMIT: bool(false) 118Caught expected exception at line %d 119There is no active transaction 120Test 5 Handle transactions ourselves 121PDO::ATTR_AUTOCOMMIT: bool(false) 122Query Results are: 123data 1 124data 2 125data 3 126data 4 127data 5 128data 6 129Done 130