Solved

Concurent insert into database

Posted on 2012-03-20
2
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 27

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 38

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

Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Suggested Courses

632 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