Solved

TDatabase component generating error in thread

Posted on 2002-04-28
10
218 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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