?
Solved

TDatabase component generating error in thread

Posted on 2002-04-28
10
Medium Priority
?
227 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
[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
  • 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 400 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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