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

Multi-threading a DataModule

203 views
Skip to first unread message

Shiv Kumar

unread,
May 10, 1999, 3:00:00 AM5/10/99
to
I posted this message before, but it seems to have got lost....

I want to know:
1. If it's possible to spawn a new thread and create a DataModule within
this thread.
2. Is there any benefit to doing this ? In the sense, does the
DataModule/TSession have some way of handling multiple requests
simultaneously ?

Shiv.

Xavier Pacheco (TeamB)

unread,
May 10, 1999, 3:00:00 AM5/10/99
to
Shiv,
Yes it possible. The key is that the datamodule must have a
TSession component on it and all database connection must go thru that
session. The TSession.AutoName property must be set to true.

When I do this, I create the datamodule dynamically withing the
TThread.Execute method. Therefore, each thread will have its own
datamodule instance and its own session.

--- x
==============================
Xavier Pacheco (TeamB)
xav...@xapware.com

Sorry but TeamB cannot answer support
questions received via email.

Jeff Overcash (TeamB)

unread,
May 10, 1999, 3:00:00 AM5/10/99
to
Shiv,

Over the weekend I put together two examples of doing this for someone
else. If you would like a copy of those two little examples e-mail me
and I'll send it to you.

--
---------------------------------------------------------------------
Jeff Overcash (TeamB) On waves of silver I dreamed of gold
(Please do not email 'Till I lost the peace that dreaming gives
me directly unless I dreamed of the moment of my own death
asked. Thank You) That no one ever dreams and lives (Marillion)
---------------------------------------------------------------------

Shiv Kumar

unread,
May 10, 1999, 3:00:00 AM5/10/99
to
Thanks for the reply Xavier. I now have another question.

What would be the advantages / disadvantages of this design ? I can
understand the overhead in creating a new instance of a DataModule for each
request.

This is what I'm doing...

I'm building a 3-tiered system using sockets as the transport layer.

In the applcation server layer I currently have a DataModule with a TSession
(AutoSessionName := True) and TDatabase component. For every client
connection, I am spawning a new thread for the actual transfer of data and
requests. But all threads use the same datamodule. I don't have good feeling
about that !

So my questions again...

What are the disadvantages of this design versus the new instance of a
datamodule (created within each thread) . So far I have not had problems
with multi-users. But will I ? What about the number of sessions hitting the
database server ?

Shiv.
Xavier Pacheco (TeamB) wrote in message
<373a0c07....@forums.inprise.com>...

Dan Butler

unread,
May 10, 1999, 3:00:00 AM5/10/99
to
At least that's the common wisdom.... Most apps that do multi-threading
should probably work this way, but what I've discovered is that the main
thing is to avoid using the same TSession by two threads simultaneously. If
you look at the BkQuery demo that comes with Delphi carefully, you will see
that in this example they actually use the same TSession object from two
different threads--but not at the same time.

Dan

Dan Butler

unread,
May 10, 1999, 3:00:00 AM5/10/99
to
> In the applcation server layer I currently have a DataModule with a
TSession
> (AutoSessionName := True) and TDatabase component. For every client
> connection, I am spawning a new thread for the actual transfer of data and
> requests. But all threads use the same datamodule. I don't have good
feeling
> about that !

The same *instance*, or *class*???

> What are the disadvantages of this design versus the new instance of a
> datamodule (created within each thread) . So far I have not had problems
> with multi-users. But will I ? What about the number of sessions hitting
the
> database server ?

If you only have one DataModule that all threads can use, then you would
need to protect that DataModule with a critical section (or some other
means), which means only one thread at a time can use it. This would cause
major performance problems (obviously). You want to be able create multiple
threads so that more than one user at a time can be doing database access in
your application server. However, the danger is having too many threads
(and too many sessions/database connections). Too many threads means a lot
of context-switching, which will hurt performance. The number of database
connections is a totally different issue.

HTH,

Dan

Adnan Zaman

unread,
May 11, 1999, 3:00:00 AM5/11/99
to
hi Xavier,

can i get the code for those 2 examples that u offered to Shiv?
i would really appreciate that.

thanx
Adnan.
(in case u do want to send it to me, here is my e-mail: adnan...@aol.com)

Dan Butler <no.spam.pr...@yahoo.com> wrote in message
news:7h7n4a$n0...@forums.borland.com...

Jeff Overcash (TeamB)

