java - 别人调用我接口,往我数据库里插数据问题,谢谢
PHP中文网
PHP中文网 2017-04-18 09:07:15
0
5
520

局域网里,别人使用C#写的程序,开了4个线程,往我接口里插入4张不同表的数据,问题如下:

  • 1.发现我的表里有很多重复数据,和对方确认后,发现对方用C#程序调用我的接口时,很多情况是因为tomcat没有及时返回response,导致对方的程序以为没有插入成功,又重新插入一遍。

    • 但是我自己用java程序来模拟了100个请求,没有发现这种现象。

  • 2.我觉得那个人写的程序可能超时时间设置的太短,因为我感觉在局域网的情况下,才开了4个线程,没有达到tomcat的瓶颈吧?(不知道这么想对不对)

  • 3.要排除对方调用我的接口插入数据时,插入重复数据,我想了两种办法,不知道哪一个效率更好,或者大家有什么更好的去重方法

    • 在数据库里设置主键,或者联合主键,让数据库帮我去判断数据是否重复

    • 每次在插入数据的时候,我用程序去查询数据库里是否有重复数据,但是我觉得一旦数据量大的话,查询起来比较慢

谢谢

PHP中文网
PHP中文网

认证0级讲师

reply all(5)
PHPzhong

Thank you, Chairman, for the invitation.
The two methods mentioned by the Chairman are the same. They are both methods to prevent duplication. The main difference is that deduplication of primary keys or unique indexes can effectively solve the problem of duplicate insertions, while checking duplications in the program. Insertion, since these two operations are not atomic, it still cannot prevent requests from being inserted between your query and the insertion, so there is still a risk of duplication.

Ty80

As long as the database can handle the amount of requests, you can use the unique index of the database. You can also use redis to make uniqueness judgments at the application layer.

小葫芦

I feel that the focus of this question is not on data deduplication, but on why the student initiated a retry when he did not receive a message that the operation failed.
It is recommended to use an interceptor to log to see the processing time of each request.

Ty80

The calling party should wait for the request response before doing the next operation. For example, the request timeout can be set shorter.

巴扎黑

The simplest and most effective method is to build a unique index, without considering performance issues for the time being.
If we really encounter a database bottleneck, it is estimated that the architecture will not be what it is now.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template