0% found this document useful (0 votes)
5 views41 pages

04 Transaction Replication

Uploaded by

vyvh.22bi13485
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views41 pages

04 Transaction Replication

Uploaded by

vyvh.22bi13485
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

TRANSACTION

Lê Hồng Hải
UET-VNUH
 A bank customer transfers money from his
savings account to his current account
 A transaction to add new sales order:
1. Insert a new sales order into the orders table
for a given customer.
2. Insert new sales order items into
the orderdetails table
 Now,imagine what would happen if
one or more steps above fail due to
some reasons
 Transaction allows you to execute a set of
operations to ensure that the database
never contains the result of partial
operations
 If one of them fails, the rollback occurs to
restore the database to its original state
 If no error occurs, the entire set of
statements is committed to the database
-- 1. start a new transaction
START TRANSACTION;
-- 2. Get the latest order number
SELECT
@orderNumber:=MAX(orderNUmber)+1
FROM
orders;
-- 3. insert a new order for customer 145
INSERT INTO orders(orderNumber,
orderDate,
requiredDate,
shippedDate,
status,
customerNumber)
VALUES(@orderNumber,
'2005-05-31',
'2005-06-10',
'2005-06-11',
'In Process',
145);
-- 4. Insert order line items
INSERT INTO orderdetails(orderNumber,
productCode,
quantityOrdered,
priceEach,
orderLineNumber)
VALUES(@orderNumber,'S18_1749', 30, '136', 1),
(@orderNumber,'S18_2248', 50, '55.09', 2);

-- 5. commit changes
COMMIT;
 The SAVEPOINT command defines a marker
in a transaction
 The ROLLBACK TO SAVEPOINT command
allows rolling back to a previous marker
 MySQL InnoDB storage engine
supports transactions
 MyISAM does not support transactions
 ACID (Atomic, Consistent, Isolated,
Durable)

https://www.ibm.com/docs/en/cics-ts/5.4?
topic=processing-acid-properties-
transactions
 http://www.mysqltutorial.org/mysql-transac
tion.aspx
 http://www.mysqltutorial.org/mysql-jdbc-tra
nsaction/
 The two-phase commit protocol provides
atomicity for distributed transactions to
ensure that each participant in the
transaction agrees on whether the
transaction should be committed or not
 In databases, multiple users can view
and edit data simultaneously
 Concurrent operations can result in
inconsistent and inaccurate data
 Dirty read occurs when a transaction is allowed to read data
being updated by another uncommitted transaction
 Occurs when within a transaction, two
identical queries return different sets of
rows when executed
Nonrepea
Dirty Phantom
Isolation Levels Usage table
reads reads
reads

Use in situations where


READ UNCOMMITTED accuracy is not so Yes Yes Yes
important

READ COMMITTED Prevent dirty reads No Yes Yes

Default Isolation level in


REPEATABLE-READ No No Yes
MySQL
Transactions are
completely isolated
SERIALIZABLE from each other and No No No
are processed
sequentially
 To decide the isolation level to use, it is
necessary to balance between the required
level of accuracy of the retrieved data and
the processing performance
 The higher the isolation level, the more
impact it has on performance
 SET SESSION tx_isolation='READ-COMMITTED';
 SELECT @@global.tx_isolation, @@session.tx_isolation

Mysql8 has renamed tx_isolation to transaction_isolation.


 MySQL uses MVCC in transaction
management
 MVCC provides each connection to the
database with a snapshot of the data
 Any changes will not be visible to other users
until the transaction is committed

http://en.wikipedia.org/wiki/
Multiversion_concurrency_control
 A deadlock happens when two or more
transactions are mutually holding and
requesting locks on the same resources,
creating a cycle of dependencies
 To solve this problem, database
systems implement various forms
of deadlock detection and
timeout
 The InnoDB storage engine will
notice circular dependencies and
return an error instantly
SELECT * from performance_schema.data_locks;
 To lock tables that do not support
transactions, use the LOCK TABLES
statement
 Once you have completed updating the
tables, you need to use the UNLOCK
TABLES statement to release the tables
REPLICATION
 Updates to one database are automatically
replicated to other replicas
 Updates to the master server (primary)
 Queries that read data can be assigned to
the master server or slave servers
(replicas)
 Availability: Replicas can be used as "hot"
backups; if the master database is not
available, the replicas can take over as the
master until the error is resolved
 Backups: Replicas can be used as backups,
which can be used to perform long backups
without locking the master
 Load Balancing: Read queries can be
distributed to different replicas
 In MySQL, replication is a one-way,
asynchronous process
 The master database will store all updates
in a binary log file. Updates in the log file
are then used to synchronize the database
on the slave server
 Slave servers connect to the master server
to read log files and update changes
 Statement-based binary logging: the
master writes SQL statements to the
binary log
 Row-based logging: the master
writes events to the binary log that
indicate how individual table rows are
changed
 Depending on certain statements, and
also the storage engine being used, the log
is automatically switched to row-based in
particular cases
CHANGE MASTER TO MASTER_HOST='192.168.0.100',
MASTER_USER='slave_user',
MASTER_PASSWORD='<some_password>',
MASTER_LOG_FILE='mysql-bin.006',
MASTER_LOG_POS=183;
 START SLAVE;
 SHOW SLAVE STATUS
 While the master is blocking (waiting for
acknowledgment from a slave), it does not
return to the session that performed the
transaction
 When the block ends, the master returns to
the session, which then can proceed to
execute other statements
 Implements a multi-master update
everywhere replication protocol.
THANKS
YOU

You might also like