Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Data module(s) and thread

87 views
Skip to first unread message

Jacques Garcia Vazquez

unread,
Mar 5, 2002, 4:28:42 AM3/5/02
to
Hi all,

After creating an application with one or more data modules, you finally
end up by using the global variable of the data module to simplify the data
access (in my case 160 units works with data acces).
I understand that this is not the way we have to handle multi threading - so
the question is:

Is it safe to define the data modules global variable as threadvar ?
Off course this instance will be created for each thread - for example in
the TWebModule constructor or OnCreate event.

Thank in advance
Jacques Garcia Vazquez


Corbin Dunn

unread,
Mar 5, 2002, 1:45:02 PM3/5/02
to
Jacques,
You are going about this the wrong way. Make the data module a private
variable of the webmodule. Create it in the constructor, and free it in the
destructor. Setup any needed components at runtime.
.corbin

"Jacques Garcia Vazquez" <j...@FlexsysBelgium.com> wrote in message
news:3c848fa0_2@dnews...

Jacques Garcia Vazquez

unread,
Mar 6, 2002, 2:46:56 AM3/6/02
to
Hi Corbin,

Thanks for the info but in that case I have to rewrite all my units (160 =
lot of work and late in time) !
By the way this is also the way I want to go, create the datamodule in the
constructor, and free it in the destructor. The only difference is that the
created datamodule in not a member of the webmodule object but is attached
to a threadvar so that available to all referenced units. I only suspect
some problems arround this way - do you have more info about the threadvar ?

Jacques Garcia Vazquez


"Corbin Dunn" <cd...@no.spam.borland.com> wrote in message
news:3c851149$1_2@dnews...

William Buchanan

unread,
Mar 6, 2002, 4:13:57 AM3/6/02
to
Hi Jacques

I asked this question about 2 weeks ago (19/02, titled Double click strange
happenings - this was followed up by some private email from other
parties)......
I had the exact same design as you which worked fine on my local server with
only me using the unit. When I uploaded the unit onto the test system it
worked fine until I clicked a form submit button more than once. This
resulted in my data module losing connection to the db. I was also
dynamically populating a query at runtime but when this happened my SQL was
cleared. I firstly had my DM as a global variable. I then changed it to a
private instance variable of the isapi which cured some of the problems but
not all.
Anyway, the end result was that ISAPI modules can't work in conjunction with
data modules safely (I was advised). I know they appear to work but I was
advised that my problems were due to the use of the data module, as they do
not multi thread in the same way as the isapi. I removed the DM and sure
enough everything works fine!

I know this isn't what you want to hear - i've been there! If anyone else
has another angle on this I would be interested.
If you need any more please feel free to reply.

Regards
Will


"Jacques Garcia Vazquez" <j...@FlexsysBelgium.com> wrote in message
news:3c848fa0_2@dnews...

Shiv Kumar

unread,
Mar 6, 2002, 5:39:11 AM3/6/02
to
"William Buchanan" <william....@bdml.co.uk> wrote in message
news:3c85dda3$1_1@dnews...

> Anyway, the end result was that ISAPI modules can't work in
conjunction with
> data modules safely (I was advised). I know they appear to work but I
was
> advised that my problems were due to the use of the data module, as
they do
> not multi thread in the same way as the isapi. I removed the DM and
sure
> enough everything works fine!
>
> I know this isn't what you want to hear - i've been there! If anyone
else
> has another angle on this I would be interested.
> If you need any more please feel free to reply.


William,

I'm not sure who "advised" you :), but I have been using Data Modules
with WebModules for ages without a problem. In fact, most of my objects
reside in DataModules as well ALL data Access (I have different data
access modules for different databases/data access methods that are
compiled using compiler directives). My Web Modules are data access
agnostic.

Other data access, my other data modules have various business related
functionality including custom business objects.

What's the difference between having a Data Module (as a
member/property) of the WebModule and say a PageProducer? They are both
members of the Web Module.

