SlideShare una empresa de Scribd logo
1 de 20
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

Más contenido relacionado

Destacado

Destacado (9)

6.1 query optimization overview
6.1 query optimization overview6.1 query optimization overview
6.1 query optimization overview
 
01 gioithieu
01 gioithieu01 gioithieu
01 gioithieu
 
2.2 cac chuong trinh my sql
2.2 cac chuong trinh my sql2.2 cac chuong trinh my sql
2.2 cac chuong trinh my sql
 
2.1 view
2.1 view2.1 view
2.1 view
 
9. partitioning
9. partitioning9. partitioning
9. partitioning
 
7. backup & restore data
7. backup & restore data7. backup & restore data
7. backup & restore data
 
8.replication
8.replication8.replication
8.replication
 
C3 2
C3 2C3 2
C3 2
 
2.3 quan ly truy cap
2.3 quan ly truy cap2.3 quan ly truy cap
2.3 quan ly truy cap
 

Similar a 4.2 transaction

Transaction Processing Concept
Transaction Processing ConceptTransaction Processing Concept
Transaction Processing ConceptNishant Munjal
 
Transactionsmanagement
TransactionsmanagementTransactionsmanagement
TransactionsmanagementSanjeev Gupta
 
Chapter-10 Transaction Processing and Error Recovery
Chapter-10 Transaction Processing and Error RecoveryChapter-10 Transaction Processing and Error Recovery
Chapter-10 Transaction Processing and Error RecoveryKunal Anand
 
Transaction management
Transaction managementTransaction management
Transaction managementArchanaMani2
 
Lecture1414_20592_Lecture1419_Transactions.ppt (2).pdf
Lecture1414_20592_Lecture1419_Transactions.ppt (2).pdfLecture1414_20592_Lecture1419_Transactions.ppt (2).pdf
Lecture1414_20592_Lecture1419_Transactions.ppt (2).pdfbadboy624277
 
dokumen.tips_silberschatz-korth-and-sudarshan1-transactions-transaction-conce...
dokumen.tips_silberschatz-korth-and-sudarshan1-transactions-transaction-conce...dokumen.tips_silberschatz-korth-and-sudarshan1-transactions-transaction-conce...
dokumen.tips_silberschatz-korth-and-sudarshan1-transactions-transaction-conce...DrCViji
 
Transaction processing ppt
Transaction processing pptTransaction processing ppt
Transaction processing pptJaved Khan
 
Transaction Properties in database | ACID Properties
Transaction Properties in database | ACID PropertiesTransaction Properties in database | ACID Properties
Transaction Properties in database | ACID Propertiesnomanbarki
 
Introduction to transaction processing
Introduction to transaction processingIntroduction to transaction processing
Introduction to transaction processingJafar Nesargi
 
Chapter 9 introduction to transaction processing
Chapter 9 introduction to transaction processingChapter 9 introduction to transaction processing
Chapter 9 introduction to transaction processingJafar Nesargi
 
Chapter 9 introduction to transaction processing
Chapter 9 introduction to transaction processingChapter 9 introduction to transaction processing
Chapter 9 introduction to transaction processingJafar Nesargi
 

Similar a 4.2 transaction (20)

DBMS UNIT 4
DBMS UNIT 4DBMS UNIT 4
DBMS UNIT 4
 
Transaction Processing Concept
Transaction Processing ConceptTransaction Processing Concept
Transaction Processing Concept
 
Transactionsmanagement
TransactionsmanagementTransactionsmanagement
Transactionsmanagement
 
Chapter-10 Transaction Processing and Error Recovery
Chapter-10 Transaction Processing and Error RecoveryChapter-10 Transaction Processing and Error Recovery
Chapter-10 Transaction Processing and Error Recovery
 
Sistem manajemen basis data 8
Sistem manajemen basis data   8Sistem manajemen basis data   8
Sistem manajemen basis data 8
 
Transaction management
Transaction managementTransaction management
Transaction management
 
Autonomous transaction
Autonomous transactionAutonomous transaction
Autonomous transaction
 
Transaction
TransactionTransaction
Transaction
 
Lecture1414_20592_Lecture1419_Transactions.ppt (2).pdf
Lecture1414_20592_Lecture1419_Transactions.ppt (2).pdfLecture1414_20592_Lecture1419_Transactions.ppt (2).pdf
Lecture1414_20592_Lecture1419_Transactions.ppt (2).pdf
 
dokumen.tips_silberschatz-korth-and-sudarshan1-transactions-transaction-conce...
dokumen.tips_silberschatz-korth-and-sudarshan1-transactions-transaction-conce...dokumen.tips_silberschatz-korth-and-sudarshan1-transactions-transaction-conce...
dokumen.tips_silberschatz-korth-and-sudarshan1-transactions-transaction-conce...
 
CH09.ppt
CH09.pptCH09.ppt
CH09.ppt
 
Sql transacation
Sql transacationSql transacation
Sql transacation
 
Transaction processing ppt
Transaction processing pptTransaction processing ppt
Transaction processing ppt
 
Transactions
TransactionsTransactions
Transactions
 
Tps revision 2017
Tps revision 2017Tps revision 2017
Tps revision 2017
 
Transaction Properties in database | ACID Properties
Transaction Properties in database | ACID PropertiesTransaction Properties in database | ACID Properties
Transaction Properties in database | ACID Properties
 
Introduction to transaction processing
Introduction to transaction processingIntroduction to transaction processing
Introduction to transaction processing
 
Chapter 9 introduction to transaction processing
Chapter 9 introduction to transaction processingChapter 9 introduction to transaction processing
Chapter 9 introduction to transaction processing
 
Chapter 9 introduction to transaction processing
Chapter 9 introduction to transaction processingChapter 9 introduction to transaction processing
Chapter 9 introduction to transaction processing
 
Transation.....thanveeer
Transation.....thanveeerTransation.....thanveeer
Transation.....thanveeer
 

Más de Trần Thanh (11)

07 trigger view
07 trigger view07 trigger view
07 trigger view
 
4 trigger
4  trigger4  trigger
4 trigger
 
Chuan
ChuanChuan
Chuan
 
C4 1 tuan 14
C4 1 tuan 14C4 1 tuan 14
C4 1 tuan 14
 
C3 2 (tuan6,7)
C3 2 (tuan6,7)C3 2 (tuan6,7)
C3 2 (tuan6,7)
 
C3 1
C3 1C3 1
C3 1
 
C2 2
C2 2C2 2
C2 2
 
C2 1
C2 1C2 1
C2 1
 
C1
C1C1
C1
 
C4 1
C4 1C4 1
C4 1
 
VoIP with Opensips
VoIP with OpensipsVoIP with Opensips
VoIP with Opensips
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 

Último (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

4.2 transaction

  • 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  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
  • 3. 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
  • 4. 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
  • 5. 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
  • 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 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
  • 12. MySQL Transaction 12 Hệ quản trị CSDL @ BM HTTT
  • 13. MySQL Transaction with savepoint 13 Hệ quản trị CSDL @ BM HTTT
  • 14. MySQL Transaction with savepoint SAVEPOINT identifier 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
  • 17. Read phenomena  Dirty Read 17 Hệ quản trị CSDL @ BM HTTT
  • 18. Read phenomena  Nonrepeatable read 18 Hệ quản trị CSDL @ BM HTTT
  • 19. Read phenomena  Phantom Reads 19 Hệ quản trị CSDL @ BM HTTT
  • 20. Isolation levels 20 Hệ quản trị CSDL @ BM HTTT