Board index » delphi » TDatabase

TDatabase

I'm creating an application that receive DDE feed from DDE Server and
post the feed to database. Each time my application receives the feed,
it creates a new thread to connect and post the data to SQL Server.

The problem is, my application often give me GPF and after I try to
locate the problems, I found that my application crash after try to open
the connection. Here is the code for opening the connection. Can any
body help me? I'm using Delphi 4 with SQL 6.5

Regards
Budi Boenbtaran

  try
     inherited Create(bSuspended);
     FreeOnTerminate := True;
     bDbOn := false;
     i := 0;

     {Create Session. Use new session for each thread}
     FSession := TSession.Create(nil);

     {Name Session with ThreadID, it's unique}
     FSession.SessionName:=IntToStr(ThreadID);
     FSession.Active:=True;

     {Create Database}
     FDatabase:=TDatabase.Create(nil);

     {Create Query}
     FQuery := TQuery.Create(nil);

     while (not bDbOn) and (i <= MAX_TRY) do
     begin
          try
             FDatabase.SessionName:=FSession.SessionName;
             FDatabase.AliasName:=myparam.sODBCAlias;

FDatabase.DatabaseName:=myparam.sODBCAlias+IntToStr(ThreadId);
             FDatabase.Params.Values['USER NAME'] := myparam.sDBUserID;
             FDatabase.Params.Values['PASSWORD'] := myparam.sDBPassword;

             FDatabase.LoginPrompt := false;
             FDatabase.Open; // This is the command that creates GPF

             FQuery.SessionName := FSession.SessionName;
             FQuery.DatabaseName := FDatabase.DatabaseName;
             bDbOn := True;
          except
             bDbOn := False;
             sleep(1000);
          end;
          inc(i);
     end;
     if i = MAX_TRY then
     begin
          Terminate;
     end;
     resume;
  Except
      Terminate;
  End;

 

Re:TDatabase


Budi Boentaran wrote

Quote
>I'm creating an application that receive DDE feed from DDE Server and
>post the feed to database. Each time my application receives the feed,
>it creates a new thread to connect and post the data to SQL Server.

>The problem is, my application often give me GPF and after I try to
>locate the problems, I found that my application crash after try to open
>the connection. Here is the code for opening the connection. Can any
>body help me? I'm using Delphi 4 with SQL 6.5

In case when I use new threads with database connections, I use the
TDatabase object, created in the main application thread (the only TDatabase
for the all threads). So, code in the thread looks like

TQuery *qryKeys=new TQuery(NULL);
 qryKeys->DatabaseName="DB";
 qryKeys->SQL->Add("select * from TABLE");
 qryKeys->Open();

It works fine.

Re:TDatabase


So it means that I have to open connection all the time?
Quote
Sergey Shapovalov wrote:
> Budi Boentaran wrote
> >I'm creating an application that receive DDE feed from DDE Server and
> >post the feed to database. Each time my application receives the feed,
> >it creates a new thread to connect and post the data to SQL Server.

> >The problem is, my application often give me GPF and after I try to
> >locate the problems, I found that my application crash after try to open
> >the connection. Here is the code for opening the connection. Can any
> >body help me? I'm using Delphi 4 with SQL 6.5

> In case when I use new threads with database connections, I use the
> TDatabase object, created in the main application thread (the only TDatabase
> for the all threads). So, code in the thread looks like

> TQuery *qryKeys=new TQuery(NULL);
>  qryKeys->DatabaseName="DB";
>  qryKeys->SQL->Add("select * from TABLE");
>  qryKeys->Open();

> It works fine.

Re:TDatabase


Budi Boentaran wrote

Quote
>So it means that I have to open connection all the time?

Yes, but I think that persistent connection to network server is much more
faster than restoring the connection if it needed.
It's a ethernal problem - speed or resources.

Re:TDatabase


Using the same TDatabase across multiple threads can be a problem.  For
example, what happens if multiple threads simultaneously attempt to execute
a query?

One thing I noticed from your thread constructor:  If bSuspended is True,
the call to inherited could immediately start your Execute method, before
the remaining code in the constructor has executed.  Since you have a Resume
at the bottom of the constructor, you should always call inherited Create
with CreateSuspended set to True.

Re:TDatabase


Russell L. Smith wrote

Quote
>Using the same TDatabase across multiple threads can be a problem.  For
>example, what happens if multiple threads simultaneously attempt to execute
>a query?

Yes it's really seems to cause a problems. But it really works. I can
simultaneosly insert a data into a table in one thread and select a data
from just the same table in another one. I have no any problems on it,
although in Delphi Help sais that a new TSession should be created for each
thread.

Re:TDatabase


Perhaps the SQL Server network client (ODBC driver?) is smart enough when
used with a blocking connection to pend requests for services that occur
while a prior request is being processed.  I don't know enough about the
low-level interaction here.

V/R
Russell L. Smith

Quote
Sergey Shapovalov wrote in message <71uifp$h...@forums.borland.com>...
>Yes it's really seems to cause a problems. But it really works. I can
>simultaneosly insert a data into a table in one thread and select a data
>from just the same table in another one. I have no any problems on it,
>although in Delphi Help sais that a new TSession should be created for each
>thread.

Re:TDatabase


Dear Sergey,

I've tried your suggestion to use the same database for all threads, but as
the result my program open too many connections.
The amount of thread created and the thread destroyed is not balanced. So in
less then one minutes my program already have more than 600 threads.

Regards.
Budi

Quote
Sergey Shapovalov wrote in message <71uifp$h...@forums.borland.com>...

>Russell L. Smith wrote
>>Using the same TDatabase across multiple threads can be a problem.  For
>>example, what happens if multiple threads simultaneously attempt to
execute
>>a query?
>Yes it's really seems to cause a problems. But it really works. I can
>simultaneosly insert a data into a table in one thread and select a data
>from just the same table in another one. I have no any problems on it,
>although in Delphi Help sais that a new TSession should be created for each
>thread.

Other Threads