PEAR DB Transaction Related

Source: Internet
Author: User
Tags dsn getmessage pear savepoint access database

1.autoCommit (), commit (), rollback ()

function autocommit ($onoff =false)
Specifies whether the transaction is committed automatically. Some back-end databases are not supported.

function commit ()
Commit the current transaction

function rollback ()
Rolling back the current transaction

For these three database transactions, it is not very understanding, first record the next instance

1<?PHP2 /**3 * Created by Phpstorm.4 * User: ${darlin}5 * DATE:2015/11/176 * time:14:377  */8 9 Ten //introducing the PDB library One require_once("db.php"); A //set Access database, login name, password, host - /* - $userName = ' root '; the $password = ' a12345 '; - $hostName = ' localhost '; - $dbName = ' DB_CHENGXL '; - //Set DSN parameters and database link, I connect the database to MySQL + $dsn = "mysql://$userName: [email protected] $hostName/$dbName"; - $db = Db::connect ($DSN); + */ A $db= & Db::connect (' Mysql://root:[email PROTECTED]/DB_CHENGXL '); at //Error Handling - if(Db::iserror ($db)) { -      die($db-getMessage ()); - } -  - //Using autocommit () in /* - $db->autocommit (false); to  + $db->query (' CREATE TABLE blah (A integer) '); - $db->query (' CREATE TABLE blue (b integer) '); the $db->commit (); *  $ $db->query (' INSERT into blah (a) VALUES (one) ');Panax Notoginseng $db->query (' INSERT into blah (a) VALUES (a) '); - $db->commit (); the  + $res 1 =& $db->query (' SELECT A from blah '); A if (Db::iserror ($res 1)) { the echo $res 1->getmessage (). "\ n"; + } - $i = 1; $ while ($res 1->fetchinto ($row, db_fetchmode_ordered)) { $ echo "Fetch data $row [0]\n]; - echo "Insert number $i ... \ n"; - $res 2 =& $db->query ("INSERT into Blue (b) VALUES ($i)"); the if (Db::iserror ($res 2)) { - echo $res 2->getmessage (). "\ n";Wuyi     } the $i + +; - } Wu $res 1->free (); - //Destroy About ////delete table deletes the data for the tables, the table still exists, and the table can still be manipulated.  $ //Also, there is a TRUNCATE TABLE TableName statement, which also deletes the data, and the table still exists.  - //drop table, which is the deletion of tables from the database. After deletion, the table structure does not exist and no further action can be made on the table.  -  - $db->query (' DROP TABLE blah '); A $db->query (' DROP TABLE Blue '); + $db->commit (); the */ -  $ //Using rollback () the $db->autocommit (false); the $db->query (' CREATE TABLE blah (a integer) '); the $db->query (' INSERT into blah (a) VALUES (11) '); the $res=&$db->query (' SELECT b from Blue '); - if(Db::iserror ($res)) { in     Echo $res->getmessage (). "\ n"; the } the  while($res->fetchinto ($row,db_fetchmode_ordered)) { About     if($row[0] = = ' problem ') { the         $db-rollback (); the     } the     Echo"123</br>"; + } - $res-Free (); the Bayi //$db->query (' DROP TABLE blah '); the $db-commit (); the  -  -  the?>

What is a transaction?

A transaction (Transaction) is the basic unit of concurrency control. The so-called transaction, which is a sequence of operations that are either executed or not executed, is an inseparable unit of work. For example, a bank transfer job: Debit from one account and add another account, both of which are either executed or not executed. Therefore, they should be regarded as a business. A transaction is a unit of data consistency maintained by a database that maintains data consistency at the end of each transaction.

As can be seen from the above description, the proposed transaction is mainly to solve the problem of maintaining data consistency in the concurrency situation.

A transaction has the following 4 basic characteristics.

Atomic (atomicity): The operations contained in a transaction are considered to be a logical unit in which the operations of the logical unit either succeed or fail altogether.

Consistency (consistency): only legitimate data can be written to the database, or the transaction should roll it back to its original state.

Isolation (Isolation): Transactions allow multiple users to concurrently access the same data without destroying the correctness and integrity of the data. At the same time, the modification of parallel transactions must be independent of the modifications of other parallel transactions.

Durability (persistent): After the transaction ends, the result of the transaction must be cured.

