简体   繁体   English

除非提交表单,否则如何锁定数据库中的ID

[英]How to lock an id in database unless a form get submitted

I have one form that shows a unique id under which the form will be submitted. 我有一个表单,显示了一个唯一的ID,该表单将根据该ID提交。 The unique id is- last unique id in DB+1 唯一ID是DB + 1中的最后一个唯一ID

Now, when the form gets submitted in DB, it actually doesn't directly insert that unique id in db. 现在,当表单在DB中提交时,实际上并没有直接在db中插入该唯一ID。 It then dynamically checks the last unique id of db and then insert last id+1. 然后,它动态检查db的最后一个唯一ID,然后插入最后一个id + 1。

So, the situation may often come that what a user sees as his form id(that unique id), the form may not actually get submitted under that unique id because if somebody else(user2) in the meantime submit the form, then User1's form will be submitted under the unique id+2 value. 因此,通常会出现这样的情况:用户将其视为自己的表单ID(唯一ID),而该表单实际上可能不会在该唯一ID下提交,因为如果其他人(user2)同时提交表单,则User1的表单将以唯一的id + 2值提交。

My question is, how to lock the id for the time duration from when the user opens the form and until submit it. 我的问题是,如何锁定从用户打开表单到提交表单的持续时间。

Please let me know, if the question is not clear to you. 如果您不清楚这个问题,请告诉我。 But this is a very tricky and important thing for large business application. 但这对于大型企业应用程序而言是非常棘手且重要的事情。 Kindly advise me. 请告诉我。

I see two options 我看到两个选择

  • Show the ID after the insert is made, not before. 在插入之后而不是之前显示ID。 What if the user cancels? 如果用户取消该怎么办? You will waste a lot of IDs. 您将浪费大量ID。 OK, there are plenty but I can image how a DoS attack would look like. 好的,还有很多,但是我可以想象一下DoS攻击的样子。
  • If you need the ID to give the user a reference ID for later contact with the support or such just create a different ID appart from the technical primary key. 如果您需要该ID为用户提供参考ID,以便以后与支持人员联系,或者只需创建与技术主键不同的ID appart。 This ID can be calculated before an insert and also saves you trouble as technical IDs might change in migration scenarios. 该ID可以在插入之前进行计算,并且还可以避免麻烦,因为在迁移方案中技术ID可能会更改。

Locking is not a good idea, especially for a large business application. 锁定不是一个好主意,特别是对于大型企业应用程序。 All the other concurrent requests (people) will have to wait until the current user submits the form. 所有其他并发请求(人员)将不得不等待,直到当前用户提交表单为止。 Users will experience long response times, app server resources will saturate and the application will crash even with a very minimin load. 用户将经历很长的响应时间,应用服务器资源将饱和,即使负载非常小,应用程序也将崩溃。

A preferable approach would be to inform the user of the id only after the form submit. 一种更好的方法是仅在提交表单后才将ID通知用户。 It likely is not useful info for them beforehand. 事先对他们来说可能不是有用的信息。

Anyway, the exact implementation of the lock depends on your db but is something like: begin transaction select max(id) from foo [somehow increment id] [wait for user submit] insert into foo values (new_id) commit 无论如何,锁的确切实现取决于您的数据库,但类似于:从foo开始事务选择max(id)[以某种方式递增id] [等待用户提交]插入foo值(new_id)中

But again, you should avoid adopting this approach 但同样,您应该避免采用这种方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将与提交的表单相对应的项目ID传递给action / actionlistener方法? - How to pass item id corresponding to submitted form to action/actionlistener methods? 在用户使用Spring MVC提交表单后,如何获取表单的数据? - How to get a form's data after the user submitted it with Spring MVC? 当提交的表单具有属性 enctype="multipart/form-data" 时,如何在控制器中获取表单数据? - How to get form data in controller when the form that was submitted has the attribute enctype="multipart/form-data"? 如何检查表单是否以Java提交 - How to check if form was submitted in Java 如何通过ID获取数据库中的项目? - How to get the items of a Database by ID? 除非提交上一页的表单,否则 Spring Boot 中是否有办法限制对某个端点的访问? - Is there a way in Spring Boot to restrict access to a certain endpoint unless a form from a previous page is submitted? 当JavaScript禁用表单时,JSF表单不会被提交 - JSF form does not get submitted when form is disabled by JavaScript 将Servlet中提交的表单值传递给Java类,从而插入数据库 - passing form values submitted in a servlet to a java class and hence inserting into database 如何获得这种形式的 div id? - How to get the div id's of this form? 如何从 Firestore 数据库中获取文档 ID - How to get document id from firestore database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM