BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

dbExpress, threads and timers

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (dbExpress)
View previous topic :: View next topic  
Author Message
Markus Humm
Guest





PostPosted: Sat Sep 09, 2006 8:10 am    Post subject: dbExpress, threads and timers Reply with quote



Hello,

for testing purposes I did the following:

- created a Interbase 7.5 database with one table wich has a ID column
which is auto filled by a trigger with generator and two other columns

- a Delphi VCL program which does the following:

- a SQLDataSets share the same SQLConnection
- on request (button click) it writes single or multiple rows to the
database, all with the same contents, this is done via inserts and a
commit work is sent afterwards (don't have the transaction syntax at
hand here but the rows get actually written)
- there is a timer which looks every second whether there is something
in the database and reads this out, aquires the global critical
section of the application and appends the read text to a string
list. After that the lock is released and the read row is deleted
from the database via the id of the read row. For reading and
deleting different datasets are used. The timer is disabled on start
of onTimer and enabled as last action in the onTimer
- the app. has a seperate thread which continously checks whether
there is something in the string list and if yes locks it, deletes
the 0th entry, frees the lock and increments a counter with public
visibility.
- another timer displays how many items are in the string list and the
current value of the thread's counter.

Now the problem:
in the OnTimer of the reading timer I often get "cursor unknown"
exceptions. Why? Has it to do with thread safety? Can it be that it
can't cope with the writing loop and simultaniously occuring onTimer
events? But the writing, reading and deleting is done via seperate
SQLDataSets which share the SQLConnection. FoFor the connection the
original Borland dbExpress drivers are used.

What causes these exceptions? I run it from the IDE. When the exception
happens I click on "halt" and it doesn't jump to any particular line in
the code, but after pressing F7 I always land in the exception clause of
the OnTimer of that reading timer where the dataset used for reading is
closed.

procedure Tmain.Timer2Timer(Sender: TObject);
begin
Timer2.Enabled:=false;
SQLDataSet2.CommandText:=
'select * from testtable where rechnerid = '+tf_eigeneid.Text;
try
SQLDataSet2.Open;
SQLDataSet2.First;
while SQLDataSet2.Eof = false do
begin
lock.Acquire;
list.Add(SQLDataSet2.FieldByName('text').AsString);
lock.Release;

SQLDataSet3.CommandText:=
'delete from testtable where id = '+
IntToStr(SQLDataSet2.FieldByName('id').AsInteger);
SQLDataSet3.ExecSQL(true);
SQLDataSet3.CommandText:='commit work';
SQLDataSet3.ExecSQL(true);

SQLDataSet2.Next;
end;
SQLDataSet2.Close;
except
SQLDataSet2.Close;
end;
Timer2.Enabled:=true;
end;

On request I can post the whole code (around 180 lines).

Greetings

Markus
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (dbExpress) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.