首页 \ 问答 \ 如何为一系列操作实现“原子性”?(How to implement “atomicity” for a series of operations? (not necessarily multithreading related))

如何为一系列操作实现“原子性”?(How to implement “atomicity” for a series of operations? (not necessarily multithreading related))

问题

我的程序有一个操作,它由一系列单个操作组成,这些操作都改变了全局状态。 每个基本操作都可能失败并使全局状态处于未定义状态。

问题

是否存在一种通用模式,可帮助我使组合操作成为“原子”,即如果其中一个子操作失败,则全局状态保持不变?

我使用C ++,所以如果答案包含代码,如果您有选择,请更喜欢该语言。 但我不介意其他语言的例子。

评论

  • 这类似于数据库的“原子性”,在执行提交时,您可以在其中添加全部或全部内容。 这是如何实现的?

  • 在我的情况下,我的全局状态是文件系统的状态。 我需要一次添加或删除多个文件,并希望确保它们都已添加或未添加任何内容。

我的想法

我能想到的最好的是一个获取操作列表及其逆操作的类。 如果任何操作失败,它将执行已执行操作的反向操作。 但如果其中一个反向操作失败该怎么办?

该类的界面如何? 我是否会有一个类似于可逆操作的额外课程? 你知道一个我可以阅读更多关于这个问题的地方吗?


The Problem

My programm has an operation which consists of a series of single operations that all change global state. Each of the elementary operations can fail and leave the global state in an undefined state.

The Question

Is there a generic pattern that helps me to make the composed operation "atomic" in the sense that if one of the suboperations fails, the global state is left unchanged?

I use C++, so if answers contain code, please prefer that language if you have a choice. But I do not mind examples in other languages.

Comments

  • This resembles the "atomicity" of a database, where you add all or nothing when doing a commit. How is that implemented?

  • In my case my global state is the state of the file-system. I need to add or remove multiple files at once and want to make sure that they are all added or nothing is added.

My Thoughts

The best I can come up with is a class that gets a list of operations and their inverse operations. If any of the operations fails, it executes the reverse operations of the already executed operations. But what to do if one of the reverse operations fails?

How would the interface of that class look like? Would I have an extra class that resembles an reversible operation? Do you know a place where I can read more about this problem?


原文:https://stackoverflow.com/questions/33548463
更新时间:2023-06-10 10:06

最满意答案

因为当用户到达页面时,它是一个GET操作,因此根本没有要求任何数据已经提供给页面。 但是你在代码中假设getPart没有返回null 。 然而,如果“......此请求属于multipart / form-data类型,但不包含所请求的Part”getPart明确定义为返回null 参考 )。

您的表单定义为使用POST ,因此您希望在doPost函数中处理它,而不是在doGet函数中处理它。


Because when the user arrives on the page, it's a GET operation, and so there's no requirement at all that any data has been provided to the page. But you're assuming in your code that getPart is not returning null. And yet, getPart is clearly defined as returning null if "... this request is of type multipart/form-data, but does not contain the requested Part." (ref).

Your form is defined as using POST, so you want to handle it in your doPost function, not your doGet function.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。