Threaded Insert Records

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
bernsoft
Posts: 2
Joined: Thu 29 May 2008 17:44

Threaded Insert Records

Post by bernsoft » Thu 29 May 2008 17:55

I have a threaded application that i wrote a while back in Delphi and ADO. I purchased a copy of mydac std - latest. I would like to port this application to Delphi/mysql (am running sql 5).

The application processes many records and creates a thread for each record to insert details in table i.e its same as running a loop 1000 times and each time you create a thread to insert records. Below find the code that actually does the insert into my database.

In ADO, i have to use coinitialize.... and then connect to the db, make my insert and thats it.

How do i achieve this same thing when my application using mysql - do i use same connection for each thread? Please give me a code snippet that i can use quickly.

Also, anyone with a better way to do what i am trying to do below

My Code:

procedure Tinsert.Execute;
begin
{ Place thread code here }
Coinitialize(nil);

try
with TADOCOmmand.Create(nil) do
begin
//frmmain.cxMemo1.Lines.Add('Adding sms');
ConnectionString:=SQLSERVER CONNECTION STRING HERE; /
CommandText:='addnewrecord '+
QuotedStr(f_Destination)+','+
inttostr(f_DestTON)+','+
inttostr(f_DestNPI)+','+
QuotedStr(f_Originator)+','+
inttostr(f_OrigTON)+','+
inttostr(f_OrigNPI)+','+
QuotedStr(f_SMText)+','+
QuotedStr(GetEnumName(TypeInfo(TtcEncodingType), integer(f_Encoding)))+','+
QuotedStr(f_UserDataHeader)+','+
QuotedStr(f_OptionalParameters);
Execute;
Free;
end;
except

on E:Exception do
begin
frmmain.cxMemo1.Lines.Add('Database Error: ' + E.Message);
end;

end;
CoUninitialize;
end;

jkuiper_them
Posts: 28
Joined: Thu 20 Dec 2007 14:48

Post by jkuiper_them » Mon 02 Jun 2008 12:55

I just read in the FAQ that MyDAC is threadsafe. The only thing you have to do is create a connection to the database for every thread.

bernsoft
Posts: 2
Joined: Thu 29 May 2008 17:44

Threaded Insert Records

Post by bernsoft » Mon 02 Jun 2008 13:09

Thank you for your reply.

I have connectivity concerns (e.g if i have 5000 threads...does that mean 500 0 connections.... what does that do to performance.

I will try your method.

I am dissappointed with CRLAB that as a licensed customer, i have been waiting for so long to get a response from them - i also sent this to their sales and support email!

I expect better....for licensed customers.

jkuiper_them
Posts: 28
Joined: Thu 20 Dec 2007 14:48

Re: Threaded Insert Records

Post by jkuiper_them » Mon 02 Jun 2008 13:21

Thank you for your reply.

I have connectivity concerns (e.g if i have 5000 threads...does that mean 500 0 connections.... what does that do to performance.
How do you manage that? It's a lot. A thread is a process witch is closed after the execution is terminated. The connection will be closed also. Don't you mean connections per desktop /pc?
I am dissappointed with CRLAB that as a licensed customer, i have been waiting for so long to get a response from them - i also sent this to their sales and support email!

I expect better....for licensed customers.
You're right about that. The support should response mutch better by email. Did you put your account /license number with it?

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Mon 02 Jun 2008 14:46

In MyDAC for each thread you should create separate TMyConnection object. To execute a command you can use the ExecSQL or ExecSQLEx method of TMyConnection. Then destroy the created TMyConnection object. In this case each command will be executed in the separate connection (one connection for one command).

If you want to reuse connections, you should enable pooling. In this case connections are put into pool on disconnect. If you need a new connection, it will be taken back from pool. Please see the following articles in MyDAC help for more information about pooling: "Using connection pooling", "TCustomDAConnection.Pooling", "TCustomDAConnection.PoolingOptions".

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Tue 03 Jun 2008 09:20

You can noticeably increase the performance of your application if you don't create a thread for inserting each record. Only call the TMyCommand.Execute method in a loop.
Also you can use the TMyLoader component which serves for fast loading of data to the server.

Post Reply