悲观锁定vs序列化事务隔离级别。

时间:2021-07-29 06:48:42

I kinda understand the purpose of entity locking and transaction isolation level, but can't get the difference between pessimistic locking and serializable level. As I understand, in both cases the table gets locked and no other transaction can access it, so in both cases actions to prevent concurrent modifications are taken by the DB, which looks like there's no difference. Could someone please explain if there actually is difference here?

我有点理解实体锁和事务隔离级别的目的,但是我不能区分悲观锁和可序列化级别。正如我所理解的,在这两种情况下,表都被锁住了,没有其他事务可以访问它,所以在这两种情况下,DB都采取了防止并发修改的操作,看起来没有区别。有人能解释一下这里是否有差异吗?

2 个解决方案

#1


1  

(I don't assume you're using ObjectDB. You'll probably get better answers if you edit your question, and include the specific database you're using with JPA.)

(我不认为你在使用ObjectDB。如果您编辑您的问题,并包含使用JPA的特定数据库,您可能会得到更好的答案。

I don't like the terms optimistic locking and pessimistic locking. I think optimistic concurrency control and pessimistic concurrency control are more accurate. Locks are the most common way to deal with concurrency control problems, but they're not the only way. (Date's chapter on concurrency in An Introduction to Database Systems is about 25 pages long.)

我不喜欢乐观锁定和悲观锁定这两个术语。我认为乐观并发控制和悲观并发控制更准确。锁是处理并发控制问题的最常见的方法,但是它们不是唯一的方法。(Date在数据库系统介绍中关于并发的章节大约有25页长。)

The topics of transaction management and concurrency control aren't limited to the relational model of data or to SQL database management systems (dbms). Transaction isolation levels have to do with SQL.

事务管理和并发控制的主题不仅限于数据的关系模型或SQL数据库管理系统(dbms)。事务隔离级别与SQL有关。

Pessimistic concurrency control really means only that you expect the dbms to prevent other transactions from accessing something when the dbms starts processing your request. Behavior is up to the dbms vendor. Different vendors might prevent access by locking the entire database, locking some tables, locking some pages, or locking some rows. Or the dbms might prevent access in some other way that doesn't directly involve locks.

悲观并发控制实际上只是意味着,当dbms开始处理您的请求时,您希望dbms能够阻止其他事务访问某些内容。行为取决于dbms供应商。不同的供应商可能通过锁定整个数据库、锁定一些表、锁定一些页面或锁定一些行来阻止访问。或者dbms可能以某种不直接涉及锁的方式阻止访问。

Transaction isolation levels are how SQL tries to solve concurrency control problems. Transaction isolation levels are defined in SQL standards.

事务隔离级别是SQL解决并发控制问题的方式。事务隔离级别在SQL标准中定义。

The serializable transaction isolation level guarantees that the effect of concurrent, serializable transactions is the same as running them one at a time in some particular order. The guarantee describes the effect--not any particular kind of concurrency control or locking needed to achieve that effect.

serializable事务隔离级别确保并发的、可序列化的事务的效果与以某种特定顺序一次运行它们的效果相同。该保证描述了这种效果——而不是实现这种效果所需的任何特定类型的并发控制或锁定。

#2


1  

Pessimistic locking normally involves writelocks to the database to do changes in a safe and exclusive way. This is normally done by doing select ... for update. This will prevent or delay other connections from doing their own select ... for update or changes on the locked records in the database until the transaction of the first connection is completed.

悲观锁定通常涉及到数据库的writelocks以一种安全且排他性的方式进行更改。这通常是通过执行select…为更新。这将防止或延迟其他连接执行它们自己的选择……更新或更改数据库中被锁定的记录,直到第一个连接的事务完成。

Serializable Isolation Level does not need to be concerned with changes but makes sure that after the transaction started, the result of reads will always stay the same (except changes by the transaction itself) until that transactions ends. To support this "Non-MVCC"-DBMS must set many locks (on each record read by the connection working serializable) in the database and therefore might hinder concurrency very much.

Serializable隔离级别不需要关注更改,但是要确保在事务启动之后,读取的结果始终保持不变(除了事务本身的更改),直到事务结束。为了支持这个“非mvcc”—dbms必须在数据库中设置许多锁(在连接可序列化读取的每个记录上),因此可能会极大地阻碍并发。

The same effect can also be achieved without locking when databases provide MVCC as do Oracle, MySql-INNODB, MariaDB, Postgres

同样的效果也可以在数据库提供MVCC的情况下实现,如Oracle、MySql-INNODB、MariaDB、Postgres。

#1


1  

(I don't assume you're using ObjectDB. You'll probably get better answers if you edit your question, and include the specific database you're using with JPA.)

(我不认为你在使用ObjectDB。如果您编辑您的问题,并包含使用JPA的特定数据库,您可能会得到更好的答案。

I don't like the terms optimistic locking and pessimistic locking. I think optimistic concurrency control and pessimistic concurrency control are more accurate. Locks are the most common way to deal with concurrency control problems, but they're not the only way. (Date's chapter on concurrency in An Introduction to Database Systems is about 25 pages long.)

我不喜欢乐观锁定和悲观锁定这两个术语。我认为乐观并发控制和悲观并发控制更准确。锁是处理并发控制问题的最常见的方法,但是它们不是唯一的方法。(Date在数据库系统介绍中关于并发的章节大约有25页长。)

The topics of transaction management and concurrency control aren't limited to the relational model of data or to SQL database management systems (dbms). Transaction isolation levels have to do with SQL.

事务管理和并发控制的主题不仅限于数据的关系模型或SQL数据库管理系统(dbms)。事务隔离级别与SQL有关。

Pessimistic concurrency control really means only that you expect the dbms to prevent other transactions from accessing something when the dbms starts processing your request. Behavior is up to the dbms vendor. Different vendors might prevent access by locking the entire database, locking some tables, locking some pages, or locking some rows. Or the dbms might prevent access in some other way that doesn't directly involve locks.

悲观并发控制实际上只是意味着,当dbms开始处理您的请求时,您希望dbms能够阻止其他事务访问某些内容。行为取决于dbms供应商。不同的供应商可能通过锁定整个数据库、锁定一些表、锁定一些页面或锁定一些行来阻止访问。或者dbms可能以某种不直接涉及锁的方式阻止访问。

Transaction isolation levels are how SQL tries to solve concurrency control problems. Transaction isolation levels are defined in SQL standards.

事务隔离级别是SQL解决并发控制问题的方式。事务隔离级别在SQL标准中定义。

The serializable transaction isolation level guarantees that the effect of concurrent, serializable transactions is the same as running them one at a time in some particular order. The guarantee describes the effect--not any particular kind of concurrency control or locking needed to achieve that effect.

serializable事务隔离级别确保并发的、可序列化的事务的效果与以某种特定顺序一次运行它们的效果相同。该保证描述了这种效果——而不是实现这种效果所需的任何特定类型的并发控制或锁定。

#2


1  

Pessimistic locking normally involves writelocks to the database to do changes in a safe and exclusive way. This is normally done by doing select ... for update. This will prevent or delay other connections from doing their own select ... for update or changes on the locked records in the database until the transaction of the first connection is completed.

悲观锁定通常涉及到数据库的writelocks以一种安全且排他性的方式进行更改。这通常是通过执行select…为更新。这将防止或延迟其他连接执行它们自己的选择……更新或更改数据库中被锁定的记录,直到第一个连接的事务完成。

Serializable Isolation Level does not need to be concerned with changes but makes sure that after the transaction started, the result of reads will always stay the same (except changes by the transaction itself) until that transactions ends. To support this "Non-MVCC"-DBMS must set many locks (on each record read by the connection working serializable) in the database and therefore might hinder concurrency very much.

Serializable隔离级别不需要关注更改,但是要确保在事务启动之后,读取的结果始终保持不变(除了事务本身的更改),直到事务结束。为了支持这个“非mvcc”—dbms必须在数据库中设置许多锁(在连接可序列化读取的每个记录上),因此可能会极大地阻碍并发。

The same effect can also be achieved without locking when databases provide MVCC as do Oracle, MySql-INNODB, MariaDB, Postgres

同样的效果也可以在数据库提供MVCC的情况下实现,如Oracle、MySql-INNODB、MariaDB、Postgres。