標籤:http io os ar 使用 for sp 資料 on
以下的文章主要向大家描述的是PHP(PHP培訓 php教程 ) + MySQL(MySQL認證 Mysql培訓 )事務操作的實際應用代碼示範,我們大家都知道在實際LAMP的應用中,一般PHP所使用的是AdoDB來對MySQL資料庫進行操作,下面給出AdoDB相應的代碼方便大家查閱:
startTrans();
實際,getOne所調用的查詢也可以直接放到rowLock來進行,這裡只是為了示範效果能更明顯些。
$adodb->rowLock(‘book‘, ‘book_id = 123‘); $bookNumber = $adodb->getOne("SELECT book_number FROM book WHERE book_id = 123"); $adodb->execute("UPDATE book SET book_numberbook_number = book_number - 1 WHERE book_id = 123"); $adodb->completeTrans(); // ... ?>
其中,rowLock的方法就是調用的FOR UPDATE來實現的行鎖,你可能會想把“FOR UPDATE”直接寫到$adodb->getOne()調用的那條SQL語句裡面去實現行鎖的功能,不錯,那樣確實可以,但是並不是所有的資料庫都使用“FOR UPDATE”文法來實現行鎖功能,比如Sybase使用“HOLDLOCK”的文法來實現行鎖功能,所以為了你的資料庫抽象層保持可移植性,我還是勸你用rowLock來實現行鎖功能,至於可移植性就交給AdoDB好了,嗯,有點扯遠了,今兒就說到這裡了。
上述的相關內容就是對PHP + MySQL事務操作的代碼示範的描述,希望會給你帶來一些協助在此方面。
附:
AdoDB中存在一個setTransactionMode()方法,能夠設定事務的隔離等級,如下:
SetTransactionMode allows you to pass in the transaction mode to use for all subsequent transactions for that connection session. Note: if you have persistent connections and using mysql or mssql, you might have to explicitly reset your transaction mode at the beginning of each page request. This is only supported in postgresql, mssql, mysql with InnoDB and oci8 currently. For example: $db->SetTransactionMode("SERIALIZABLE"); $db->BeginTrans(); $db->Execute(...); $db->Execute(...); $db->CommiTrans(); $db->SetTransactionMode(""); // restore to default $db->StartTrans(); $db->Execute(...); $db->Execute(...); $db->CompleteTrans(); Supported values to pass in: * READ UNCOMMITTED (allows dirty reads, but fastest) * READ COMMITTED (default postgres, mssql and oci8) * REPEATABLE READ (default mysql) * SERIALIZABLE (slowest and most restrictive)
以上的相關內容就是對PHP + MySQL事務操作的代碼示範的介紹,望你能有所收穫。
PHP + MySQL事務操作的實際應用代碼示範