Hệ quản trị cơ sở dữ liệu

Transaction
Dư Phương Hạnh
Bộ môn Hệ thống thông tin
Khoa CNTT, trường Đại học Công nghệ
Đại học Quốc 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.
2

Hệ quản trị CSDL @ BM HTTT
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.

3

Hệ quản trị CSDL @ BM HTTT
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

Hệ quản trị CSDL @ BM HTTT
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

5

Hệ quản trị CSDL @ BM HTTT
The ACID Test
 Most of us have been burned enough by data
loss problems to realize that steps must be
taken to ensure that data gets from one
place to another. Although there are a number
of benefits, this is what transactions are all
about.
 A bona fide transaction must meet the following
criteria:

6

Hệ quản trị CSDL @ BM HTTT
The ACID Test
Atomic — All steps and operations that are part of a transaction are
treated as an atomic unit. Either all succeed or all fail together.
Consistent — The outcome of any transaction is always
predictable; all of the operations either fail or succeed. All
operations abide by consistency rules and checks to ensure
data integrity within the database.
Isolated — Any operations performed before, during, or after the
transaction will see related data in a consistent state, rather
than in a state of partial completion. Any user or operation that
queries data affected by a transaction will perceive that the
entire transaction was committed instantaneously.
Durable — If a transaction succeeds, data is written to disk and
does not revert to its previous state. Data can survive system
failure.

7

Hệ quản trị CSDL @ BM HTTT
Hệ quản trị cơ sở dữ liệu

MySQL Transaction
Dư Phương Hạnh
Bộ môn Hệ thống thông tin
Khoa CNTT, trường Đại học Công nghệ
Đại học Quốc gia Hanoi
hanhdp@vnu.edu.vn
Using MySQL Transaction
 To start a transaction you use the START TRANSACTION
statement
 To undo MySQL statements you use ROLLBACK statement.
Note that there are several SQL statements you cannot use
ROLLBACK such as:
– CREATE / ALTER / DROP DATABASE
– CREATE /ALTER / DROP / RENAME / TRUNCATE TABLE
– CREATE / DROP INDEX
– CREATE / DROP EVENT
– CREATE / DROP FUNCTION
– CREATE / DROP PROCEDURE
– …
9

Hệ quản trị CSDL @ BM HTTT
Using MySQL Transaction
 To write the changes into the database within a
transaction you use the COMMIT statement.
 It is important to note that MySQL automatically
commit the changes to the database by default. To
force MySQL not to commit changes automatically,
you need to use the following statement:
SET autocommit = 0;

10

Hệ quản trị CSDL @ BM HTTT
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ệ quản trị CSDL @ BM HTTT
MySQL Transaction

12

Hệ quản trị CSDL @ BM HTTT
MySQL Transaction with savepoint

13

Hệ quản trị CSDL @ BM HTTT
MySQL Transaction with
savepoint
SAVEPOINT identifier
ROLLBACK [WORK] TO [SAVEPOINT] identifier
RELEASE SAVEPOINT identifier

14

Hệ quản trị CSDL @ BM HTTT
Hệ quản trị cơ sở dữ liệu

Isolation level
Dư Phương Hạnh
Bộ môn Hệ thống thông tin
Khoa CNTT, trường Đại học Công nghệ
Đại học Quốc 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

Hệ quản trị CSDL @ BM HTTT
Read phenomena
 Dirty Read

17

Hệ quản trị CSDL @ BM HTTT
Read phenomena
 Nonrepeatable read

18

Hệ quản trị CSDL @ BM HTTT
Read phenomena
 Phantom Reads

19

Hệ quản trị CSDL @ BM HTTT
Isolation levels

20

Hệ quản trị CSDL @ BM HTTT

4.2 transaction 2

  • 1.
    Hệ quản trịcơ sở dữ liệu Transaction Dư Phương Hạnh Bộ môn Hệ thống thông tin Khoa CNTT, trường Đại học Công nghệ Đại học Quốc gia Hanoi hanhdp@vnu.edu.vn
  • 2.
    Introducing Transactions  Welive 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. 2 Hệ quản trị CSDL @ BM HTTT
  • 3.
    Transaction Types  ExplicitTransaction. – 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. 3 Hệ quản trị CSDL @ BM HTTT
  • 4.
    Transaction Types  ImplicitTransaction. – 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 Hệ quản trị CSDL @ BM HTTT
  • 5.
    Transaction Types  Auto-CommitTransaction. – 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 5 Hệ quản trị CSDL @ BM HTTT
  • 6.
    The ACID Test Most of us have been burned enough by data loss problems to realize that steps must be taken to ensure that data gets from one place to another. Although there are a number of benefits, this is what transactions are all about.  A bona fide transaction must meet the following criteria: 6 Hệ quản trị CSDL @ BM HTTT
  • 7.
    The ACID Test Atomic— All steps and operations that are part of a transaction are treated as an atomic unit. Either all succeed or all fail together. Consistent — The outcome of any transaction is always predictable; all of the operations either fail or succeed. All operations abide by consistency rules and checks to ensure data integrity within the database. Isolated — Any operations performed before, during, or after the transaction will see related data in a consistent state, rather than in a state of partial completion. Any user or operation that queries data affected by a transaction will perceive that the entire transaction was committed instantaneously. Durable — If a transaction succeeds, data is written to disk and does not revert to its previous state. Data can survive system failure. 7 Hệ quản trị CSDL @ BM HTTT
  • 8.
    Hệ quản trịcơ sở dữ liệu MySQL Transaction Dư Phương Hạnh Bộ môn Hệ thống thông tin Khoa CNTT, trường Đại học Công nghệ Đại học Quốc gia Hanoi hanhdp@vnu.edu.vn
  • 9.
    Using MySQL Transaction To start a transaction you use the START TRANSACTION statement  To undo MySQL statements you use ROLLBACK statement. Note that there are several SQL statements you cannot use ROLLBACK such as: – CREATE / ALTER / DROP DATABASE – CREATE /ALTER / DROP / RENAME / TRUNCATE TABLE – CREATE / DROP INDEX – CREATE / DROP EVENT – CREATE / DROP FUNCTION – CREATE / DROP PROCEDURE – … 9 Hệ quản trị CSDL @ BM HTTT
  • 10.
    Using MySQL Transaction To write the changes into the database within a transaction you use the COMMIT statement.  It is important to note that MySQL automatically commit the changes to the database by default. To force MySQL not to commit changes automatically, you need to use the following statement: SET autocommit = 0; 10 Hệ quản trị CSDL @ BM HTTT
  • 11.
    Examples Using MySQL transactionto 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ệ quản trị CSDL @ BM HTTT
  • 12.
  • 13.
    MySQL Transaction withsavepoint 13 Hệ quản trị CSDL @ BM HTTT
  • 14.
    MySQL Transaction with savepoint SAVEPOINTidentifier ROLLBACK [WORK] TO [SAVEPOINT] identifier RELEASE SAVEPOINT identifier 14 Hệ quản trị CSDL @ BM HTTT
  • 15.
    Hệ quản trịcơ sở dữ liệu Isolation level Dư Phương Hạnh Bộ môn Hệ thống thông tin Khoa CNTT, trường Đại học Công nghệ Đại học Quốc gia Hanoi hanhdp@vnu.edu.vn
  • 16.
  • 17.
    Read phenomena  Dirty Read 17 Hệquản trị CSDL @ BM HTTT
  • 18.
  • 19.
    Read phenomena  Phantom Reads 19 Hệquản trị CSDL @ BM HTTT
  • 20.
    Isolation levels 20 Hệ quảntrị CSDL @ BM HTTT