为什么在Java中默认使用TRANSACTION_READ_COMMITTED?

huangapple go评论75阅读模式
英文:

Why is TRANSACTION_READ_COMMITTED the default in Java?

问题

事务教程在使用事务维护数据完整性一节中进行了解释:

默认事务隔离级别取决于您的DBMS。例如,对于Java DB,它是TRANSACTION_READ_COMMITTED。

对于我的Postgresql数据库也是一样。但是这似乎不是最佳选择,因为只有TRANSACTION_SERIALIZABLE能够防止所有的不一致性:

隔离级别                   │事务        │脏读         │不可重复读     │幻读
===============================================================================
TRANSACTION_NONE         │不支持       │不适用       │不适用           │不适用
TRANSACTION_READ_COMMITTED│支持         │被阻止       │允许             │允许
TRANSACTION_READ_UNCOMMITTED│支持       │允许         │允许             │允许
TRANSACTION_REPEATABLE_READ│支持        │被阻止       │被阻止           │允许
TRANSACTION_SERIALIZABLE  │支持        │被阻止       │被阻止           │被阻止

为什么不选择默认的最佳选项?

英文:

The transaction tutorial explains in the section Using Transactions to Preserve Data Integrity:

> The default transaction isolation level depends on your DBMS. For example, for Java DB, it is TRANSACTION_READ_COMMITTED.

For my Postgresql database it is the same. But this does not seem to be the best, because only TRANSACTION_SERIALIZABLE prevents all inconsistencies:

<!-- language: none -->

Isolation Level             │Transactions │Dirty Reads   │Non-Repeatable Reads│Phantom Reads
=============================================================================================
TRANSACTION_NONE            │Not supported│Not applicable│Not applicable      │Not applicable
TRANSACTION_READ_COMMITTED  │Supported    │Prevented     │Allowed             │Allowed
TRANSACTION_READ_UNCOMMITTED│Supported    │Allowed       │Allowed             │Allowed
TRANSACTION_REPEATABLE_READ │Supported    │Prevented     │Prevented           │Allowed
TRANSACTION_SERIALIZABLE    │Supported    │Prevented     │Prevented           │Prevented

Why is not the best option the default?

答案1

得分: 4

有事务安全性和性能之间的反向关系。
如果您选择了最佳隔离级别,则性能也会最差。
您必须在两者之间做出权衡。

有关更多详细信息,请参见此基准测试

英文:

There is an inverse relationship between transaction security and performance.<br>
If you choose the best isolation level, you also get the worst performance.<br>
You have to make a trade-off between the two.

See this benchmark for more details.

huangapple
  • 本文由 发表于 2020年9月1日 15:58:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63683580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定