Solved

TDatabase component generating error in thread

Posted on 2002-04-28
10
222 Views
Last Modified: 2010-04-04
!!!!!!!!!!!!!!!!HELP!!!!!!!!!!!!!!!!!!!!!!!!!

Now I have your attention :)

I've got a thread which runs simultaneously with my main app.  The thread processes jobs which are passed to it.  I've got a REAL problem tho, and I can't figure out what I'm doing wrong.

I've been told that each thread requires it's own TDatabase.  No worries, I'll stick on in.  Here's what I do:

constructor TReportThread.CreateThread(CreateSuspended: Boolean; Alias, Params: String);
begin
  Inherited Create(CreateSuspended);

  FreeOnTerminate := true;

  ADatabase := TDatabase.Create(nil);
  ASession := TSession.Create(nil);
  ASession.AutoSessionName := true;
  ASession.Active := true;
  ADatabase.SessionName := ASession.SessionName;
  ADatabase.AliasName := Alias;
  ADatabase.DatabaseName := 'PMREPTHREADDM';
  ADatabase.Params.Text := Params;
  ADatabase.LoginPrompt := false;
  ADatabase.Open;
end;

When the thread is created, I can trace through the code until the database is opened.

ADatabase and ASession are declared in the Private section of the class and are freed when the thread is terminated.

Any help would be appreciated.

Ta!

Stu
0
Comment
Question by:SJohnson
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 1

Author Comment

by:SJohnson
ID: 6976328
Oops!  Forgot to add to this that the error I'm getting is C0000008
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6976855
Hi SJohnson,

what is the value for CreateSuspended passed as parameter?
If your thread not suspended then execute started immediate after constructor called. In this case databse can be not initialized before some action in execute method will refer to it. Better would be to move database and session initialization code inside execute method.

------
Igor.

0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6976998
Hi Igor,

I create the thread unsuspended.  And then after the constructor, change it to suspend = true.  Is this OK, or should I started it suspened?

Thanks for the info.

Stu
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:adeng
ID: 6978010
I think each thread requires it's own TSession and try to make ASession.AutoSessionName := False and create your own session name for each thread.

Regards
Adeng
0
 
LVL 1

Author Comment

by:SJohnson
ID: 6978904
Hi Adeng,

From the Delphi 5 help: "Set AutoSessionName to control whether or not a unique session name is automatically generated. AutoSessionName is intended to guarantee developers of multi-threaded applications that sessions spawned for each thread are unique at runtime. "

Cheers!

Stu
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6979849
Hi Stuart,

did you try to move initializtion code to execute method? The deal is that Create method executed in main VCL thread.

-----
Igor.

PS: it's not good idea to use two logins :-)
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6979905
Hi Igor,

No it didn't work :(  Everything goes OK until it reaches the Datebase.Open line and then the execute method is exitted with no exception.  If I do this:

try
  ADatabase.Open;
except
  on E: Exception do
    ShowMessage(E.Message)
end;

no error is displayed.  The application stops responding.

What do you mean by it's not a good idea to use two logins? Do you mean two different login IDs?  If so, I'm only using the one that's used to log into the actual application.

Thanks for you help,

Stu.
0
 
LVL 9

Accepted Solution

by:
ITugay earned 100 total points
ID: 6982837
Hi Stuart,

I will try to explore my HDD to find examples of DB threading. Seems it is at home. On success, I will public it here.

Two logins:
1. SJohnson
2. Stuart Johnson  
0
 
LVL 1

Author Comment

by:SJohnson
ID: 6998471
Hi Igor,

I've done this the only way I know how and the only way which works.  I use the TDatabase component from the main application.  So far I've had absolutely no problems what so ever.  I know this probably isn't right, but I've had no other choice as I had to get this part of the app working.  If you can tell me how it works, I'll be more than happy to change what I've done.

As for the two logins, it's cool for me to have the two of them.  See here: http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20148878

Cheers,

Stu
0
 
LVL 1

Author Comment

by:SJohnson
ID: 7168623
Hi Igor,

Well, I guess we never got any further on this one.  I'm just going to giv eyou the points so that it's cleared out.

Thanks for you help with the question though!

Cheers,

Stu.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
code issue 8 186
Delphi - replicating a form 8 93
Twebbrowser add css to the header 3 39
MS Access from Delphi 31 81
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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