SQL插入语句执行两次

发布于 2024-12-19 09:47:44 字数 94 浏览 2 评论 0原文

防止用户双击或刷新页面导致执行 SQL 插入语句两次的最佳方法是什么,我尝试在单击后禁用该按钮,但结果并不是很好。我希望可以从代码隐藏中做到这一点。更像是 SQL 提交和回滚

what is the best way to prevent user double click or refresh page that would cause execute SQL insert statement twice, I've tried to disable the button after click, but the result is not really good. I am expecting that it is possible to do it from code-behind. something more like SQL commit and rollback

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

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

发布评论

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

评论(2

指尖上的星空 2024-12-26 09:47:44

也许PRG 维基百科文章可以帮助你:

Post/Redirect/Get (PRG) 是 Web 开发人员的常见设计模式
帮助避免某些重复的表单提交并允许用户代理
使用书签和刷新按钮表现得更直观。

Perhaps PRG wikipedia article can help to you:

Post/Redirect/Get (PRG) is a common design pattern for web developers
to help avoid certain duplicate form submissions and allow user agents
to behave more intuitively with bookmarks and the refresh button.

_蜘蛛 2024-12-26 09:47:44

如果您希望防止这种情况发生,您将需要服务器知道当前用户已经开始操作,并且在满足条件之前无法再次开始操作。

您需要能够从众多可能访问您网站的用户中识别出该用户。使用 SessionState 可以最简单地完成此操作,但如果您对 SessionState 没有其他需要并且希望大规模扩展您的应用程序,则可以使用一个简单的随机 cookie 来识别用户,作为您用于将项目放入 SessionState 的任何键的前缀。服务器缓存。

假设您使用了 SessionState。您将执行类似以下操作(伪):

public void StartAction()
{
  var inProgress = HttpContext.Current.Session["actionInProgress"] as bool;
  if (!inProgress)
  {
     try
     {
        HttpContext.Current.Session["actionInProgress"] = true;
        MySqlController.DoWork();
     }
     finally
     {
        HttpContext.Current.Session["actionInProgress"] = false;
     }
  }
}

上面的内容不考虑以下内容:

  • 捕获异常和/或关闭 finally 块中的任何连接
  • 由于下一个操作而对后续操作进行排队单击您的客户端(如果操作已经在进行中,则此伪代码只会返回)
  • 我选择了最简单的解决方案,但实际上更好的做法是将其包含为异步运行的服务,以便您可以监控进展,双方都受益用户的,并防止多个并行进程。

If you wish to protect against this you're going to need the server to be aware that the current user has already begun an action, and can't begin it again until a condition is met.

You need to be able to identify that user amongst the many that are potentially visiting your site. This is most simply done using SessionState, but if you have no other need for SessionState and wish to scale your application massively, a simple random cookie to identify the user can be used as a prefix for any keys that you use to place items into the server cache.

Let's say you used SessionState. You'd do something like the following (pseudo):

public void StartAction()
{
  var inProgress = HttpContext.Current.Session["actionInProgress"] as bool;
  if (!inProgress)
  {
     try
     {
        HttpContext.Current.Session["actionInProgress"] = true;
        MySqlController.DoWork();
     }
     finally
     {
        HttpContext.Current.Session["actionInProgress"] = false;
     }
  }
}

The above does not account for the following:

  • Catching exceptions and/or closing any connections in your finally block
  • Queueing up subsequent actions as a result of the next clicks on your client (this pseudo-code just returns if the action is already in progress)
  • I've gone for the simplest solution, but in reality a better practise would be to have this encompassed as a service which runs asynchronously so that you can monitor the progress both for the benefit of the user, and for the prevention of multiple parallel processes.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文