Page 1 of 1

Multithreaded Insert

Posted: 08.02.2008, 09:37
by r2d2
Hello,

I want to change my application to run multithreaded.

Right now I am using zeos and firebird. It inserts data sets in
a sequential way into database.

In general this could be done in parallel. But I wonder if zeos supports
this?

Sequence:
Insert;
dataset.fieldbyname('xxx')=xxx;
post;

How can this be done threadsafe?

Thanks!

Posted: 08.02.2008, 10:26
by sandeep_c24
You could pool your database connections and then get one connection for each thread from that pool.

Regards

Sandeep

Posted: 08.02.2008, 10:39
by r2d2
Thanks for the quick answer!

Can u give me a example?

Thx
Andreas

Posted: 08.02.2008, 10:57
by sandeep_c24
Hi Andreas

You will have to declare a function that takes care of managing and returning a connection TO and FROM the Pool. There should be some docs on the web on this topic.

So in your thread you could do something like

lZCon := GetConFromPool;
try
// Assign connection to all your controls that connect to database.
finally
ReturnConToPool(lZCon);
end;

This is the basic structure that I have used in my apps and has worked fine for me.

Hope that helps.

Regards

Sandeep

Posted: 08.02.2008, 11:36
by mdaems
Isn't it enough to open and close a connection in each thread here?

Posted: 08.02.2008, 12:17
by r2d2
Hi Sandeep,

would it also work to do a connection.create() and a
freeandnil(connection) in each thread?

Thx
Andreas

Posted: 08.02.2008, 12:42
by gto
r2d2 wrote:Hi Sandeep,

would it also work to do a connection.create() and a
freeandnil(connection) in each thread?

Thx
Andreas
It would do the trick, but with a connection pool you'll have some persistent connections available, loosing the weight of connection and disconnecting in all threads.

Well, you get the idea.

Posted: 09.02.2008, 11:31
by r2d2
Hi,

That worked perfect! Thx!

But now my dbgrid from the main app doesnt update. It uses
a different connection.

Any idea how to get this updated?

Thx
Andreas

Posted: 09.02.2008, 12:12
by sandeep_c24
Could you give a bit more details about your app and how you have structured it?

Regards

Sandeep

Posted: 09.02.2008, 12:32
by r2d2
The app has the following db design:

Main: TZCon1->TZQuery1->TDataSource->DBGrid

it issues threads with the following structure

Threadn: TZCon2->TZQuery2->insert/update/...

While running the threads work but Main doesnt update.
After close and restart of Main I get all records displayed in DBGrid.

Is this a buffer problem? Or invalidate/update problem?

Regards
Andreas

Posted: 09.02.2008, 16:36
by r2d2
Hello,

I got it. The TransactIsolationLevel of ZCon was not set properly.

Many thanks for all who contributed here!!!

Andreas