Practica controlconcurrencia

1,574 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,574
On SlideShare
0
From Embeds
0
Number of Embeds
968
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Practica controlconcurrencia

  1. 1. Bases de Datos.SET AUTOCOMMIT = {0 | 1}Si el modo de autocommit está en apagado SET AUTOCOMMIT = 0, entonces seasume que la misma transacción continua hasta que se realice un COMMIT o unROLLBACK. Por default el modo de autocommit está encendido, de manera que cadaoperación es considerada una transacción y todo cambio se va reflejandoautomáticamente en la base de datos.Partiendo del esquema siguiente se presentan las acciones de 2 transacciones concurrentes.mysql> desc bank; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | | PRI | 0 | | | debit | float | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.08 sec) T1 T2 mysql> set autocommit=0; mysql> set autocommit=0; Query OK, 0 rows affected (0.09 Query OK, 0 sec) rows affected (0.00 sec) mysql> select * from bank; mysql> select * from bank; +------+-------+ +------ | id | debit | +-------+ +------+-------+ | id | debit | | 32 | 999 | +------ | 64 | 7865 | +-------+ +------+-------+ | 32 | 999 | 2 rows in set | 64 | 7865 | (0.00 sec) +------ mysql> insert into bank +-------+ values(66,3453); 2 rows in set Query OK, 1 row (0.00 sec) affected (0.09 sec) mysql> select * from bank; +------ +-------+ | id | debit | +------ +-------+ | 32 | 999 | | 64 | 7865 | +------ +-------+ 2 rows in set (0.15 sec) mysql> select * from bank; mysql> select * from bank; +------ +------+-------+ +-------+ | id | debit | | id | debit | +------+-------+ +------ | 32 | 999 | +-------+ | 64 | 7865 | | 32 | 999 | 1
  2. 2. Bases de Datos. | 66 | 3453 | | 64 | 7865 | +------+-------+ +------ 3 rows in set +-------+ (0.00 sec) 2 rows in set mysql> delete from bank where (0.00 sec) id=64; Query OK, 1 row affected (0.12 sec) mysql> select * from bank; +------+-------+ | id | debit | +------+-------+ | 32 | 999 | | 66 | 3453 | +------+-------+ 2 rows in set mysql> select * from bank; (0.00 sec) +------ +-------+ | id | debit | mysql> commit; +------ Query OK, 0 rows affected (0.07 +-------+ sec) | 32 | 999 | | 64 | 7865 | +------ +-------+ mysql> set autocommit=0; 2 rows in set Query OK, 0 rows affected (0.00 (0.00 sec) sec) mysql> commit; mysql> select * from bank; Query OK, 0 rows affected +------+-------+ (0.00 sec) | id | debit | +------+-------+ mysql> set autocommit=0; | 32 | 999 | Query OK, 0 rows affected | 66 | 3453 | (0.00 sec) +------+-------+ mysql> select * from bank; 2 rows in set +------ (0.00 sec) +-------+ mysql> rollback; | id | debit | Query OK, 0 rows +------ affected (0.00 sec) +-------+ | 32 | 999 | | 66 | 3453 | +------ +-------+ 2 rows in set (0.00 sec) mysql> rollback; Query OK, 0 rows affected (0.01 sec) mysql> set autocommit=0; mysql> set autocommit=0; Query OK, 0 rows Query OK, 0 affected (0.01 sec) rows affected (0.00 sec) mysql> select * from bank where mysql> update bank set id=32 for update; debit=666 where id=32; +------+-------+ . | id | debit | . +------+-------+ . | 32 | 999 | . 2
  3. 3. Bases de Datos. +------+-------+ . 1 row in set . (0.00 sec) .Wait unlock mysql> rollback; . Query OK, 0 rows . affected (0.00 sec) . . mysql> update bank set debit=666 where id=32; Query OK, 1 row affected (13.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from bank; +------+-------+ mysql> select * from bank; | id | debit | +------ +------+-------+ +-------+ | 32 | 999 | | id | debit | | 66 | 3453 | +------ +------+-------+ +-------+ 2 rows in set | 32 | 666 | (0.00 sec) | 66 | 3453 | +------ +-------+ 2 rows in set (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.07 sec) mysql> select * from bank; +------+-------+ | id | debit | mysql> select * from bank; +------+-------+ +------ | 32 | 999 | +-------+ | 66 | 3453 | | id | debit | +------+-------+ +------ 2 rows in set +-------+ (0.00 sec) | 32 | 666 | | 66 | 3453 | mysql> commit; +------ Query OK, 0 rows +-------+ affected (0.07 sec) 2 rows in set mysql> select * from bank; (0.01 sec) +------+-------+ | id | debit | +------+-------+ | 32 | 666 | | 66 | 3453 | mysql> select * from bank; +------+-------+ +------ 2 rows in set +-------+ (0.01 sec) | id | debit | +------ +-------+ | 32 | 666 | | 66 | 3453 | +------ +-------+ 2 rows in set (0.01 sec) 3
  4. 4. Bases de Datos. mysql> set autocommit=0; mysql> set autocommit=0; Query OK, 0 rows Query OK, 0 affected (0.00 sec) rows affected (0.00 sec) mysql> select * from bank where mysql> update bank set id=32 lock in share mode; debit=777 where id=32; +------+-------+ . | id | debit | . +------+-------+ . | 32 | 666 | .>Wait unlock +------+-------+ . 1 row in set . (0.00 sec) . mysql> update bank set . debit=888 where id=32; . ERROR 1213 (40001): Deadlock . found when trying to get lock; . Try restarting transaction . . . . >T1 tiene bloqueado a 32, T2 . espera la liberacion . >al hacer el update en T1 ahora . T1 espera a T2 mysql> update bank set >por lo tanto se produce un debit=777 where id=32; deadlock Query OK, 1 >el dbms hace rollback en T1 row affected (23.10 sec) (ultimo en espera) y procesa T2 Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from bank; +------ +-------+ | id | debit | mysql> select * from bank; +------ +------+-------+ +-------+ | id | debit | | 32 | 777 | +------+-------+ | 66 | 3453 | | 32 | 666 | +------ | 66 | 3453 | +-------+ +------+-------+ 2 rows in set 2 rows in set (0.00 sec) (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql> select * from bank; +------ +-------+ mysql> select * from bank; | id | debit | +------+-------+ +------ | id | debit | +-------+ +------+-------+ | 32 | 777 | | 32 | 666 | | 66 | 3453 | | 66 | 3453 | +------ +------+-------+ +-------+ 2 rows in set 2 rows in set (0.00 sec) (0.00 sec) mysql> rollback; Query OK, 0 rows affected (0.00 sec) mysql> select * from bank; 4
  5. 5. Bases de Datos. +------+-------+ | id | debit | +------+-------+ | 32 | 777 | | 66 | 3453 | +------+-------+ 2 rows in set (0.01 sec) mysql> set autocommit=0; mysql> set autocommit=0; Query OK, 0 rows Query OK, 0 affected (0.00 sec) rows affected (0.00 sec) mysql> select * from bank where mysql> select * from bank id=32 for update; where id=32 for update; +------+-------+ . | id | debit | . +------+-------+ . | 32 | 777 | . +------+-------+ . 1 row in set . (0.00 sec) .> Wait unlock mysql> update bank set . debit=111 where id=32; . Query OK, 1 row . affected (0.00 sec) . Rows matched: 1 . Changed: 1 Warnings: 0 mysql> select * from bank where id=32 for update; mysql> commit; +------ Query OK, 0 rows +-------+ affected (0.01 sec) | id | debit | +------ +-------+ | 32 | 111 | +------ +-------+ 1 row in set mysql> select * from bank; (32.45 sec) +------+-------+ | id | debit | +------+-------+ | 32 | 111 | | 66 | 3453 | mysql> rollback; +------+-------+ Query OK, 0 2 rows in set rows affected (0.00 sec) (0.00 sec) 5
  6. 6. Bases de Datos. mysql> set autocommit=0; Query OK, 0 rows affected (0.00 sec) mysql> update bank set debit=000 where id=32; mysql> select * from bank; Query OK, 1 row +------ affected (6.88 sec) +-------+ Rows matched: 1 | id | debit | Changed: 1 Warnings: 0 +------ mysql> select * from bank; +-------+ +------+-------+ | 32 | 111 | | id | debit | | 66 | 3453 | +------+-------+ +------ | 32 | 0 | +-------+ | 66 | 3453 | 2 rows in set +------+-------+ (0.00 sec) 2 rows in set (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql> select * from bank; +------+-------+ | id | debit | +------+-------+ | 32 | 0 | | 66 | 3453 | +------+-------+ 2 rows in set (0.00 sec) mysql> select * from bank; +------ +-------+ | id | debit | +------ +-------+ | 32 | 0 | | 66 | 3453 | +------ +-------+ 2 rows in set (0.00 sec) mysql> set autocommit=0; mysql> set autocommit=0; Query OK, 0 rows Query OK, 0 affected (0.00 sec) rows affected (0.00 sec) mysql> select * from bank 6
  7. 7. Bases de Datos. where id=32 for update; +------ +-------+ | id | debit | +------ mysql> update bank set +-------+ debit=333 where id=32; | 32 | 0 | . +------ . +-------+ . 1 row in set . (0.00 sec) . .> Wait unlock . . . . . mysql> commit; mysql> update bank set Query OK, 0 debit=333 where id=32; rows affected (0.00 sec) Query OK, 1 row affected (3.20 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from bank; +------+-------+ | id | debit | +------+-------+ | 32 | 333 | | 66 | 3453 | mysql> select * from bank; +------+-------+ +------ 2 rows in set +-------+ (0.00 sec) | id | debit | mysql> commit; +------ Query OK, 0 rows +-------+ affected (0.00 sec) | 32 | 0 | mysql> select * from bank; | 66 | 3453 | +------+-------+ +------ | id | debit | +-------+ +------+-------+ 2 rows in set | 32 | 333 | (0.00 sec) | 66 | 3453 | +------+-------+ 2 rows in set (0.00 sec) mysql> select * from bank; +------ +-------+ | id | debit | +------ +-------+ | 32 | 0 | | 66 | 3453 | +------ +-------+ 2 rows in set (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) 7
  8. 8. Bases de Datos. mysql> select * from bank; +------ +-------+ | id | debit | +------ +-------+ | 32 | 333 | | 66 | 3453 | +------ +-------+ 2 rows in set (0.00 sec) mysql> set autocommit=0; mysql> set autocommit=0; Query OK, 0 rows Query OK, 0 affected (0.00 sec) rows affected (0.00 sec) mysql> select * from bank where id=32 lock in share mode; +------ +-------+ | id | debit | +------ +-------+ | 32 | 333 | +------ mysql> select * from bank where +-------+ id=32 lock in share mode; 1 row in set +------+-------+ (0.00 sec) | id | debit | +------+-------+ | 32 | 333 | +------+-------+ 1 row in set (0.00 sec) mysql> update bank set debit=444 where id=32; . . . . .> Wait unlock . . . mysql> update bank set . debit=777 where id=32; . ERROR 1213 (40001): Deadlock . found when trying to get . lock; Try restarting transaction mysql> update bank set debit=444 where id=32; Query OK, 1 row >T2 tiene bloqueado a 32, T1 affected (30.83 sec) espera la liberacion Rows matched: 1 >al hacer el update en T2 Changed: 1 Warnings: 0 ahora T2 espera a T1 >por lo tanto se produce un mysql> select * from bank; deadlock +------+-------+ >el dbms hace rollback en T2 8
  9. 9. Bases de Datos. | id | debit | (ultimo en espera) y procesa +------+-------+ T1 | 32 | 444 | | 66 | 3453 | +------+-------+ 2 rows in set (0.00 sec) mysql> commit; Query OK, 0 rows mysql> select * from bank; affected (0.00 sec) +------ +-------+ mysql> select * from bank; | id | debit | +------+-------+ +------ | id | debit | +-------+ +------+-------+ | 32 | 333 | | 32 | 444 | | 66 | 3453 | | 66 | 3453 | +------ +------+-------+ +-------+ 2 rows in set 2 rows in set (0.00 sec) (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql> select * from bank; +------ +-------+ | id | debit | +------ +-------+ | 32 | 444 | | 66 | 3453 | +------ +-------+ 2 rows in set (0.00 sec) mysql> set autocommit=0; mysql> set autocommit=0; Query OK, 0 rows affected (0.00 Query OK, 0 rows affected sec) (0.01 sec) mysql> select * from bank where id=32 lock in share mode; +----+-------+ | id | debit | +----+-------+ | 32 | 444 | mysql> update bank set +----+-------+ debit=555 where id=32; 1 row in set . (0.00 sec) . > . > Nadie puede modificar la . tupla 32 .> Wait unlock > . . mysql> commit; . Query OK, 0 rows . affected (0.00 sec) . . mysql> update bank set debit=555 where id=32; Query OK, 1 9
  10. 10. Bases de Datos. row affected (13.21 sec) mysql> select * from bank; Rows +----+-------+ matched: 1 Changed: 1 | id | debit | Warnings: 0 +----+-------+ | 32 | 444 | | 66 | 3453 | +----+-------+ 2 rows in set (1.02 sec) mysql> commit; Query OK, 0 rows affected mysql> select * from bank; (0.14 sec) +----+-------+ mysql> select * from bank; | id | debit | +---- +----+-------+ +-------+ | 32 | 555 | | id | debit | 66 | 3453 | | +----+-------+ +---- 2 rows in set +-------+ (0.01 sec) | 32 | 555 | | 66 | 3453 | +---- +-------+ 2 rows in set (0.00 sec) 10

×