如何处理node.js中的多个(相同)请求? [英] How to handle multiple (same) request in node.js?

查看:303
本文介绍了如何处理node.js中的多个(相同)请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个在'user'表(mysql db)中插入记录的函数。这个用户表只有'id'主要字段,自动增加,没有任何其他字段是唯一的,但在程序中我正在检查字段的值(例如字段是'用户名')是否存在然后不存在插入记录。但我得到了重复记录,因为两个请求同时进入服务器(我也在(creation_date)字段中保存创建时间)。当我手动逐个插入记录时,它工作完美,但使用程序或JMeter测试工具同时触发多个请求然后重复记录在表格中。



我可以创建用户名唯一字段,但在我的情况下,唯一密钥长度超过密钥长度。



任何其他解决方案请建议。

解决方案

你需要深入研究数据库理论。您的问题是,在已经请求新数据时尚未写入数据,导致正在发送旧数据库集。有很多模式可以解决您的问题,例如同步排队池,例如每次只使用数据库连接请求,另一个正在使用事务等。



你可能缺乏这项任务的大量知识(对不起,没有冒犯) )。

I have written a function to insert record in 'user' table (mysql db). This user table has only 'id' primary field with auto increment and no any other field is unique, but In program I am checking if value of a field (let say for example the field is 'username') is exist then don't insert record. But the I am getting duplication record because the Two requests are coming to the server at same time (I am also saving the creation time in (creation_date) field). When I am manually inserting the record one by one then It Works perfect but using program or JMeter testing tool firing the multiple request at the same time then duplicate record found in table.

I can make username unique field but in my situation unique key length exceeds the key length.

Any other Solution please suggest.

解决方案

you will need to deep dive into database theory. your problem is that data is not yet written while new data is already requested, resulting in an "old" database set is being send. There are a lot of pattern to solve your problem, like a synchronized queuing pool e.g. doing on request per time using only on db connection, another one is using transactions, etc.

you might lack a lot of knowledge for this task (sorry, no offense).


这篇关于如何处理node.js中的多个(相同)请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