Transaction and Concurrency Control: Insert Statement, The Transaction Uses Four (4) Database
Transaction and Concurrency Control: Insert Statement, The Transaction Uses Four (4) Database
Transaction and Concurrency Control • Isolation: The data used during the execution of a current
transaction cannot be used by another transaction until the first one
A. Transaction is completed.
Example: If two (2) people use the same ATM card and make a
• A transaction is a collection of operations that form a single logical transaction at the same time, the first one to be connected will have
unit of work. its first transaction. Therefore, other accesses from that account
Example: As a customer, you will make a payment from the cashier would be locked until the existing session is over.
to purchase two (2) pairs of Nike shoes. From a customer's • Durability – ensures that once transaction changes are done and
standpoint, this can be considered as a single operation. In the committed, they cannot be undone or lost.
database, however, it consists of several operations, such as
writing a new customer’s invoice, reducing the quantity of on-hand Real-life transaction: Fund transfer
products in inventory, and updating the sales. If one operation is not A transaction to transfer ₱1000 from the bank account of Aldous to the
successful, all other operations will be undone. account of Brendon:
• A database request is the equivalent of a single SQL statement in 1. read Aldous's account
an application program or transaction. 2. Aldous's account = Aldous’s account – ₱1000
o If a transaction has three (3) update statements and one (1) 3. Write changes in Aldous's account
insert statement, the transaction uses four (4) database 4. Read Brendon's account
requests. 5. Brendon’s account = Brendon's account + ₱1000
• Database consistent state – satisfies the constraints specified in the 6. Write changes in Brendon's account
schema.
o Assume that a specific table contains a gender column where Atomicity requirement: If the transaction fails after step 3, and
it only accepts an entry having values of “Male” and “Female”. before step 6, the system should ensure that its updates are not
If a user attempts to enter a value of ‘Person’, then the database reflected in the database, an inconsistency will result.
will disallow the entry of such a value. Consistency requirement: The sum of Aldous' account and
• Transaction log – A DBMS uses this to keep track of all the transactions Brendon's account is unchanged by the execution of the
that update the database. transaction. If between steps 3 and 6, another transaction is allowed
o In the case of system failure, this log helps bring the database to access the partially updated database, it will see an inconsistent
back to a consistent state. database.
Isolation requirement: Transactions should be run sequentially.
Properties of transactions Therefore, no other transaction will happen on both accounts until
• Atomicity – requires that all operations (SQL requests) of a the current transaction is completed.
transaction should be completed. Durability requirement: Once the user has been notified that the
Example: Transaction T1 has four (4) SQL requests that must be transaction has completed (i.e., the transfer of the P1000 has taken
successfully completed. Otherwise, the entire transaction is place), the updates to the database by the transaction must persist
aborted. despite failures.
• Consistency – ensures that only valid data following all rules and
constraints will be written in the database. When a transaction SQL Transactional Commands
results in invalid data, the database reverts to its previous state • BEGIN TRANSACTION - This marks the beginning of transaction
execution.
--Transaction 2 SERIALIZABLE
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
INSERT INTO Stocks VALUES (3, 'Iphone 11', 3)
SELECT * FROM Stocks
WAITFOR DELAY '00:00:01'
COMMIT
4. A SERIALIZABLE isolation
o Ensures that the data that one transaction has read, will be
prevented from being updated or deleted by any other
transaction.
o Most restrictive level and gives solution to the phantom read.
problem.
Example: Using the Stocks (table 1)
Explanation:
REFERENCES
Coronel, C. and Morris, S. (2018). Database systems design,
implementation, & management (13th ed.). Cengage Learning.