Solved

TDatabase component generating error in thread

Posted on 2002-04-28
10
221 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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