Solved

How to use TSessions for database access in INDY HTTP SERVER

Posted on 2006-06-10
6
1,433 Views
Last Modified: 2008-01-09
I would just like to know the right way to do this:

MY APPLICATION:
1.      i am using delphi 7
2.      i made a web server application using the INDY HTTP SERVER component.
3.      everytime a client accesses the web server application, the application will have to access and update a firebird database (odbc), which is done inside the OnCommandGet event of the IdHTTPServer; Here, many users may access the database simultaneously.
4.      i believe that the right way to do this is by using database sessions (TSession).

QUESTIONS:
1.      As this application is multithreaded were clients may connect simultaneously, could a datamodule containing queries and stored procedures (created at design time) be used with unique DATABASE sessions (TSession)?
2.      OR, should we just create a new DATABASE session (TSession) and the databases, queries and stored procedures components dynamically (runtime) in the OnCommandGet event in the IdHTTPServer?
3.      In the OnCommandGet event, should the database be opened everytime a client connects with a new HTTP session?
4.      In the OnCommandGet event, should the database be closed after processing and giving a response to the client?
5.      Or should the database be closed only when the client ends the HTTP session?
6.      When do we free the TSessions created?

NOTE: the HTTP SESSION is different from the database TSESSION

You may answer my questions by giving me a DELPHI SAMPLE CODE.

Thank you very much for your help,
Syndman
0
Comment
Question by:syndman
  • 3
  • 3
6 Comments
 
LVL 17

Expert Comment

by:TheRealLoki
Comment Utility
You are correct.
For a multithreaded environment (which is what your web sessions are) you need to use a different TSession for each.
If your web session created a TDatamodule, you could use the TSessions's AutoSessionName property. If it doesn't (which is more likely the case) you should make up your own sessionname. You will need a unique name, perhaps some web session id, or similar.

Personally, I would create the TSession dynamically when the web session starts, and free it when the web session ends.
as an example, I will also create a TTable here

    var
{this needs to be in the web session, and accessible for the whole thread}
        Session_ForThisThread: TSession;
        tbBioLife: TTable;
        i: integer; // just for example
        s: string; // just for example
    begin
{create the session and necessary tables
note: you can not use "AutoSessionName" if you have more than 1 TSession on this
form/datamodule, so we will use a UniqueID that is passed in the creation of this web session, and
will be available for thei thread only.}
        Session_ForThisThread := TSession.Create(nil);
        Session_ForThisThread.SessionName := 'Session_' + IntToStr(UniqueID);
        Session_ForThisThread.Active := True;

        tbBioLife := TTable.Create(nil);
        tbBioLife.DatabaseName := 'DBDEMOS';
        tbBioLife.SessionName := 'Session_UniqueID';
        tbBioLife.TableName := 'biolife.db';

        tbBioLife.Open;
        for i := 0 to pred(tbBioLife.FieldCount) do
          s := s + tbBioLife.Fields[i].AsString; // just an example
        tbBioLife.Close;

{destroy the TSession and TTable when the web session/thread is destroyed}
        tbBioLife.Free;
        Session_ForThisThread.Free;
    end;
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 125 total points
Comment Utility
oops, should be
tbBioLife.SessionName := 'Session_' + IntToStr(UniqueID); // the same as we made for the TSession

so I think for Indy HTTP server, it would look something like this :-

// our session with the TSession, and tables needed by the web session
type
  TMyHTTPSession = class(TIdHTTPSession)
  private
    Session_ForThisThread: TSession;
    tbBioLife: TTable;
  end;


procedure TForm1.IdHTTPServer1CreateSession(ASender: TIdContext; var VHTTPSession: TIdHTTPSession);
begin
    VHTTPSession := TMyHTTPSession.Create(IdHTTPServer1.SessionList);
end;

procedure TForm1.IdHTTPServer1SessionStart(Sender: TIdHTTPSession);
begin
    if (Sender is TMyHTTPSession) then
    with (Sender as TMyHTTPSession) do
    begin
        Session_ForThisThread := TSession.Create(nil);
        Session_ForThisThread.SessionName := 'Session_' + SessionID;
        Session_ForThisThread.Active := True;

        tbBioLife := TTable.Create(nil);
        tbBioLife.DatabaseName := 'DBDEMOS';
        tbBioLife.SessionName := 'Session_' +SessionID;
        tbBioLife.TableName := 'biolife.db';
    end;
end;

procedure TForm1.IdHTTPServer1SessionEnd(Sender: TIdHTTPSession);
begin
    if (Sender is TMyHTTPSession) then
    with (Sender as TMyHTTPSession) do
    begin
        tbBioLife.Free;
        Session_ForThisThread.Free;
    end;
end;
0
 

Author Comment

by:syndman
Comment Utility
TheRealLoki,
Thanks for your very comprehensive reply.
May I know your email address.
Thanks again,
Syndman
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:syndman
Comment Utility
TheRealLoki,
Oh, you are one of the top experts here.
Thanks again,
Syndman
0
 
LVL 17

Expert Comment

by:TheRealLoki
Comment Utility
no problem. lokiwashere at yahoo co nz
0
 

Author Comment

by:syndman
Comment Utility
thanks again
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now