Multithreaded Insert

Forum related to version 6.5.1 (alpha) and 6.6.x (beta) of ZeosLib's DBOs

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
r2d2
Fresh Boarder
Fresh Boarder
Posts: 6
Joined: 08.02.2008, 09:32

Multithreaded Insert

Post 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!
sandeep_c24
Expert Boarder
Expert Boarder
Posts: 158
Joined: 06.11.2005, 01:43

Post by sandeep_c24 »

You could pool your database connections and then get one connection for each thread from that pool.

Regards

Sandeep
r2d2
Fresh Boarder
Fresh Boarder
Posts: 6
Joined: 08.02.2008, 09:32

Post by r2d2 »

Thanks for the quick answer!

Can u give me a example?

Thx
Andreas
sandeep_c24
Expert Boarder
Expert Boarder
Posts: 158
Joined: 06.11.2005, 01:43

Post 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
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

Isn't it enough to open and close a connection in each thread here?
Image
r2d2
Fresh Boarder
Fresh Boarder
Posts: 6
Joined: 08.02.2008, 09:32

Post by r2d2 »

Hi Sandeep,

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

Thx
Andreas
gto
Zeos Dev Team
Zeos Dev Team
Posts: 278
Joined: 11.11.2005, 18:35
Location: Porto Alegre / Brasil

Post 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.
Use the FU!!!!!IN Google !

gto's Zeos Quick Start Guide

Te Amo Taís!
r2d2
Fresh Boarder
Fresh Boarder
Posts: 6
Joined: 08.02.2008, 09:32

Post 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
sandeep_c24
Expert Boarder
Expert Boarder
Posts: 158
Joined: 06.11.2005, 01:43

Post by sandeep_c24 »

Could you give a bit more details about your app and how you have structured it?

Regards

Sandeep
r2d2
Fresh Boarder
Fresh Boarder
Posts: 6
Joined: 08.02.2008, 09:32

Post 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
r2d2
Fresh Boarder
Fresh Boarder
Posts: 6
Joined: 08.02.2008, 09:32

Post by r2d2 »

Hello,

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

Many thanks for all who contributed here!!!

Andreas
Post Reply