多个 destroy() 锁定backbone.js 中的后端

发布于 2024-10-19 20:19:12 字数 759 浏览 1 评论 0原文

在backbone.js的示例Todos应用中,会发生以下情况

clearCompleted: function() {
  _.each(Todos.done(), function(todo){ todo.clear(); });
  return false;
},

:通过向支持应用程序的任何服务发送多个 http DELETE 请求来删除多个模型。在示例的情况下,这没有问题,因为他们使用的是本地存储解决方案。

但是,当我在后端数据库(sqlite/datamapper/sinatra)尝试类似的过程时,它同时发送多个删除http请求的事实会导致数据库锁定并发回错误。

你们有人遇到过这种情况吗?

我可以想到两种解决方法:

  1. 使用 destroyBatch() 将 id 数组发送到 DELETE 调用中,并让 sinatra 嗅出多个 id 并在服务器端一次性处理所有删除。

  2. 在客户端有一个 destroyAsync(),它将 id 推入队列,并以异步链式反应逐一调用模型上的 destroy(),直到它们全部消失(但你会看到它们被在屏幕上一一删除,每个之间有一个暂停)。

这些解决方案中的任何一个看起来合理吗?或者我只是一只疯狂拍打翅膀的脆弱的鹅?

-j

In the example Todos app for backbone.js, this takes place:

clearCompleted: function() {
  _.each(Todos.done(), function(todo){ todo.clear(); });
  return false;
},

This deletes multiple models by sending out multiple http DELETE requests to whatever service is backing the app. In the example's case that is no problem b/c they are using a local storage solution.

But when I try a similar process with a database on the backend (sqlite/datamapper/sinatra) the fact that it sends off multiple delete http requests simultaneously causes the db to lock and send back an error.

Is this something any of you have run into?

I can think of two ways around it:

  1. Have a destroyBatch() that sends an array of id's into a DELETE call, and have sinatra sniff out the multiple ids and handle the deletes all at once server-side.

  2. Have a destroyAsync() on the client-side that pushes the ids into a queue and calls destroy() on the models one-by-one in an async chain reaction until they are all gone ( but you would see them being deleted one by one on the screen with a pause in between each).

Do either of those solutions seem reasonable, or am I a frail goose flapping wildly?

-j

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

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

发布评论

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

评论(1

缘字诀 2024-10-26 20:19:13

选项 2 不是一个可行的选项。您的用户可以单击“返回”或关闭窗口,并且删除不会完全成功。所以就用这个吧。

这让我们需要:

  • 修复数据库中最初的锁定问题:D
  • 立即发送所有要删除的 id。

我会先尝试解决最初的问题。是什么导致他们被锁起来?我非常确定在开发模式下 sinatra 将一次处理一个请求,因此发送一堆删除实际上将在后端处理上序列化...这完全是另一个问题,与返回的 sqlite 错误相关联。

至于批量发送删除。这是一个好主意,但它偏离了标准的 RESTful 控制器。因此,您必须处理自己作为骨干无法提供执行此操作的方法。您可以在集合上添加 deleteAll 方法并从那里处理同步(如果您依赖事件,请不要忘记发送事件)。

Option 2 is not a viable one. Your user can click back or close the window and the deletion will not succeed completely. So out with this one.

This leaves us to:

  • Fix your initial problem of locks in the DB :D
  • Send all ids to be deleted at once.

I would try to solve the initial problem first. What is causing them to lock up? I am pretty sure that in development mode sinatra will process a single request at a time, so sending a bunch of delete will actually be serialized on the backend processing... That is another question altogether that would be linked to the sqlite error returned.

As for sending the deletion in batches. It is a good idea, but it deviates from the standard RESTful controller. So you will have to handle that yourself as backbone do not provide a way to do this. You can add a deleteAll method on the collection and handle the sync from there (do not forget to send events if you are relying on them).

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