Yes, declaring a Data Module as a global variable will cause a lot of
grief! This is so, because "global" variables are shared across threads
and as such access to them MUST be serialized.

--
Shiv Kumar
The Delphi Apostle
http://www.matlus.com
http://www.delphisoap.com


Shiv Kumar

unread,
Mar 6, 2002, 5:45:10 AM3/6/02
to
"Jacques Garcia Vazquez" <j...@FlexsysBelgium.com> wrote in message
news:3c85c943_2@dnews...

> Hi Corbin,
>
> Thanks for the info but in that case I have to rewrite all my units
(160 =
> lot of work and late in time) !
> By the way this is also the way I want to go, create the datamodule in
the
> constructor, and free it in the destructor. The only difference is
that the
> created datamodule in not a member of the webmodule object but is
attached
> to a threadvar so that available to all referenced units. I only
suspect
> some problems arround this way - do you have more info about the
threadvar ?

If you must have your data modules as global threadvars I would suggest
creating them as singletons. There is no point having a global variable
in a multi-threaded application that is created for each thread
instance.

You might want to have a global threadsafe list (where the list is
declared as a var in the global section) and ensuring a single instance
of this list. The list in turn contains instances of each of your Data
Modules. You shouldn't have to change your DataModules to do this.

However, sharing your DataModules across threads is bound to impact
(quite heavily) your performance, since every thread will potentially
have to wait on other threads to finish before it can use the data
Modules. Effectively, you're using a single threaded application!

Jacques Garcia Vazquez

unread,
Mar 6, 2002, 5:59:02 AM3/6/02
to
Hi Will,

Well, first thanks for your answer.
But this sound strange and I would be very interrested to know why a datamod
can't works safely with a webmodule.
When looking at the source code, it seems that only one thread access one
webmodule at a time so normally every think is fine if you have a session
(not shared) per datamodule.

On the other side, I would be very interrested to know if someone had
created a web app over an existing -big- application without having to
rewrite all data acces part. The object concept was also done to have
reusability and if you can't reuse existing object in the web app this is a
BIG problem.

Regards
Jacques


"William Buchanan" <william....@bdml.co.uk> wrote in message
news:3c85dda3$1_1@dnews...

Jacques Garcia Vazquez

unread,
Mar 6, 2002, 7:21:30 AM3/6/02
to
Thanks for the info Shiv.
So it seems that I will have to rewrite part of my units that have data
access.
What I will do is what you suggest that is a singleton thread safe list of
datamodule created per thread.
I will replace the global data module variable by a function that return a
per thread datamodule - something like this:

function MyDatamodule: tMyDataModule;
begin
Result := MySingletonList.find (GetCurrentThreadID);
assert (result <> nil);
end;

Then in the constructor of the webmodule I can add my datamodule(s)
MySingletonList.Add (MyDataModule.Create) and free them in the destructor.

This will certainly works - I don't have tested it yet.

Anyway thanks for your help.
Regards
Jacques


"Shiv Kumar" <shivk...@erols.com> wrote in message
news:3c85f36d$1_2@dnews...

William Buchanan

unread,
Mar 6, 2002, 8:33:22 AM3/6/02
to
Hi Shiv
Thanks for the reply. What you say makes sense and I know it will be
correct.
Can you think of any reason I could have experienced problems..... I
originally fell into the global variable trap (you advised me on that one!),
but then declared my data module as an instance var and still had problems
(although not as many). My data module contained 2 TIBDataBase's, 2
TIBTransactions and several TIBQueries. Some of my queries SQL were being
populated at runtime - this is how I discovered the problem: under normal
circumstances everything appeared normal. If a form submit was clicked twice
the site worked as normal except where I was using a query which was
populated at runtime, and only on one of the databases. For some reason the
query was not being populated with sql, even though the procedure seemed to
run.......
However, once I had put all my data components back onto the web module the
whole thing worked as normal.
Having read your post I am confident it was something I was doing
wrong....... but I have never got to the bottom of it.
Anyway, I think I will return to that one another day!!!!


