如何为一系列操作实现“原子性”?(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
最满意答案
因为当用户到达页面时,它是一个
GET
操作,因此根本没有要求任何数据已经提供给页面。 但是你在代码中假设getPart
没有返回null
。 然而,如果“......此请求属于multipart / form-data类型,但不包含所请求的Part” ,getPart
明确定义为返回null
。 ( 参考 )。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 thatgetPart
is not returningnull
. And yet,getPart
is clearly defined as returningnull
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 yourdoPost
function, not yourdoGet
function.
相关问答
更多-
因为当用户到达页面时,它是一个GET操作,因此根本没有要求任何数据已经提供给页面。 但是你在代码中假设getPart没有返回null 。 然而,如果“......此请求属于multipart / form-data类型,但不包含所请求的Part” , getPart明确定义为返回null 。 ( 参考 )。 您的表单定义为使用POST ,因此您希望在doPost函数中处理它,而不是在doGet函数中处理它。 Because when the user arrives on the page, it's a ...
-
05-29 20:57:29.906: E/AndroidRuntime(359): Caused by: java.lang.NullPointerException 05-29 20:57:29.906: E/AndroidRuntime(359): at com.dd.qsg.DatabaseHandler.getLongestSmoke(DatabaseHandler.java:85) 有你的线索。 DatabaseHandler.java第85行的东西试图取消引用空引用。 根据你的编辑,我猜那 ...
-
public User login(String userName, String pwd){ User actual = null; if(users!=null){ for (User me : users) { if (me.checkLogin(userName, pwd)){ actual = me; } } return actual; } return null; } ...
-
Connection con = null; ...... stmt = con.createStatement(); ^ 连接未初始化。 您必须连接到DB才能检索数据。 Connection con = null; ...... stmt = con.createStatement(); ^ The connection is not initialized. You have to connect to DB in order to retrieve data.
-
谈论过于复杂的事情......在这里我想到的是关于doGet和doPost对同一页面的两次连续调用,可能是响应或请求可能已经损坏了某个属性,或者更糟糕的是配置是错误的...不,像往常一样,更简单,更明显的方式,一旦你注意到它当然。 简单地说,在doPost中没有再次使用“tiendas”引用,因为出于某种原因我认为它仍然附加在请求上。 因此,当jsp页面尝试执行“tiendas.get(i)”时,显然它没有找到变量并且抛出了NullPointerException。 这也是因为我缺乏调试我专注于servle ...
-
查看异常日志后,我可以高度肯定地说您的问题是由于尝试对包含null元素的数组进行排序。 鉴于您的代码流程,我强烈反对我之前建议完整地构建您的数组,然后对其进行排序,然后使用它来生成您的html。 After viewing your exception log I can say with a high degree of certainty that your issue is due to attempting to sort an array containing null elements. Giv ...
-
java.lang.NullPointerException jdbc大错误[重复](java.lang.NullPointerException jdbc big error [duplicate])[2022-01-02]
它的statement为null 要使整个代码按预期工作,请使用以下代码,因为statement正在各个地方使用 package com.smy.gome; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class MySQL { static ... -
在你的setUp()方法中,声明一个名为driver的本地WebDriver变量,并将其设置为一个新的驱动程序实例。 您的类级驱动程序变量永远不会设置为实例,因此始终为空。 public class SuccessfullHoverTestCib { private WebDriver driver; private String baseUrl; private StringBuffer verificationErrors = new StringBuffer(); @ ...
-
重写处理程序,如下所示: waterButton.addActionListener(new ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ Statement st = connectionObject ...
-
多线程程序中的java.lang.NullPointerException(java.lang.NullPointerException in the multithread program)[2023-05-05]
你有竞争条件。 因为您在初始化p1之前在init()中启动了线程,所以在init有机会初始化之前,您可能会遇到线程可能运行的风险。 您只需要在线程启动之前将p1和b1的初始化移动到: ... p1 = new HumanPaddle(1); b1 = new Ball(); thread = new Thread(this); thread.start(); } You have a race condition. Because you start your thre ...