在谈论数据库系统时,事务层由什么构成?

发布于 2024-12-28 22:22:27 字数 172 浏览 1 评论 0原文

例如,LevelDB 不支持多语句事务。我在某处读到您必须处理“事务层”中的那些内容。

为了向不支持事务的较低级别库添加事务支持,该层必须做什么?

For example, LevelDB doesn't support multi-statement transactions. I read somewhere that you would have to deal with those in a "transactional layer".

What would this layer have to do to add transaction support to a lower-level library that doesn't support transactions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下伊人醉 2025-01-04 22:22:27

定义事务的方法有多种,并且实现它们的方法也有多种。事务的一个共同属性是它的 ACID:

  • 原子性 - 全有或全无。一个数据库事务的所有任务都必须完成;如果由于任何可能的原因导致不完整,则必须中止数据库事务。
  • 一致性 - 可串行化和完整性。数据库事务前后数据库必须处于一致或合法的状态。这意味着数据库事务不得破坏数据库完整性约束。
  • 隔离 数据库事务执行期间使用的数据在执行完成之前不得被其他数据库事务使用。因此,在事务成功提交之前,不完整事务的部分结果一定不能用于其他事务。这也意味着一个事务的执行不会受到其他并发事务的数据库操作的影响。
  • 持久性 即使事务完成后发生系统故障,事务的所有数据库修改也将永久保存。

一个事务可能有几种状态:

  • 活动状态:分为两个阶段。初始阶段:数据库事务处于该阶段,此时其语句开始执行。部分提交阶段:数据库事务在执行其最终语句后进入此阶段。在此阶段,数据库事务已完成其执行,但事务仍有可能中止,因为执行的输出可能仍暂时驻留在主内存中 - 诸如硬件故障之类的事件可能会擦除输出。
  • 失败状态:当数据库事务由于硬件或程序错误而无法正常执行时,就会进入失败状态。
  • 中止状态:如果 DBMS 确定数据库事务失败,则数据库事务进入中止状态。中止的事务必须对数据库没有影响,因此它对数据库所做的任何更改都必须撤消,或者用技术术语来说,回滚。当中止的事务回滚后,数据库将返回到其一致状态。 DBMS 的恢复方案负责管理事务中止。
  • 提交状态:数据库事务成功执行后,当足够的信息写入磁盘时,就进入提交状态。在这种状态下,大量信息已写入磁盘,以至于事务产生的影响无法通过中止来撤销;即使发生系统故障,已提交事务所做的更改也可以在系统重新启动时重新创建。

LevelDB 不支持事务,但它确实具有一些ACID 属性:

  • 批量写入是原子的。
  • 一致性取决于你。
  • 隔离支持有限。
  • 耐用性是一个可配置的选项。

那么...回到你的问题:

问:

该层必须做什么才能向不支持事务的较低级别库添加事务支持?

答:
这取决于您如何定义事务。如果您使用上述属性定义事务,并且希望事务具有 ACID,那么您必须弄清楚 LevelDB 是否可以实现这一点(集成了大多数 ACID 属性),然后您必须围绕 LevelDB 编写一个包装器,以确保正确维护事务的状态。但是,我并不完全确定单独的包装器可以做到这一点,因此您可能必须实际获取源代码并将其修改为真正支持事务。

There are various ways to define transactions and there are various ways to implement them. A common property of a transaction is that it's ACID:

  • Atomicity - all or nothing. All of the tasks of a database transaction must be completed; If incomplete due to any possible reasons, the database transaction must be aborted.
  • Consistency - serializability and integrity. The database must be in a consistent or legal state before and after the database transaction. It means that a database transaction must not break the database integrity constraints.
  • Isolation Data used during the execution of a database transaction must not be used by another database transaction until the execution is completed. Therefore, the partial results of an incomplete transaction must not be usable for other transactions until the transaction is successfully committed. It also means that the execution of a transaction is not affected by the database operations of other concurrent transactions.
  • Durability All the database modifications of a transaction will be made permanent even if a system failure occurs after the transaction has been completed.

A transaction may have several states:

  • Active State: It is divided into two phases. Initial Phase: a database transaction is in this phase while its statements start to be executed. Partially Committed Phase: a database transaction enters this phase when its final statement has been executed. At this phase, the database transaction has finished its execution, but it is still possible for the transaction to be aborted because the output from the execution may remain residing temporarily in main memory - an event like hardware failure may erase the output.
  • Failed State: A database transaction enters the failed state when its normal execution can no longer proceed due to hardware or program errors).
  • Aborted State: A database transaction, if determined by the DBMS to have failed, enters the aborted state. An aborted transaction must have no effect on the database, and thus any changes it made to the database have to be undone, or in technical terms, rolled back. The database will return to its consistent state when the aborted transaction has been rolled back. The DBMS's recovery scheme is responsible to manage transaction aborts.
  • Committed State: A database transaction enters the committed state when enough information has been written to disk after completing its execution with success. In this state, so much information has been written to disk that the effects produced by the transaction cannot be undone via aborting; even when a system failure occurs, the changes made by the committed transaction can be re-created when the system restarts.

LevelDB does not support transactions, however it does have some of the ACID properties:

  • Batch writes are atomic.
  • Consistency is up to you.
  • There is limited isolation support.
  • Durability is a configurable option.

So... back to your question:

Q:

What would this layer have to do to add transaction support to a lower-level library that doesn't support transactions?

A:
It depends on how you define a transaction. If you define a transaction with the above-mentioned properties and if you want your transactions to be ACID, then you'd have to figure out if that's possible with LevelDB (most of the ACID properties are integrated) and then you'd have to write a wrapper around LevelDB which ensures that the states of the transactions are maintained properly. However, I'm not entirely sure that a wrapper alone would do it, so you may have to actually take the source code and modify it to truly support transactions.

若有似无的小暗淡 2025-01-04 22:22:27

鉴于您的数据库是单线程的,您可以执行以下操作:

  1. 使用 leveldb 批处理功能:创建一个新密钥,而不是覆盖旧密钥。还记录密钥的旧值和新值。

  2. 如果此时数据库崩溃,请查找日志记录并通过将事务一部分的键重新声明为其旧值来回滚事务。删除日志以完成回滚。

  3. 如果事务已提交,则删除旧密钥并删除日志条目以完成提交。

然后,您有一个使用多个版本的单线程键/值存储事务。

如果数据库是多线程的,则必须使用 MVCC(参见 Yahoo 的 Omid、PostgreSQL、Wiredtiger、bsddb...)以及诸如精确可序列化快照隔离(PSSI)之类的东西。

Given your database is single threaded you can do the following:

  1. Using leveldb batch feature: Instead of overriding old key, create a new one. Also log key's old and new value.

  2. If the database crash at this point, lookup the log records and rollback the transaction by restate keys that were part of the transaction to their old value. Delete the log to complete the rollback.

  3. If the transaction is committed, delete old keys and delete log entries to complete commit.

Then you have a transaction for single threaded key/value store using multiple versions.

If the database is multithread you have to use MVCC (see Yahoo's Omid, PostgreSQL, Wiredtiger, bsddb...) and something like Precisely Serializable Snapshot Isolation (PSSI).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文