快照隔离与可序列化
DBMS 中的事务隔离级别有助于克服脏读、幻读和不可重复读等并发问题。快照和可序列化隔离级别都被认为是最高的隔离级别,因为它们都避免了所有并发问题,但方式却截然不同。
快照隔离:
顾名思义,“快照”允许同时发生的事务看到与事务开始时相同的数据库快照或副本。因此,允许第二个事务对另一个并发事务要读取的数据进行更改。这个其他事务不会观察到第二个事务所做的更改,并将继续处理数据库的前一个快照。
可序列化:
可序列化执行之后的事务虽然并发运行,但看起来好像它们以串行顺序运行。可序列化使用锁来对事务进行读写操作。锁确保在完成之前,不允许其他并发事务修改持有锁的事务使用的数据。
快照隔离和可序列化的区别:
S.No. | Snapshot Isolation | Serializable |
---|---|---|
1. | In Snapshot, the SQL server avoids locks by making use of row versioning. | In Serializable, the SQL server makes use of locks and holds them until the transaction is committed or aborted. |
2. | Follows optimistic concurrency control. | Follows pessimistic concurrency control. |
3. | The concurrency level is high in comparison to serializable. | Low-level of concurrency is achieved as one transaction needs to wait for the completion of another transaction. |
4. | Since no locks are imposed on data when it is read, other concurrent transactions are allowed to write data at the same time without any conflicts. | If two transactions try to read and write data at the same time then a deadlock occurs and one of the transactions is killed or rolled back as a deadlock victim. |
5. | If two transactions try to update the same record at the same time then an update conflict occurs and the SQL server had to kill one of the transactions. | If two transactions try to update the same record then the second transaction will wait for the first transaction to either rollback or commit. |