unread,
May 11, 1999, 3:00:00 AM5/11/99
to
That was me, not Xavier <g>. I'll send it to you.

Adnan Zaman wrote:
>
> hi Xavier,
>
> can i get the code for those 2 examples that u offered to Shiv?
> i would really appreciate that.
>
> thanx
> Adnan.
> (in case u do want to send it to me, here is my e-mail: adnan...@aol.com)
>

--

Xavier Pacheco (TeamB)

unread,
May 11, 1999, 3:00:00 AM5/11/99
to
Jeff,

Good deal. Please send my way and I'll put them in my library of
"how tos". :) -- x

Xavier Pacheco (TeamB)

unread,
May 11, 1999, 3:00:00 AM5/11/99
to
Shiv,
I think that Dan pointed out most of the issue here. The key thing
to remember is that if it's possible for two separate threads to
attempt to access the same resource, be it a session, file, or memory
location, then you have to make it thread safe. By creating a
separate session for each thread, you avoid any possible conflicts.

Shiv Kumar

unread,
May 11, 1999, 3:00:00 AM5/11/99
to
It's the same instance ( of the DataModule ). So it looks like from your
answer, that I should create an instance of a new dataModule each time I
spawn a new thread. Just like the examples that Xavier sent me ?

This then begs another question....

Currently, I have tested my server application with about 25 concurrent
users (25 threads hitting one datamodule) , each doing updates, deletes,
inserts , views , with no problem so far !! How is it that nothing has
happened yet ? I'm not using critical sections either.

This is scary, as my server is running off of my PC at home, while the users
are the people at work (including me). If my server bombs out or crashes....

I'll change the server app to create a new instance of the DM. But if there
is any explanation for the above, I'd appreciate it.

Shiv.


Dan Butler wrote in message <7h7nls$mv...@forums.borland.com>...

Xavier Pacheco (TeamB)

unread,
May 11, 1999, 3:00:00 AM5/11/99
to
Actually, the examples were Jeff's.
--- x

Andrey Egorov

unread,
May 12, 1999, 3:00:00 AM5/12/99
to

Hi Jeff!

Please don't forget to send it to me too.

Thank you

Andrey (a_eg...@mtu-net.ru)

Adnan Zaman

unread,
May 12, 1999, 3:00:00 AM5/12/99
to
thanx Jeff for sending me those files. but now i have another problem. i
can't build the files in 'withtdat'.

the error says: "Required package: RXDB4 not found". what's this package and
how can i do a successful build? (btw, i am using delphi 4.03 (CS)).

thanx
Adnan.


Andrey Egorov <a_eg...@mtu-net.ru> wrote in message
news:7ha9s5$pc...@forums.borland.com...

Dan Butler

unread,
May 12, 1999, 3:00:00 AM5/12/99
to
Sounds to me like you've just been lucky so far. Actually, what I do in my
app is create a pool of sessions (this would be DataModules in your case),
and then I have a mechanism for requesting one whenever a thread needs one.
It finds one that is not currently "in use" by another thread, and then
locks it (using a critical section) before returning it to the requesting
thread. The thread is then responsible for unlocking it as soon as its done
so that it will be available for some other thread to use. This way I don't
have to have so many sessions, database connections, etc.

Dan

Shiv Kumar <sku...@datasourceinc.com> wrote in message
news:7ha5op$p2...@forums.borland.com...

Dan Butler

unread,
May 12, 1999, 3:00:00 AM5/12/99
to
I think that is RX Lib, which is a freeware available at:
http://www.officeauto.com/ (I think that's right, although I can't seem to
see that page right now).

Dan

Adnan Zaman <Adnan...@aol.com> wrote in message
news:7hblrn$q9...@forums.borland.com...

Rea Berryman

unread,
May 12, 1999, 3:00:00 AM5/12/99
to
Uncheck the "Build with Packages" option found under Project/Options/Packages

Hope this helps,
Rea

Jeff Overcash (TeamB)

unread,
May 12, 1999, 3:00:00 AM5/12/99
to
Turn off use packages in the project options. I forgot to do that
before sending them out <g>. The packages list reflects my installed
packages. Turning it off will fix that.

Adnan Zaman wrote:
>
> thanx Jeff for sending me those files. but now i have another problem. i
> can't build the files in 'withtdat'.
>

> the error says: "Required package: RXDB4 not found". what's this package and
> how can i do a successful build? (btw, i am using delphi 4.03 (CS)).
>

