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

ahem.. Question about using tWebModule and tDataModule

142 views
Skip to first unread message

Christian

unread,
Mar 26, 2001, 3:17:30 PM3/26/01
to
Dear all,
this is weird.
SHORT: tWebModule and tDataModule cannot coexist.
LONG:
When trying to create an ISAPI server from a traditional executable
application with Delphi 5, I encountered a bizarre limitation.
I wanted to keep the units in the original project unchanged as much as
possible. The original project uses a tDataModule for its data. I created a
new ISAPI webserver, with its own tWebModule and included the original
tDataModule in the new project.

But this is, sadly, impossible - it will cause runtime access violations!
You can never add a tDataModule to a tWebModule application.
In other words, you can never port an application with a tDataModule to a
ISAPI webserver with the tDataModule intact. WHAT!?!

Borland's solution in the help file: Merge your code in the two modules. No
module can coexist with a tWebModule.
This really came as a surprise to me, as you can have as many tDataModules
coexisting in an application.

I am not ready to start merging the two modules into one, since the
tDataModule belongs in the old project as well,
and simply cannot come to terms with having to make two copies of the
module. Similar maintainance in two modules is bad style.

So here comes the question,
does anybody know of a different option?
Would it suffice to encapsulate the tDataModule in a separate .DLL project
and then let the ISAPI webserver access that library?
Or is coexistence really impossible?


Kind regards,
Christian Loft


Wayne Niddery (TeamB)

unread,
Mar 26, 2001, 5:42:55 PM3/26/01
to
Christian wrote in message <3abfa454_2@dnews>...

>Dear all,
>this is weird.
>SHORT: tWebModule and tDataModule cannot coexist.

There is no such limitation, you can freely use datamodules in a web
application.

What the issue is that you are running into is the fact that a web
module is, by definition, multi-threaded, and different threads cannot
safely access the same datamodule (or the same anything else) at the
same time. Do not autocreate your datamodule(s). Instead, when you
handle an action in your webmodule you must decide which of the
datamodules you need to service that request and create an instance of
them then (and free them at the end of the action - best done in a try /
finally block).

--
Wayne Niddery (WinWright Inc.)
RADBooks - http://members.home.net/wniddery/RADBooks/delphibooks.html
"At the apex of every great tragedy of mankind there stands the figure
of an incorruptible altruist" - Ayn Rand


Andrew Watts

unread,
Mar 27, 2001, 2:40:33 AM3/27/01
to
My question is the same as Wayne's, Do not autocreate your datamodule(s)??

I've an ISAPI app the has both a TWebModule and TDataModule. However I did
hit a problem when we first started developing but this was due to the
TDataModule being autocreated, or being created within the initialization
section of a pas file. Once this was removed and dynamically created as and
when needed we've had no problems since.

Andy.
andrew...@xenicom.com

>Do not autocreate your datamodule(s). ??

Christian

unread,
Mar 27, 2001, 3:02:59 AM3/27/01
to
Dear Wayne,
thank you for your detailed reply. I am very pleased to know that what I
thought was simply not the case.

An instance of the tDataModule per tWebModule action is, of course,
necessary for data integrity, now that I think of it.
My only grudge now is that my tDataModule contains 16 tables, which are all
needed for the action.
Thus, instantiating the module takes time,
and the multiple instantiations will lay claims on db connections and
memory.
But this was to be expected, I guess.

Now I know where to go.
Kind regards,
Christian Loft

"Wayne Niddery (TeamB)" <winw...@chaffhome.com> wrote in message
news:3abfc4d8$1_2@dnews...

Christian

unread,
Mar 27, 2001, 3:54:18 AM3/27/01
to
Dear Wayne and Andew,
thank you for your replies.

I have changed my code but still get the exception "Only one datamodule per
application".
I am now only creating the tWebModule in the project source.
The webmodule has an action which looks like this:

procedure TWebModule1.WebModule1ProcessAction(Sender: TObject; Request:
TWebRequest; Response: TWebResponse; var Handled: Boolean);
var Data : tdata; // <----- this is my tDataModule object
begin
//Handling Request fields.. not shown here

Data := tData.Create(self);
//Process data.... not shown here

//Handling Response content ... not shown here

Data.free;
end;


This is wrong, I gather..
But how is it wrong?

Best regards,
Christian

"Andrew Watts" <andrew...@xenicom.com> wrote in message
news:3ac043f1_1@dnews...

Danilo Cuculic

unread,
Mar 27, 2001, 5:02:26 AM3/27/01
to
<...>

>My only grudge now is that my tDataModule contains 16 tables, which are all
>needed for the action.
>Thus, instantiating the module takes time,

There is strategy to overcome this - create your DM in WebModuleCreate
and free it in WebModuleDestroy.

>and the multiple instantiations will lay claims on db connections and
>memory.
>But this was to be expected, I guess.

Connection object (TDatabase, TADOConnection, ...) could be passed to DM
initialization for reuse.

Also, there is a way to reuse DM in a web application with a visual form
inheritance: create new WebBroker project, add DM unit to project,
create new unit that inherits from DM and add TWebDispatcher to it - now
we have main module where new web actions can be defined. In this setup
all DB specific stuff goes to ancestor (original DM) , WebBroker
specific stuff goes to derived module.

---
Danilo Cuculic

Michael REMY

unread,
Mar 27, 2001, 8:28:56 AM3/27/01
to
This looks good... You may forgot to remove the line which creates your
datamodule in the initialization section or in the dpr file. Take care
about this, Delphi automatically adds this new line when you had a
module in your project.

Regards,
Michael

Danilo Cuculic

unread,
Mar 27, 2001, 7:16:46 AM3/27/01
to
>I have changed my code but still get the exception "Only one datamodule per
>application".
>I am now only creating the tWebModule in the project source.
>The webmodule has an action which looks like this:
<...>
> Data := tData.Create(self);
<...>

>This is wrong, I gather..
>But how is it wrong?

Data := tData.Create(nil);

---
Danilo Cuculic

Christian

unread,
Mar 28, 2001, 2:23:30 AM3/28/01
to
Dear Danilo,

thank you for your replies.
I will go try nil first..
Reuse, however, is to be preferred. I think I would go with the inheritance
solution.

Best regards,
Christian Loft

"Danilo Cuculic" <dan...@spamblock-techie.com> wrote in message
news:aon0ctotf1os0mj9l...@4ax.com...

0 new messages