Académique Documents
Professionnel Documents
Culture Documents
Transaction
D Phng Hnh
B mn H thng thng tin Khoa CNTT, trng i hc Cng ngh i hc Quc gia Hanoi
hanhdp@vnu.edu.vn
Introducing Transactions
We live in a transactional world and we perceive that things such as money, files, and data move from one place to another.
We understand that data doesn't really move. It gets copied from storage and the new copy is inserted into a new storage location, and then the original copy is deleted from its initial location.
In a database, a transaction is simply a mechanism to ensure and verify that data gets to its intended destination. Just like a purchase or bank transaction, both parties must be satisfied with the results.
Transaction Types
Explicit Transaction.
The explicit transaction is defined by the presence of an explicit BEGIN TRANSACTION statement followed by one or more dependent data modification statements and completed with an explicit COMMIT TRANSACTION statement. Error checking is added prior to the COMMIT TRANSACTION statement so that if an error occurred the transaction can be reversed with a ROLLBACK TRANSACTION statement.
Transaction Types
Implicit Transaction.
The implicit transaction follows the behavior of some other database products in that whenever a data modification is executed it implicitly begins a transaction. However, it does not complete the transaction and release the modified data until an explicit COMMIT TRANSACTION or ROLLBACK TRANSACTION statement is issued. Implicit transactions are enabled on a connection basis with the SET IMPLICIT_TRANSACTIONS ON command.
4
Transaction Types
Auto-Commit Transaction.
If a data modification statement is executed against the database without an explicit or implicit transaction, it is considered an auto-commit transaction. The modification contained in an auto-commit transaction follows the same pattern as other transactions
H qun tr c s d liu
MySQL Transaction
D Phng Hnh
B mn H thng thng tin Khoa CNTT, trng i hc Cng ngh i hc Quc gia Hanoi
hanhdp@vnu.edu.vn
10
Examples
Using MySQL transaction to add new sale order into our Classicmodels database and add the transaction processing steps:
Start a transaction using START TRANSACTION Get latest sale order number from orders table, and use the next sale order number as the new sale order number. Insert a new sale order into orders table for a given customer Insert new sale order items into orderdetails table Commit changes using COMMIT statement Get data from both table orders and orderdetails tables to confirm the changes
11
H qun tr CSDL @ BM HTTT
MySQL Transaction
12
13
14
H qun tr c s d liu
Isolation level
D Phng Hnh
B mn H thng thng tin Khoa CNTT, trng i hc Cng ngh i hc Quc gia Hanoi
hanhdp@vnu.edu.vn
Introduce
Isolation is a property that defines how/when the changes made by one operation become visible to other concurrent operations. Of the four ACID properties in a DBMS, the isolation property is the one most often relaxed.
The isolation levels defined by the ANSI/ISO SQL standard are listed as follows:
16
Read phenomena
Dirty Read
17
Read phenomena
Nonrepeatable read
18
Read phenomena
Phantom Reads
19
Isolation levels
20