Solved

How to use TSessions for database access in INDY HTTP SERVER

Posted on 2006-06-10
6
1,458 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
ID: 16906795
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
ID: 16907105
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
ID: 16908496
TheRealLoki,
Thanks for your very comprehensive reply.
May I know your email address.
Thanks again,
Syndman
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

Expert Comment

by:TheRealLoki
ID: 16908537
no problem. lokiwashere at yahoo co nz
0
 

Author Comment

by:syndman
ID: 16908677
thanks again
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

21 Experts available now in Live!

Get 1:1 Help Now