> thanx
> Adnan.
>
> Andrey Egorov <a_eg...@mtu-net.ru> wrote in message
> news:7ha9s5$pc...@forums.borland.com...
> >
> > Hi Jeff!
> >
> > Please don't forget to send it to me too.
> >
> > Thank you
> >
> > Andrey (a_eg...@mtu-net.ru)
> >

--

Craig Woodard

unread,
May 13, 1999, 3:00:00 AM5/13/99
to
Could I also get a copy, please? TIA!

"Jeff Overcash (TeamB)" wrote:

> That was me, not Xavier <g>. I'll send it to you.
>
> Adnan Zaman wrote:
> >
> > hi Xavier,
> >
> > can i get the code for those 2 examples that u offered to Shiv?
> > i would really appreciate that.
> >
> > thanx
> > Adnan.
> > (in case u do want to send it to me, here is my e-mail: adnan...@aol.com)
> >
>

Wilson Sun

unread,
May 14, 1999, 3:00:00 AM5/14/99
to
Hi, Jeff,

Can I ask for one copy of those 2 examples as well?

Thanks in advance.
Wilson Sun

Manuel Perez

unread,
May 17, 1999, 3:00:00 AM5/17/99
to Jeff Overcash (TeamB)
Hi Jeff,
I will appreciate if you send me your examples.
Thanks in advance.
Manuel Perez.
email-1 : nova...@nl1.telmex.net.mx
email-2 : nova...@compuserve.com

Jeff Overcash (TeamB)

unread,
May 17, 1999, 3:00:00 AM5/17/99
to
It's been sent. Anyone else who would like it please e-mail me. I
usually only glance at this newsgroup so a request here stands a good
chance of slipping past me. Thanks.

Manuel Perez wrote:
>
> Hi Jeff,
> I will appreciate if you send me your examples.
> Thanks in advance.
> Manuel Perez.
> email-1 : nova...@nl1.telmex.net.mx
> email-2 : nova...@compuserve.com
>

--

Manuel Perez

unread,
May 19, 1999, 3:00:00 AM5/19/99
to Jeff Overcash (TeamB)
Hi Jeff,
-Thanks for the examples.
-I have a question about the examples.
Is it possible to use only 1 database connection, and keep the thead
safe ?
If It is, I will appreciate if you use the examples to explain it to me.

Thanks in advance for your comments and help, and sorry if it is a dummy
question. I am a starting to program with Delphi.

Jeff Overcash (TeamB)

unread,
May 19, 1999, 3:00:00 AM5/19/99
to
No you will need a minimum of two. On session is created by default in
the main application. The threaded Query must have its own Session so
that would make two connections. You can reuse this second session
though with many queries.

Ken Byington

unread,
May 20, 1999, 3:00:00 AM5/20/99
to
How about posting the examples in CodeCentral?

Jeff Overcash (TeamB)

unread,
May 20, 1999, 3:00:00 AM5/20/99
to
I keep meaning too <g>. I'll try to remember tonight.

Manuel Perez

unread,
May 21, 1999, 3:00:00 AM5/21/99
to Dan Butler
Hi Dan,
I need to share a database connection with 16 threads.
Your comments looks like you have the answer. So, I will appreciate if you send
me an example, if you have it available.

Thanks in advance for your help.

Rene Trevino

unread,
May 22, 1999, 3:00:00 AM5/22/99
to
Hi there,

I was in the same case but I make this in the unit's Thread:
In Interfase section I add the following declaration:
Var
DBProcess: DataModuleX;

In the DataModuleX, I have:
1 TDataBase
3 TDataSet
3 TQuery

And I have not any problem with access to my DB Server. I can Insert, Edit, View
and more. In conclusion, I believe that this way is Thread Safe.

But I have the following problem. Each thread use one Lisence of my DB Server
(PROGRESS v8.2b).

1.- How can I do to make just One Connection (one lisence) shared with all
thread, with Thread Safe?

2.-When in Run-Time try to close one connection (DBProcess.Database1.Close or
DBProcess.Database1.Connected:=False) in any thread to free this lisence, my app
is aborted. Any suggestion?

Thanks
Rene Trevino

Shiv Kumar wrote:

