如何防止.net应用程序中由于突然断电而导致数据丢失?
我们正在开发 RetailPOS .net (windows) 应用程序。
一位客户问“当突然断电时,应用程序中正在处理的当前数据会发生什么?”、“应用程序能够恢复未保存的数据吗?”
我想知道如何将这个功能包含在.net应用程序中?
有人可以帮助我吗,为此需要做什么?
We are developing an RetailPOS .net (windows) application.
One of the customer asks 'What will happen to current data being processed in the application when a power went off suddenly?', 'Will the application able to recover the unsaved data?'
I am wondering, how this feature can be included in the .net application?
Can some help me, what needs to be done for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,考虑使用 UPS(不间断电源);
其次,考虑创建持久性事务,这些事务将尽早且经常保存,但在您指示之前不会提交。 这有点超出了 TSQL 中的典型事务。
First, look into a UPS (un-interuptable power supply)
Second, look into creating persistent transactions, transactions that will save early and often, but not commit until you tell it to. This goes a little beyond the typical transactions in TSQL.
如果是硬断电(就像笔记本电脑上的电池在拔掉插头时被取出),那么除了每次写入磁盘之外,您无能为力。 步。 的。 这。 方式。 (谈论性能影响。)还要确保您记录了一些内容,以便让应用程序知道它何时启动备份之前正在执行的操作,以便它可以继续或重试该操作。
如果是断电,例如按下电源按钮来关闭/睡眠/休眠计算机,则侦听 SystemEvents.PowerModeChanged 事件并采取相应措施。
最后,请确保如果您的数据在写入数据库的过程中会被损坏,请确保您的数据库写入是原子的(要么所有更改都发生并被接受,要么没有任何更改)。
If it's a hard power-cut (like the battery removed on a laptop while unplugged), then there's not much you can do other than write to disk every. step. of. the. way. (Talk about a performance hit.) And also make sure that you have something recorded to let the application know when it starts back up what it was in the middle of doing before, so it can either continue, or retry the operation.
If it's a power-cut, like the power button being pressed to shutdown/sleep/hibernate the computer, then listen for the SystemEvents.PowerModeChanged event and act accordingly.
Lastly, make sure that if your data would be corrupted if halfway written to a database, make sure your database writes are atomic (either all the changes happen and are accepted, or none of them are).
您可以将当前收到的信息附加到文件系统中。 这会对性能造成影响,但可以让您返回到最后捕获的项目。
当应用程序启动时,检测是否有挂起的文件,并提供恢复选项。 适当加载文件中保存的数据。
掌握所有信息后,当您执行付款时发生的流程时,请确保以原子方式记录流程中的任何重大更改。 如果您正在访问外部系统,请记录您何时启动操作以及何时立即返回。 在上面的内容中,总是有一个可能失败的时间窗口,但您希望使其尽可能短,并为恢复过程提供足够的信息。 最坏的情况是如果系统无法自动恢复,您希望它有足够的信息来确定它在进程的哪个位置停止。
You can append the current information being received to the file system. It will be a performance hit, but will allow you to get back to the last item being captured.
When the application starts detect if there is a pending file, and provide the option to recover. Load the data saved in the file appropriately.
Once you have all the information, when you are doing the process that happens when paying, make sure to record any significant changes in the process atomically. If you are hitting external systems, record when you are initiating the operation and when it comes back right away. In the above there is always a window of time when it can fail, but you want to keep it as short as possible and to have enough information for the recovery processes. Worst case if the system can't automatically recover, you want it to have enough information to determine where in the process it stopped.
您真正想要的是磁盘上的原子事务。 执行这些操作的方法有很多,但通常情况下,如果您使用现有的数据库产品,那么它就会为您完成。
如果您使用自己的数据库或文件格式,我们会根据您的文件格式为您削减一些工作。
What you really want are atomic on disk transactions. There are many ways to perform these, but often times if you are using an existing database product it will be done for you.
If you rolled your own database or file formats, you have some work cut our for you depending on your file format.
PowerFailureException 类将解决这个问题。 其实只要用UPS维持供电就可以了。 唯一的其他选择实际上是维护数据库事务的原子性。
PowerFailureException class will solve this. Actually just maintain power with a UPS. The only other option really is to maintain atomicity for database transactions.
您可能需要考虑让您的应用程序使用 SQLite。
他们有一些关于何时使用页面的重要信息。
一些突出的点是:
我正在为我要实现的应用程序寻找类似的选项,该应用程序将在第三世界国家使用,那里全天经常断电......到目前为止,SQLite 似乎是一个不错的选择。
但最终,您的选择将取决于您的个人情况和要求。
You might want to consider having your application use SQLite.
They have some great info on their when to use page.
Some points that stand out are:
I'm looking for a similar option for an application I am going to implement which will be used in a third world country where the power goes out frequently throughout the day....so far SQLite seems to stand out as a good possible choice.
But at the end of the day, your choice will depend on your individual circumstances and requirements.