Lecture 05.7 SQL Transactions and Concurrency Management_26
Lecture 05.7 SQL Transactions and Concurrency Management_26
and concurrency
TRANSACTION MANAGEMENT AND CONCURRENCY
1 / 27
Transaction
Management
Refer Joel Murach, Murach’s MySQL, 2nd Edition, 2015, ISBN-13: 978-1890774820
START TRANSACTION;
INSERT INTO invoices VALUES (115, 34, 'ZXA-080', '2015-01-18', 14092.59, 0, 0, 3, '2015-04-18', NULL);
INSERT INTO invoice_line_items VALUES (115, 1, 160, 4447.23, 'HW upgrade');
INSERT INTO invoice_line_items VALUES (115, 2, 167, 9645.36, 'OS upgrade');
IF sql_error = FALSE
THEN COMMIT;
SELECT 'The transaction was committed.';
ELSE
ROLLBACK;
SELECT 'The transaction was rolled back.’;
END IF;
END//
4 / 27
SAVEPOINT to Manage Transactions
• Save points can be used to roll back a
transaction to the beginning of the transaction => INSERT INTO product_key VALUES (AAA);
or to a particular save point => SAVEPOINT my_savepoint;
• You can use the SAVEPOINT statement to create a
save point with the specified name => INSERT INTO product_key VALUES (BBB);
• You can use the ROLLBACK TO SAVEPOINT => INSERT INTO product_key VALUES (CCC);
statement to roll back a transaction to the => ROLLBACK TO SAVEPOINT my_savepoint;
specified save point
=> INSERT INTO product_key VALUES (DDD);
• Save points are useful when a single transaction => COMMIT;
contains so many SQL statements that rolling
back the entire transaction would be inefficient
This rolls back the values BBB and CCC that
• In that case, an application can roll back to the
last save point before an error occurred. were entered after the savepoint, my_savepoint,
• Then, the appropriate processing can be done was established.
from there. Only the values AAA and DDD are inserted at
• For most applications, though, you won’t need to commit.
use save points.
5 / 27
Save point – a depiction in time
Pgm A Pgm B Pgm C Pgm D
(
(
(
(
(
Save Point
(
(
(
Save Point
(
(
(
(
Save Point
(
(
6 / 27
CONCURRENCY AND LOCKING
• When two or more users have access to the same database, it’s possible for them to be
working with the same data at the same time - this is called concurrency
• Most database systems provide ability to support two or more transactions that are working with the
same data at the same time
7 / 27
Example: Concurrent Transactions
Two transactions that retrieve and then modify the data in the same row
Transaction A Transaction B
START TRANSACTION;
UPDATE invoices SET credit_total =
--Use a second connection to execute these
credit_total + 100 WHERE invoice_id = statements! --Otherwise, they might not work
6;
as described.
START TRANSACTION;
Waits
--the SELECT statement in Transaction B
won't show the updated data --the T SELECT invoice_id, credit_total FROM
UPDATE statement in Transaction B will invoices WHERE invoice_id = 6;
wait for transaction A to finish i
m UPDATE invoices SET credit_total =
COMMIT;
e credit_total + 200 WHERE invoice_id = 6;
--the SELECT statement in Transaction B
will display the updated data --the COMMIT;
UPDATE statement in Transaction B will
execute immediately
8 / 27
Example: Concurrent Transactions
Two transactions that retrieve and then modify the data in the same row
9 / 27
Four Types of Concurrency Problems
• In a large system with many users, you should expect for
these kinds of problems to occur. In general, you don’t
need to take any action except to anticipate the
problem. In many cases, if the SQL statement is
resubmitted, the problem goes away.
10 / 27
Lost update
1. Transaction 1 reads the items in
stock for laptops which is 12. A little
later transaction 2 reads the value
for ItemsinStock for laptops which
will still be 12 at this point of time.
Transaction 2 then sells three
laptops, shortly before transaction 1
sells 2 items.
2. Transaction 2 will then complete its
execution first and update
ItemsinStock to 9 since it sold three
of the 12 laptops. Transaction 1
commits itself. Since transaction 1
sold two items, it updates
ItemsinStock to 10.
3. This is incorrect, the correct figure is
Update is lost 12-3-2 = 7
Courtesy : CodingInsigt.com
11 / 27
Dirty read 1. Transaction 1 will perform the purchase task for
the user. The first step in the transaction will be
to update the ItemsinStock.
ItemsinStock = 12
2. Before the transaction, there are 12 items in the
stock; the transaction will update this to 11. The
transaction will now communicate with an
external billing gateway.
12 / 27
Non repeatable reads
13 / 27
Phantom Reads Courtesy : vladmihalcea.com
1. Alice and Bob start two
database transactions.
2. Bob’s reads all the
post_comment records
associated with the post
row with the identifier value
of 1.
3. Alice adds a new
post_comment record
which is associated with the
post row having the
identifier value of 1.
4. Alice commits her database
transaction.
5. If Bob’s re-reads the
post_comment records
having the post_id column
value equal to 1, he will
Phantom observe a different version
Read of this result set.
Courtesy : vladmihalcea.com
14 / 27
Changing Locking Behavior to Prevent
Concurrency Problems
• The transaction isolation level controls the degree to which transactions are isolated from one another. At the
more restrictive isolation levels, concurrency problems are reduced or eliminated. However, at the least
restrictive levels, performance is enhanced.
• To change the transaction isolation level, you use the SET TRANSACTION ISOLATION LEVEL statement.
• If you include the GLOBAL keyword, the isolation level is set globally for all new transactions in all sessions. If
you include the SESSION keyword, the isolation level is set for all new transactions in the current session. If you
omit both GLOBAL and SESSION, the isolation level is set for the next new transaction in the current session.
• The default transaction isolation level is REPEATABLE READ. This level places locks on all data that’s used in a
transaction, preventing other users from updating that data. However, this isolation level still allows inserts, so
phantom reads can occur.
• The READ UNCOMMITTED isolation level doesn’t set any locks and ignores locks that are already held. This level
results in the highest possible performance for your query, but at the risk of every kind of concurrency problem.
For this reason, you should only use this level for data that is rarely updated.
• The READ COMMITTED isolation level locks data that has been changed but not committed. This prevents dirty
reads but allows all other types of concurrency problems.
• The SERIALIZABLE isolation level places a lock on all data that’s used in a transaction. Since each transaction
must wait for the previous transaction to commit, the transactions are handled in sequence. This is the most
restrictive isolation level.
15 / 27
Changing Locking Behavior to Prevent Concurrency Problems
16 / 27
Optimistic locking versus pessimistic locking
Courtesy : vladmihalcea.com
17 / 27
Optimistic locking versus pessimistic locking
Courtesy : vladmihalcea.com
18 / 27
Deadlock or “deadly embrace”
Alice Database Bob
Begin Transaction
Begin Transaction
Update Product ID = 12
Update Customer ID = 24
Update Product ID = 12
Update Customer ID = 24
Courtesy : vladmihalcea.com
19 / 27
Preventing Deadlocks
• A deadlock occurs when neither of two transactions can be committed because each has a lock
on a resource needed by the other transaction.
UPDATE statements that illustrate deadlocking
Transaction A
START TRANSACTION; How to prevent deadlocks
UPDATE savings SET balance = balance - transfer_amount;
UPDATE checking SET balance = balance + transfer_amount; • Don’t allow transactions to remain open for very long.
COMMIT;
• Don’t use a transaction isolation level higher than necessary.
Transaction B (possible deadlock)
START TRANSACTION;
• Make large changes when you can be assured of nearly exclusive
UPDATE checking SET balance = balance - transfer_amount; access.
UPDATE savings SET balance = balance + transfer_amount;
COMMIT;
• Consider locking when coding your transactions.
Transaction B (prevents deadlocks) • Revisit the sequence of your transactions.
START TRANSACTION;
UPDATE savings SET balance = balance + transfer_amount;
UPDATE checking SET balance = balance - transfer_amount;
COMMIT;
20 / 27
Prevent Deadlocks, contd…
• Don’t allow transactions to remain open for very long
• Keep transactions short
• Keep SELECT statements outside of the transaction except when absolutely necessary.
• Never code requests for user input during a transaction.
• Make large changes when you can be assured of nearly exclusive access
• If you need to change millions of rows in an active table, don’t do so during hours of peak usage.
• If possible, give yourself exclusive access to the database before making large changes.
21 / 27
Distributed transactions – Problem?
A monolithic system decomposed into self-encapsulated services, it can break transactions.
This means a local transaction in the monolithic system is now distributed into multiple services that will
be called in a sequence.
Courtesy: developers.redhat.com
22 / 27
Distributed transactions – Problem? Contd..
A monolithic system decomposed into self-encapsulated services, it can break transactions.
This means a local transaction in the monolithic system is now distributed into multiple services that will
be called in a sequence.
Courtesy: developers.redhat.com
23 / 27
Distributed transactions – 2 Phase Commit – A solution
• When the user sends a put order request, the
Coordinator first creates a global transaction with all
the context information.
• It will then tell CustomerMicroservice to prepare for
updating a customer fund with the created
transaction.
• The CustomerMicroservice will then check, for
example, if the customer has enough funds to
proceed with the transaction.
• Once CustomerMicroservice is OK to perform the
change, it will lock down the object from further
changes and tell the Coordinator that it is prepared.
• The same thing happens while creating the order in
the OrderMicroservice.
• Once the Coordinator has confirmed all microservices
are ready to apply their changes, it will then ask them
to apply their changes by requesting a commit with
the transaction.
• At this point, all objects will be unlocked. Courtesy: developers.redhat.com
24 / 27
Distributed transactions – 2 Phase Commit – A solution
Courtesy: developers.redhat.com
25 / 27
Distributed transactions – Saga Pattern– Another solution
26 / 27
Distributed transactions – Saga Pattern– Another solution
27 / 27