php - How PDO rollbacks the queries before executing rollBack() function? -
here script:
try { $dbh_con->begintransaction(); $stmt1 = $dbh_conn->prepare("update activate_account_num set num = num + 1"); $stmt1->execute(); $stmt2 = $dbh_con->prepare("select user_id activate_account token = ?"); $stmt2->execute(array($token)); $num_rows = $stmt2->fetch(pdo::fetch_assoc); if ( $num_rows['user_id'] ){ $_session['error'] = 'all fine'; } else { $_session['error'] = 'token invalid'; header('location: /b.php'); exit(); } $dbh_con->commit(); header('location: /b.php'); exit(); } catch(pdoexception $e) { $dbh_con->rollback(); $_session['error'] = 'something wrong'; header('location: /b.php'); exit(); }
as see, else
block contains exit()
function. when else
block executes, surely rollback();
function doesn't execute, because before executing rollback();
, script exits. surprisingly update
statement rollbacks.. how ?
transactions used when it's critical changes happen together, or no change happens @ all.
to preserve integrity of db in event of abrupt stoppage (eg: script exits unexpectedly, server crashes, power supply cut...), implementations of transactions keep protected not making change until commit()
called. when execute
intermediary queries, change not hitting database, held in limbo. if exit without committing, limbo thrown away.
when rollback()
, limbo thrown away.
Comments
Post a Comment