简体   繁体   English

同时请求球衣休息服务

[英]concurrent request to jersey rest service

I am developing a very simple REST web service with Eclipse, Tomcat7 and Jersey implementation, with a connection to MySQL. 我正在使用Eclipse,Tomcat7和Jersey实施开发一个非常简单的REST Web服务,并连接到MySQL。 Looking to the jersey documentation i know that every request create a new object of the root resource class. 查看球衣文档,我知道每个请求都会创建根资源类的新对象。 But i dont know if every request is independet, for example if one request have to wait a long time, the server will accept more request normaly? 但是我不知道每个请求是否都是独立的,例如,如果一个请求必须等待很长时间,服务器会正​​常接受更多请求吗?

The problem is : I have 2 main classes, 1 class implements Jersey with annotations(Proxy.java), and other class that connects to a BD(Notificator.java), there is only one instance of this class (Singleton) in order to use only 1 Connection object. 问题是:我有2个主要类,其中1个类实现带有批注(Proxy.java)的Jersey,而其他类连接到BD(Notificator.java),则只有一个此类(Singleton)的实例才能仅使用1个Connection对象。 The classes who implements Jersey use this class. 实现Jersey的类使用此类。 So, if one of the request is blocked , i dont know if the others will run because they are using the same (Notificator.java) instance. 因此,如果其中一个请求被阻止,我不知道其他请求是否会运行,因为它们使用的是相同的(Notificator.java)实例。

The relation is N instances of(Proxy.java) uses the only one (Notificator.java) witch have one Connection to a MySQL. 该关系是(Proxy.java)的N个实例使用唯一的一个(Notificator.java)与MySQL有一个Connection。

Jersey is developed on top of servlets. Jersey是在servlet之上开发的。 There is a new thread for each of the incoming request. 每个传入请求都有一个新线程。 Your code is creating a bottleneck for all the threads as there is a contention for single available connection object. 您的代码正在为所有线程创建瓶颈,因为单个可用连接对象存在争用。 If you have multiple requests then only one request will be using that connection and others have to wait. 如果您有多个请求,则只有一个请求将使用该连接,而其他请求则必须等待。 If the wait is not too long then there is no problem. 如果等待时间不太长,那么就没有问题。 But if wait is more than the HTTP REQUEST TIMEOUT then your other requests may end up as TIMED OUT. 但是,如果等待时间超过了HTTP REQUEST TIMEOUT,那么您的其他请求可能最终会成为TIMED OUT。

I understand that you may be having single connection bottleneck due to some business requriement/ complication. 我了解由于某些业务需求/复杂性,您可能遇到单一连接瓶颈。 So in all such cases where we cannot process all the requests simulataneously and there can be variety of reasons for it, then we should create our web services as Asynchronous. 因此,在所有无法同时处理所有请求且可能有多种原因的情况下,我们应该将Web服务创建为“异步”。 Asynchronous web services work on the model of SUBMIT REQUEST-> REQUEST ACCEPTED(will be processed asynchronously) and JOB URL returned for polling-> CLIENT POLLS till the JOB IS NOT COMPLETED. 异步Web服务在SUBMIT REQUEST-> REQUEST ACCEPTED(将被异步处理)的模型上工作,并返回JOB URL以进行轮询-> CLIENT POLLS,直到未完成JOB。

Hope it helps! 希望能帮助到你!

Try database connection pooling, more on this here: 尝试数据库连接池,有关更多信息,请参见:

http://en.wikipedia.org/wiki/Connection_pool http://en.wikipedia.org/wiki/Connection_pool

How to establish a connection pool in JDBC? 如何在JDBC中建立连接池?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM