SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Andere mochten auch

Andere mochten auch (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Kürzlich hochgeladen

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
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
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
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
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 

Kürzlich hochgeladen (20)

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 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...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
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
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 

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