Board index » delphi » Threading with TQuery

Threading with TQuery

Hi,

I'm using Delphi 4 c/s to do some multithreading database queries.

There are three queries which are started all at the same time.
Each thread has got it's own database object and of course it's own
session object with AutoSessionName set to true.

All runs fine, however I discovered in the log files, which I write to
disk, some stange things, I can not explain at all:

1. As mentioned above: All threads are started the same time.
2. the first thread manages to login to database.
3. the second thread logs into database just when the first thread did a
logout from db.
4. whereas the third thread logs on rights after the 2nd one logs out
from db again.

I thought that multithreading means "nearly all at the same time".
In my example above I expected that all threads log into db at the same
time and do some simultanously db processing.

Any ideas to that stange behaviour? I do not use any vcl components
besides the db aware ones!

Michael
--
Michael Fritz

Please remove S_P_A_M_ from mail address when answering.
mailto: Fritz.Michael@S_P_A_M_kermi.de

 

Re:Threading with TQuery


Quote
Michael Fritz wrote in message <38E8558D.A1B25F0F@S_P_A_M_Kermi.de>...

>I thought that multithreading means "nearly all at the same time".
>In my example above I expected that all threads log into db at the same
>time and do some simultanously db processing.

>Any ideas to that stange behaviour? I do not use any vcl components
>besides the db aware ones!

If you have attached data-aware controls to these threaded querys, this may
be why. The VCL controls are not thread safe and so a threaded query should
not have any such visual connections.

--
Wayne Niddery - WinWright Consulting
RADBooks - http://members.home.net/wniddery/
You have a Right to Free Speech, but not the right to make me listen, nor to
use my property as a soapbox.

Re:Threading with TQuery


Wayne,

as pointed out in the last line I do not use any visual components in my
thread.
The threads are started from inside a Win NT service and are started at
a specific timepoint.

The job of the thread is just to append the records from one table to
another table.
That's all.

Any further ideas?
Michael

"Wayne Niddery (TeamB)" schrieb:

Quote

> Michael Fritz wrote in message <38E8558D.A1B25F0F@S_P_A_M_Kermi.de>...

> >I thought that multithreading means "nearly all at the same time".
> >In my example above I expected that all threads log into db at the same
> >time and do some simultanously db processing.

> >Any ideas to that stange behaviour? I do not use any vcl components
> >besides the db aware ones!

> If you have attached data-aware controls to these threaded querys, this may
> be why. The VCL controls are not thread safe and so a threaded query should
> not have any such visual connections.

> --
> Wayne Niddery - WinWright Consulting
> RADBooks - http://members.home.net/wniddery/
> You have a Right to Free Speech, but not the right to make me listen, nor to
> use my property as a soapbox.

--
Michael Fritz

Please remove S_P_A_M_ from mail address when answering.
mailto: Fritz.Michael@S_P_A_M_kermi.de

Re:Threading with TQuery


Quote
Michael Fritz wrote in message <38E995BF.9285AFC3@S_P_A_M_Kermi.de>...

>The threads are started from inside a Win NT service and are started at
>a specific timepoint.

>The job of the thread is just to append the records from one table to
>another table.

Make sure the query used in each thread is, in fact, connecting to the
specific TDatabase and TSession components you are supplying for them. Make
sure you aren't using the Synchronize method from within any of the threads.

If these are long querys then you should be able to see that they are
running concurrently, if they are quite fast however, then it is entirely
possible one completes before the next gets started.

I can at least vouch that they do work - I have an app in successful release
that *depends* on threaded queries actually working.

--
Wayne Niddery - WinWright Consulting
RADBooks - http://members.home.net/wniddery/
You have a Right to Free Speech, but not the right to make me listen, nor to
use my property as a soapbox.

Re:Threading with TQuery


Wayne,

thank you for answering again. Could you provide me with a small demo
application?
It must not be a full running one, but only the appropriate lines which are
of further interest.

Michael

"Wayne Niddery (TeamB)" <winwri...@chaffhome.com> schrieb im Newsbeitrag
news:38e9fd26@dnews...

Quote
> Michael Fritz wrote in message <38E995BF.9285AFC3@S_P_A_M_Kermi.de>...

> >The threads are started from inside a Win NT service and are started at
> >a specific timepoint.

> >The job of the thread is just to append the records from one table to
> >another table.

> Make sure the query used in each thread is, in fact, connecting to the
> specific TDatabase and TSession components you are supplying for them.
Make
> sure you aren't using the Synchronize method from within any of the
threads.

> If these are long querys then you should be able to see that they are
> running concurrently, if they are quite fast however, then it is entirely
> possible one completes before the next gets started.

> I can at least vouch that they do work - I have an app in successful
release
> that *depends* on threaded queries actually working.

> --
> Wayne Niddery - WinWright Consulting
> RADBooks - http://members.home.net/wniddery/
> You have a Right to Free Speech, but not the right to make me listen, nor
to
> use my property as a soapbox.

Re:Threading with TQuery


It appears like MSSQL 7 has one optimization that affects simultaneous
queries - time slice is quite long, so if your queries are fast it appears
like they're serialized.
Also, if one query blocks others and blocking option is on, they will be
serialized.

--
----------------------
Regards
Robert Cerny
Remove both qwe when replying
email: robert.qwe.ce...@neosys.xrs.qwe.si

