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

SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 

Kürzlich hochgeladen (20)

SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 

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