Using TidHTTPServer to serve GET request the safe way

流过昼夜 提交于 2019-12-08 02:13:08

问题


I have a class with many services that basically call Database connections (DBISAM or via ZEOS).

I am makinge this class available in a webservice like using TidHTTPServer. I am using the OnCommandGet Event from it and use the ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo to make the requests and results.

I have created a TObjectList to hold an instance of each client related to this service class. The connection to the database is created and freed for every call done, in a hope to not have thread problems.

I want to know if this is the proper way. I am not using session from the TidHTTPServer. Upon each service request I find out the object on the TObjectList that stores the info related to that connection.

I have looked over questions but I did not get all the information I need.

However I am not sure about the safety, specially when many calls can happen simultaneously. I have been using it regularly mostly 1 call per time, since is hard to test with many connections at same time.

I want to know if this is the right approach, or if not, how can I implement a "bridge" to safely call my class (I have created a QueryActivity on this class, mimicking the COM query style) passing request and returning the parameters.

Delphi XE2 - Indy 10.5.8


回答1:


Indy TCP server classes (as tTIdHTTPServer) have the option to assign a application-specific context class. This class contains connection-specific information about the client and can be extended with custom properties to carry your application specific information. To do this, create a subclass from TIdServerContext, and assign the class to the server's ContextClass.

If the connection is kept alive between requests (which is recommended to save resources), this context will keep the application-specific client information for its lifetime.

Indy also allows to iterate the list of all connected connection context, for example to support broadcast messages, or to collect information.

Examples on Stackoverflow:

How to track number of clients with Indy TIdTCPServer

Indy 10 TCP server

How do I send a command to a single client instead of all of them?




回答2:


Dealing with threads and therefore take care about threadsafe you always have to keep an eye on the targets.

Database

MultiUser Databases are threadsafe, SingleUser are not

Database Access Component

Some are threadsafe, some not. If you don't know, treat them as not threadsafe


In your case (hope you have a multiuser database) it is safe to use a connection/query instance per thread.

If you want to speed up your app use an ObjectPool for connection and query instances.

For testing purpose you can build a small client that performs a lot of requests in a loop and run multiple instances of this client.



来源:https://stackoverflow.com/questions/17497566/using-tidhttpserver-to-serve-get-request-the-safe-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!