34
1 cs236607

1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

Embed Size (px)

Citation preview

Page 1: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

1cs236607

Page 2: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

2

Intersection of Concurrent AccessesA fundamental property of Web sites:

Concurrent accesses by multiple usersConcurrent accesses intersect at

Database accesses (read / write)Execution of application code (Servlets / JSP)

Users activate the same application objects:Database

Shared connection / connection poolServlet / JSP object

For each Servlet / JSP, only one object is created

cs236607

Page 3: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

3

Concurrency HazardsWhat are the potential problems?Inconsistency due to concurrent accesses

For example, generating IDs, verifying uniqueness of names, presenting information while being updated

Transactions: Want to consider a sequence of operations as an atomic action Until commitment, updates are partial (inconsistent)On failure, the origin state is restored (rollback) How do we disable the server from relying on

partial information or such that may later be cancelled due to roll back?

cs236607

Page 4: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

4

Handling ConcurrencyFor managing concurrency in the level of Java

code, use known Java toolsSynchronization, wait / notify, etc.

For handling concurrency in database accesses, transactions are used

Concurrent transactions use distinct connections

But (generating) connections are expensive!Use thread poolsShare connections whenever possible (e.g., simple

reads)cs236607

Page 5: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

5

Thumbnail RulesSome concurrency problems can be solved at

both the code and database levels (what about client-side programming?)

In principle, prefer letting the database handle concurrency Database research and vendors have put a lot

of effort on handling concurrent transactionsBut you need to understand the exact model of

transaction management that is implemented by your specific database engineNext, transaction management in MySQL

cs236607

Page 6: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

6cs236607

Page 7: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

7

Transactions and JDBCTransaction:Transaction: A sequence of statements that

must all succeed (or all fail) togethere.g., updating several tables due to customer

purchaseFailure: System must reverse all previous

actionsCannot leave DB in inconsistent state halfway

through a transactionCOMMIT = complete transactionROLLBACK = cancel all actions

cs236607

Page 8: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

cs236607 8

Atomicity

Consistency

Isolation

Durability

Page 9: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

ACIDAtomicity: transactions are atomic and

indivisibleConsistency: operations transform the

database from one valid state to another valid state

Isolation: transactions do not affect each other while they are running

Durability: the changes to the database of a transaction that has committed are permanent

cs236607 9

Page 10: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

10

An ExampleSuppose that we want to transfer money

from bank account 13 to account 72:

PreparedStatement pstmt = con.prepareStatement("update BankAccount set amount = amount + ? where accountId = ?");

pstmt.setInt(1,-100); pstmt.setInt(2, 13);pstmt.executeUpdate();

pstmt.setInt(1, 100); pstmt.setInt(2, 72);pstmt.executeUpdate();

What happens if this update fails?

cs236607

Page 11: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

11

Transaction Life CycleThrough JDBC, transactions are not opened and

closed explicitlyA transaction starts on 1st (successful) command

After a connection is establishedAfter the previous transaction ends

A transaction ends when COMMIT or ROLLBACK are appliedEither explicitly or implicitly (next slides)

Multiple transactions are obtained by using multiple connections to the database

cs236607

Page 12: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

12

Committing a TransactionHow do we commit?

Explicitly invoking Connection.commit()Implicitly

After every query execution, if AutoCommit is true

When the user normally disconnects (i.e., appropriately closes the connection)

In some DBs: After invoking a DDL command (CREATE, DROP, RENAME, ALTER, …)

cs236607

Page 13: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

13

Automatic CommitmentA Connection object has a boolean AutoCommitIf AutoCommit is true (default), then every

statement is automatically committed If AutoCommit is false, then each statement is

added to an ongoing transactionChange using setAutoCommit(boolean)If AutoCommit is false, need to explicitly commit

or rollback the transaction using Connection.commit() and Connection.rollback()

cs236607

Page 14: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

14

Rolling BackRolling Back: Undoing any change to

data within the current transactionThe ROLLBACK command explicitly

rolls back (and ends) the current transaction

ROLLBACK is implicitly applied when the user abnormally disconnects (i.e., without appropriately closing the connection)

cs236607

Page 15: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

15

Fixed Examplecon.setAutoCommit(false);

try {

PreparedStatement pstmt = con.prepareStatement("update BankAccount

set amount = amount + ?

where accountId = ?");

pstmt.setInt(1,-100); pstmt.setInt(2, 13); pstmt.executeUpdate();

pstmt.setInt(1, 100); pstmt.setInt(2, 72); pstmt.executeUpdate();

con.commit();

catch (SQLException e) { con.rollback(); …; }

cs236607

Page 16: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

16cs236607

Page 17: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

17

Isolation IssuesHow do different transactions

interact?

Does a running transaction see uncommitted changes?

Does it see committed changes?

Can two transactions update the same row?Next part …

cs236607

Page 18: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

18

Isolation LevelsThe isolation level determines the capabilities of

a transaction to read/write data that is accessed by other transactions

In MySQL, there are four levels of isolation:1. READ UNCOMMITTED2. READ COMMITTED3. REPEATABLE READ (default)4. SERIALIZABLE

Each transaction determines its isolation levelConnection.setTransactionIsolation(int level)

cs236607

Page 19: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

19

READ COMMITED & SERIALIZABLE

SERIALIZABLE: During the whole transaction, statements read only the changes that were committed by the time the transaction begun (and the changes made by the transaction itself)

REPEATABLE READ: each transaction gets to work in an isolated version of the table, where each row remains as it was when the transaction started

cs236607

Page 20: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

20

READ COMMITED & SERIALIZABLE

READ COMMITTED: A statement reads the data that was committed by the time the statement (not the transaction) begun

READ UNCOMMITTED : It is possible for transactions to read changes that other transactions have made before the changes have been committed (practically, no transactions)

cs236607

Page 21: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

21

Some DefinitionsDirty reads:Dirty reads: A transaction reads data that is

written by another, uncommitted transaction

Non-repeatable reads: Non-repeatable reads: A transaction rereads data it previously read and finds that a committed transaction has modified or deleted that data

Phantom reads:Phantom reads: A transaction re-executes a query returning a set of rows satisfying a search condition and finds that a committed transaction inserted additional rows that satisfy the condition

cs236607

Page 22: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

22

ComparisonREAD READ

UNCOMMITED UNCOMMITED READ READ

COMMITED COMMITED REPEATABLE REPEATABLE

READREAD SERIALIZABLESERIALIZABLE

Dirty Reads Possible Impossible Impossible Impossible

Non-repeatable Reads

Possible Possible Impossible Impossible

Phantom Reads Possible Possible

Possible (not likely)

Impossible

cs236607

Page 23: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

23

ComparisonREAD READ

UNCOMMITED UNCOMMITED READ READ

COMMITED COMMITED REPEATABLE REPEATABLE

READREAD SERIALIZABLESERIALIZABLE

Dirty Reads Possible Impossible Impossible Impossible

Non-repeatable Reads

Possible Possible Impossible Impossible

Phantom Reads Possible Possible

Possible (not likely)

Impossible

cs236607

Page 24: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

24

What Happens Here (1)?1.1. CREATE TABLE pairs (x INTEGER, y INTEGER);

2. 2. select * from pairs3.3. insert into pairs values(1,1)

6. 6. select * from pairs

8.8. COMMIT

2. 2. select * from pairs3.3. insert into pairs values(1,1)

6. 6. select * from pairs

8.8. COMMIT

4. 4. select * from pairs5.5. insert into pairs values(1,2)

7. 7. select * from pairs

9.9. COMMIT

4. 4. select * from pairs5.5. insert into pairs values(1,2)

7. 7. select * from pairs

9.9. COMMITT.1: R. COMMITTED

T.2: SERIALIZABLEcs236607

Page 25: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

25

What Happens Here (2)?1.1. CREATE TABLE pairs (x INTEGER, y INTEGER);

T.1: R. COMMITTED

T.2: SERIALIZABLE

3.3. insert into pairs values(1,1)4.4. COMMIT5. 5. select * from pairs

9. 9. select * from pairs10. 10. COMMIT

3.3. insert into pairs values(1,1)4.4. COMMIT5. 5. select * from pairs

9. 9. select * from pairs10. 10. COMMIT

2. 2. select * from pairs

6. 6. select * from pairs7.7. insert into pairs values(1,2)8.8. COMMIT

2. 2. select * from pairs

6. 6. select * from pairs7.7. insert into pairs values(1,2)8.8. COMMIT

cs236607

Page 26: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

26

What Happens Here (3)?1.1. CREATE TABLE pairs (x INTEGER, y INTEGER);

2.2. insert into pairs values(1,1)

4.4. COMMIT5. 5. select * from pairs

9. 9. select * from pairs10. 10. COMMIT

2.2. insert into pairs values(1,1)

4.4. COMMIT5. 5. select * from pairs

9. 9. select * from pairs10. 10. COMMIT

3. 3. select * from pairs

6. 6. select * from pairs7.7. insert into pairs values(1,2)8.8. COMMIT

3. 3. select * from pairs

6. 6. select * from pairs7.7. insert into pairs values(1,2)8.8. COMMIT

T.1: SERIALIZABLE

T.2: SERIALIZABLE

Is it equivalent to any truly serial execution of the transactions?

cs236607

Page 27: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

27cs236607

Page 28: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

28

LocksMySQL controls concurrent access to data by

deploying locksEach operation on the database (select,

update, insert, create, drop, etc.) requires some lock to be obtained by the running transactionIf a lock is not available, the transaction is

blockedLocks are applied to either whole tables or to

(sets of) specific rows

cs236607

Page 29: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

29

Table-Level Locks

Two types of table locksRead (shared): Can be held by multiple

transactionsWrite (exclusive): Other transactions cannot

have this lock (shared or exclusive)

cs236607

Page 30: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

30

Locking a TableTo lock the table mytbl, use the command

Acquires the write (exclusive) lockThus, no transaction can read or update the

tableThis lock is also needed in ALTER TABLE & DROP

TABLEThis operation blocks until all locks on the

table are released

LOCK TABLES mytbl AS alias write

cs236607

Page 31: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

31

Concurrent UpdatesMySQL prevents updating a row that is being

updated by an uncommitted transactionLocks are held until the transaction endsThe second updating transaction is blocked

until the lock is released (first one commits or rolls back)BEGIN

UPDATE r

END

BEGIN

UPDATE r

END

T1 T2

T2 READ COMMITTED: Update applied when T1

terminates

T2 SERIALIZABLE: T1 commits: Update failsT1 rolls back: Succeeds

cs236607

Page 32: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

32

What Happens Here (5)?1.1. CREATE TABLE pairs (x INTEGER, y INTEGER);

2.2. insert into pairs values(1,1)3.3. COMMIT

5. 5. update pairs set y=3 where x=1

8. 8. select * from pairs9. 9. COMMIT

2.2. insert into pairs values(1,1)3.3. COMMIT

5. 5. update pairs set y=3 where x=1

8. 8. select * from pairs9. 9. COMMIT

4. 4. update pairs set y=2 where x=1

6. 6. select * from pairs7.7. COMMIT

4. 4. update pairs set y=2 where x=1

6. 6. select * from pairs7.7. COMMIT

T.1: R. COMMITTED

T.2: SERIALIZABLE

cs236607

Page 33: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

33

What Happens Here (4)?1.1. CREATE TABLE pairs (x INTEGER, y INTEGER);

2.2. insert into pairs values(1,1)3.3. COMMIT

5. 5. update pairs set y=3 where x=1

??????

2.2. insert into pairs values(1,1)3.3. COMMIT

5. 5. update pairs set y=3 where x=1

??????

4. 4. update pairs set y=2 where x=1

6. 6. select * from pairs7.7. COMMIT

4. 4. update pairs set y=2 where x=1

6. 6. select * from pairs7.7. COMMIT

T.1: SERIALIZABLE

T.2: SERIALIZABLE

cs236607

Page 34: 1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect

Linkshttp://dev.mysql.com/books/mysqlpress/mysql

-tutorial/ch10.htmlhttp://dev.mysql.com/doc/refman/5.0/en/trans

actional-statements.html

cs236607 34