> Thanks for the reply Xavier. I now have another question.
>
> What would be the advantages / disadvantages of this design ? I can
> understand the overhead in creating a new instance of a DataModule for each
> request.
>
> This is what I'm doing...
>
> I'm building a 3-tiered system using sockets as the transport layer.
>

> In the applcation server layer I currently have a DataModule with a TSession
> (AutoSessionName := True) and TDatabase component. For every client
> connection, I am spawning a new thread for the actual transfer of data and
> requests. But all threads use the same datamodule. I don't have good feeling
> about that !
>

> So my questions again...


>
> What are the disadvantages of this design versus the new instance of a
> datamodule (created within each thread) . So far I have not had problems
> with multi-users. But will I ? What about the number of sessions hitting the
> database server ?
>

> Shiv.
> Xavier Pacheco (TeamB) wrote in message
> <373a0c07....@forums.inprise.com>...
> >Shiv,
> > Yes it possible. The key is that the datamodule must have a
> >TSession component on it and all database connection must go thru that
> >session. The TSession.AutoName property must be set to true.
> >
> > When I do this, I create the datamodule dynamically withing the
> >TThread.Execute method. Therefore, each thread will have its own
> >datamodule instance and its own session.
> >

Manuel Perez

unread,
May 22, 1999, 3:00:00 AM5/22/99
to
Hi Jeff,
I am sorry, but I did not understand you comment.
From you examples, I test the example and . . .
- I monitored the Progress Database and we have 1 database connection for each
thread/Session/Query.
- From Delphi > view> Threads, we have 2>3>2 Threads.

From Dan's comments in this Thread, He talks about . .
- Generate a pool of datamodules (database connection), to be assigned to a
thread, then lock it with a "critical section" ?

Now what I need is to have only 1 Database connection shared to be used by all
the threads/Session/Query.
Is it possible ? If so, What do I must do in your example ?

Thanks in advance for your help.

"Jeff Overcash (TeamB)" wrote:

> No you will need a minimum of two. On session is created by default in
> the main application. The threaded Query must have its own Session so
> that would make two connections. You can reuse this second session
> though with many queries.
>

> Manuel Perez wrote:
> >
> > Hi Jeff,

> > -Thanks for the examples.
> > -I have a question about the examples.
> > Is it possible to use only 1 database connection, and keep the thead
> > safe ?
> > If It is, I will appreciate if you use the examples to explain it to me.
> >
> > Thanks in advance for your comments and help, and sorry if it is a dummy
> > question. I am a starting to program with Delphi.
> >

Jeff Overcash (TeamB)

unread,
May 22, 1999, 3:00:00 AM5/22/99
to
Niether of my examples shared a Session across DataModules. What you will need
to do a create a session strictly dedicated to the Threads, you don't want to
use the default Session used in the Main thread. Each TSession will create
either a default TDatabase connection (the WithoutTDatabase example) or will use
a physical TDatabase (the other example). Either way, you will get another
connection. You need to route everything through a single TSession if you only
want 1 DB connection from a lisnce standpoint. Then you can pass that session
as a param to the TDataModule's constructor and use that Session. Both examples
I gave out have their own TSession with each DataModule so will have a DB
connection per DataModule created.

I've been planning on getting a Session / Thread pooling example made up using
DataModules, but haven't had time. I'll see if I can get something together
this week (no promises becasue I do have some traveling involved this week, but
I'll try). Take a look at the Pooler example in the Midas section of the
examples for ideas on how to pool DataModules to limit Sessions. This example
does not use threads, but does give the basics of pooling.

Manuel Perez wrote:
>
> Hi Jeff,

> I am sorry, but I did not understand you comment.
> From you examples, I test the example and . . .
> - I monitored the Progress Database and we have 1 database connection for each
> thread/Session/Query.
> - From Delphi > view> Threads, we have 2>3>2 Threads.
>
> From Dan's comments in this Thread, He talks about . .
> - Generate a pool of datamodules (database connection), to be assigned to a
> thread, then lock it with a "critical section" ?
>
> Now what I need is to have only 1 Database connection shared to be used by all
> the threads/Session/Query.
> Is it possible ? If so, What do I must do in your example ?
>
> Thanks in advance for your help.
>

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
The fool escaped from paradise will look over his shoulder and cry
Sit and chew on daffodils and struggle to answer why?
As you grow up and leave the playground
Where you kissed your Prince and found your frog
Remember the jester that showed you tears, the script for tears. (Fish)
--

0 new messages