2. Statement of the transaction
Start things: Begin TRANSACTION
Commit a thing: Commit TRANSACTION
3.4 Properties of a transaction
① atomicity (atomicity): All elements in a transaction are committed or rolled back as a whole, and the elements of a transaction are non-divided, and the transaction is a complete operation.
② Consistency (CONSISTEMCY): When things are done, the data must be consistent, that is, the data in the data store is in a consistent state before the things begin. Ensure that data is lossless.
③ Isolation (Isolation): Multiple transactions that modify data are isolated from each other. This indicates that the transaction must be independent and should not be in any way or affect other transactions.
④ Persistence (Durability): After a transaction is complete, its effect on the system is permanent, and the modification persists even if a system failure occurs, and the database is actually modified
4. SavePoint of transactions
Save TRANSACTION savepoint Name--the name and location of the custom save point
ROLLBACK TRANSACTION Save Point Name-rollback to a custom savepoint

Some additions to other masters:

Standard definition of a transaction: a series of operations performed as a single logical unit of work, which require four properties of atomicity, consistency, isolation, and persistence, collectively known as ACID properties.

The so-called transaction is a user-defined sequence of database operations, which are either done entirely or not, and are an inseparable unit of work. For example, in a relational database, a transaction can be an SQL statement, a set of SQL statements, or an entire program.
Transactions and programs are two concepts. Generally speaking, a program contains multiple transactions.
The start and end of a transaction can be explicitly controlled by the user. If the user does not explicitly define transactions, the DBMS automatically divides things by default
Works In the SQL language, there are three statements that define a transaction:

The display transaction is identified with BEGIN TRANSACTION and end transaction, where the update and DELETE statements are either all executed or not executed at all. Such as:
Update student
Set Name= ' Tank '
where id=2006010
Delete from student
where id=2006011
Simply put, a transaction is a mechanism that maintains the integrity of the database.

The implementation form is to embed ordinary SQL statements into the BEGIN Tran ... Commit Tran (or full form Begin Transaction ... Commit Transaction), of course, you can also use rollback Tran to rollback a transaction, which is the undo operation, if necessary.

Using the transaction mechanism, the operation of the database is either all executed or not executed, ensuring the consistency of the database. SQL statements that require transactions are typically update and delete operations, and so on.

End Transaction T1

About SavePoint

A user can declare (declare) within a transaction (transaction) called a savepoint (savepoint).
The tag. The save point divides a large transaction into smaller fragments.

The user can use the savepoint (savepoint) to label anywhere within the transaction (transaction)
Remember. After the user rolls back the transaction (rolling), it is possible to choose from the current
The execution position is rolled back to any savepoint within the transaction. For example, a user can use a series of complex
A save point is inserted between the new (update) operation, and if an error occurs during the execution of a statement, use the
The user can roll back to a savepoint before the error without having to resubmit all the statements.

You can also use the Save Point (savepoint) when you develop your application. If a process
(procedure) contains multiple functions (function) that the user can start at each function
Create a save point. When a function fails, it is easy to restore the data to the function before it is executed
Status, rollback (roll back) can modify parameter recall function, or execute related error

When a transaction (transaction) is rolled back (rollback) to a savepoint (savepoint),
Oracle releases the locks used by the rollback statement. Other transactions that are waiting for the locked resource can continue
Perform. Transactions that require an update (update) of the locked data row can also continue to execute.

The process of rolling back a transaction (transaction) to a savepoint (savepoint) such as
1. Oracle rolls back the statement after the specified save point
2. Oracle retains the specified savepoint, but the save point created after it is cleared
3. Table-level locks (tables Lock) and row-level locks obtained by Oracle when this save point is released (row
Lock), but the previous data lock is still retained.

A partially rolled-back transaction (transaction) is still active and can continue execution.

A transaction (transaction), while waiting for another transaction, rolls back to
A save point (savepoint) operation does not release row lock. To avoid transactions
Because a lock cannot be acquired and is suspended, you should use the for before you perform an UPDATE or DELETE operation
UPDATE ... The NOWAIT statement. (This is what you get before rolling back the savepoint.)
Lock. The row-level locks obtained after the savepoint are freed, while the save point is executed after the
SQL Statements are also fully rolled back).

PEAR DB Transaction Related

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.