Solved

Concurent insert into database

Posted on 2012-03-20
2
337 Views
Last Modified: 2012-03-21
Hello experts!

My application reads data sent through the Internet.

I use IdTCPServer to read the data.

Sometimes it is lots of data and easy approach like:

onExecute:
 with DataModule2.ZQuery1 do
     begin
       Close;
       Params[0].AsInteger := 1;
       Params[1].AsDateTime := Today;
       Params[2].AsString := lineOfData;
       ExecSQL;
       ApplyUpdates;

     end;

works fine but is not stable, after some time it crashes.

What approach would be appropriate here?

Thank you

panJames
0
Comment
Question by:panJames
2 Comments
 
LVL 25

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 37745952
Seem that you have few concurrent request for write to db with globally defined query.

you can two ways:
- create query component each time data comes or

- useing critical sections ... so define global variable like :

var
  LockDB: TRtlCriticalSection;

Open in new window


... on botton of unit before 'end.' add....

...
initialization
  InitializeCriticalSection(LockDB);

finalization
  DeleteCriticalSection(LockDB);

Open in new window


... and on Execute :

...
  EnterCriticalSection(LockDB);
  try
    with DataModule2.ZQuery1 do
     begin
       Close;
       Params[0].AsInteger := 1;
       Params[1].AsDateTime := Today;
       Params[2].AsString := lineOfData;
       ExecSQL;
     end;
  finally
    LeaveCriticalSection(LockDB);
  end;

...

Open in new window

0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 37745956
the approach depends on a some considerations
> what type of database ? oracle, mssql, interbase ?

you are adding 1 line at a time or is this 1 line the whole text file ?
what is this lineofdata ?

for your delphi approach there are several considerations
are you catching all the errors ? and handling them appropriately ?
what do you do when the database is down ? eg for maintenance
what happens if you lose connection ?

for the database > is there space enough, are you archiving/deleting old data ?
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now