Regards

Will


"Shiv Kumar" <shivk...@erols.com> wrote in message

news:3c85f206$1_2@dnews...

Shiv Kumar

unread,
Mar 6, 2002, 9:16:58 AM3/6/02
to
William,

I think I remember your case :)

>global variable trap (you advised me on that one!),

Did I ask you to use a global variable or advised you against it ? :).

You must have been doing thing wrong. There is no need to treat a
DataModule (or modules) any different to a regular component you drop on
a WebModule.

At the same time, They all behave like a WebModule, in that, they all
seem to be "brain dead" after satisfying a response, so the next time
around you've got to make sure things are "reset/initialized".

William Buchanan

unread,
Mar 6, 2002, 9:24:47 AM3/6/02
to
Hi Shiv
Thanks again......

> Did I ask you to use a global variable or advised you against it ? :).

Advised against.

> At the same time, They all behave like a WebModule, in that, they all
> seem to be "brain dead" after satisfying a response, so the next time
> around you've got to make sure things are "reset/initialized".

This could possibly explain it although most of the initialising takes place
in the web module create event which was being fired correctly.

Will take another look I think!

Regards
Will

Shiv Kumar

unread,
Mar 6, 2002, 9:12:59 AM3/6/02
to
"Jacques Garcia Vazquez" <j...@FlexsysBelgium.com> wrote in message
news:3c86099e_1@dnews...

> Thanks for the info Shiv.
> So it seems that I will have to rewrite part of my units that have
data
> access.
> What I will do is what you suggest that is a singleton thread safe
list of
> datamodule created per thread.

If you're creating a list of data Modules per web module/thread, then
you don't need to have access to them serialized :)

Either you have each data Module as a member/property of the WebModule
class (create them and destroy them yourself or let the WebModule
destroy them if it is the owner).

or

Have a global thread safe, singleton list (that holds each of your data
modules). That is only one instance is shared across threads.

Kevin Frevert

unread,
Mar 6, 2002, 1:42:07 PM3/6/02
to
Jacques,

http://www.midwayusa.com
- 100% Delphi CGI

Add a few items to your cart and get a shipping quote. The datamodule that
calculates shipping charges is the exact same one our contact center
associates use while placing (phone) orders (standard windows app). Address
verification module, shopping cart module, are all shared between web
applications. Only the shopping cart module is created on startup (declared
in the Private section) of the CGI app, everything else is create/destroyed
on an as-needed basis (like tag/action events, declared locally in that
procedure)

Good luck,
krf

Jacques Garcia Vazquez <j...@FlexsysBelgium.com> wrote in message

news:3c85f64a_2@dnews...

Jacques Garcia Vazquez

unread,
Mar 6, 2002, 11:59:44 AM3/6/02
to
Thanks for the info.


"Kevin Frevert" <kfre...@midwayusa.com> wrote in message
news:3c864737$1_1@dnews...

Corbin Dunn

unread,
Mar 6, 2002, 12:08:31 PM3/6/02
to
> Either you have each data Module as a member/property of the WebModule
> class (create them and destroy them yourself or let the WebModule
> destroy them if it is the owner).

As I already mentioned, I highly reccomend doing the above. Otherwise, you
are going to have performance issues.
.corbin


Jacques Garcia Vazquez

unread,
Mar 6, 2002, 12:14:32 PM3/6/02
to
Well neither of those solutions.
The original problem was how to handle the global variables that are
declared with my 20 datamodules that my 160 units use.
1- I don't want to serialize because this is a "very" intensive database
application and if I serialize the data access (which are more than 90% of
the time spend in the web app), I will not be able to handle the requests !
2- I don't want to rewrite my units (around 300 000 lines of code) ....

so what I will do is to replace the global variable with a function that
will return a per thread data module.

Jacques


"Shiv Kumar" <shivk...@erols.com> wrote in message

news:3c86240c$1_2@dnews...

0 new messages