No questions via email, unless explicitly invited.

Quote
Michael Fritz wrote in message <38E995BF.9285AFC3@S_P_A_M_Kermi.de>...
>Wayne,

>as pointed out in the last line I do not use any visual components in my
>thread.
>The threads are started from inside a Win NT service and are started at
>a specific timepoint.

>The job of the thread is just to append the records from one table to
>another table.
>That's all.

Re:Threading with TQuery


Robert,

Since I do not use MSSQL but other RDBMS this should not happen to my
program.

Besides each of the thread runs, when started in a "normal" application, for
about two upto five minutes.

Thanks for answering,

Michael

"Robert Cerny" <robert.qwe.ce...@neosys.xrs.qwe.si> schrieb im Newsbeitrag
news:8cdgcb.47e.1@neosys.xrs.si...

Quote
> It appears like MSSQL 7 has one optimization that affects simultaneous
> queries - time slice is quite long, so if your queries are fast it appears
> like they're serialized.
> Also, if one query blocks others and blocking option is on, they will be
> serialized.

> --
> ----------------------
> Regards
> Robert Cerny
> Remove both qwe when replying
> email: robert.qwe.ce...@neosys.xrs.qwe.si

> No questions via email, unless explicitly invited.
> Michael Fritz wrote in message <38E995BF.9285AFC3@S_P_A_M_Kermi.de>...
> >Wayne,

> >as pointed out in the last line I do not use any visual components in my
> >thread.
> >The threads are started from inside a Win NT service and are started at
> >a specific timepoint.

> >The job of the thread is just to append the records from one table to
> >another table.
> >That's all.

Re:Threading with TQuery


If all you're doing is moving records from one table to another, why not
write a stored proc to do it and simply kick it off from the client?  If
your server handles threading itself, then you shouldn't have to worry about
it...

Just a thought -

  T

Quote
"Michael Fritz" <fritz.mich...@gmx.de> wrote in message

news:8cdctq$8ae2@bornews.borland.com...
Quote
> Robert,

> Since I do not use MSSQL but other RDBMS this should not happen to my
> program.

> Besides each of the thread runs, when started in a "normal" application,
for
> about two upto five minutes.

> Thanks for answering,

> Michael

> "Robert Cerny" <robert.qwe.ce...@neosys.xrs.qwe.si> schrieb im Newsbeitrag
> news:8cdgcb.47e.1@neosys.xrs.si...
> > It appears like MSSQL 7 has one optimization that affects simultaneous
> > queries - time slice is quite long, so if your queries are fast it
appears
> > like they're serialized.
> > Also, if one query blocks others and blocking option is on, they will be
> > serialized.

> > --
> > ----------------------
> > Regards
> > Robert Cerny
> > Remove both qwe when replying
> > email: robert.qwe.ce...@neosys.xrs.qwe.si

> > No questions via email, unless explicitly invited.
> > Michael Fritz wrote in message <38E995BF.9285AFC3@S_P_A_M_Kermi.de>...
> > >Wayne,

> > >as pointed out in the last line I do not use any visual components in
my
> > >thread.
> > >The threads are started from inside a Win NT service and are started at
> > >a specific timepoint.

> > >The job of the thread is just to append the records from one table to
> > >another table.
> > >That's all.

Re:Threading with TQuery


Thomas,

thanks for your idea, but the RDBMS I use is not capable of handling stored
procedures properly. Besides Delphi does not get along with this database
and it's stored proc.

Michael
"Thomas J. Theobald" <ttheob...@opentable.com> schrieb im Newsbeitrag
news:8cgh3c$7hk2@bornews.borland.com...

Quote
> If all you're doing is moving records from one table to another, why not
> write a stored proc to do it and simply kick it off from the client?  If
> your server handles threading itself, then you shouldn't have to worry
about
> it...

> Just a thought -

>   T

> "Michael Fritz" <fritz.mich...@gmx.de> wrote in message
> news:8cdctq$8ae2@bornews.borland.com...
> > Robert,

> > Since I do not use MSSQL but other RDBMS this should not happen to my
> > program.

> > Besides each of the thread runs, when started in a "normal" application,
> for
> > about two upto five minutes.

> > Thanks for answering,

> > Michael

> > "Robert Cerny" <robert.qwe.ce...@neosys.xrs.qwe.si> schrieb im
Newsbeitrag
> > news:8cdgcb.47e.1@neosys.xrs.si...
> > > It appears like MSSQL 7 has one optimization that affects simultaneous
> > > queries - time slice is quite long, so if your queries are fast it
> appears
> > > like they're serialized.
> > > Also, if one query blocks others and blocking option is on, they will
be
> > > serialized.

> > > --
> > > ----------------------
> > > Regards
> > > Robert Cerny
> > > Remove both qwe when replying
> > > email: robert.qwe.ce...@neosys.xrs.qwe.si

> > > No questions via email, unless explicitly invited.
> > > Michael Fritz wrote in message <38E995BF.9285AFC3@S_P_A_M_Kermi.de>...
> > > >Wayne,

> > > >as pointed out in the last line I do not use any visual components in
> my
> > > >thread.
> > > >The threads are started from inside a Win NT service and are started
at
> > > >a specific timepoint.

> > > >The job of the thread is just to append the records from one table to
> > > >another table.
> > > >That's all.

Other Threads