[英]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 我看到两个选择
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.