BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Opening a database connection in new thread.

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Sean Veale
Guest





PostPosted: Mon Jan 26, 2004 10:07 pm    Post subject: Opening a database connection in new thread. Reply with quote



I'm trying to open a database connection from a seperate thread than the
main vcl one but i have problems. Probally I can not do what I am trying to
do but I wanted to ask first.

Thank you in advance.

DB is an TADOConnection compent.

---------code---------------------------------------------------------------
------------------

void __fastcall TOpenDatabaseThread::Execute()
{
FreeOnTerminate = true;
// Check to see if we should terminate the thread.
if(Terminated) return;

// Try to connect to the database.
try
{
TestDatabases->DB->Connected = true; // This throws an exception.

}
catch(...)
{

bUnableToOpenDataBase = true;
ShowMessage("Error opening the database by a thread.");
}
//--------------------------------------------------------------------------
----
// Code that invokes the thread
TOpenDatabaseThread* OpenDataBaseThread;

// Create the thread and suspend it.
OpenDataBaseThread = new TOpenDatabaseThread(true);
if(!OpenDataBaseThread)
{
// We couldn't create the thread for some reason. Warn the user and
proceed.
MessageDlg("Unable to create database thread. There will be na delay
when saving data the first time.", mtWarning, TMsgDlgButtons() << mbOK, 0);
return;
}

OpenDataBaseThread->Priority = tpLowest;
// Start the thread
OpenDataBaseThread->Resume();



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Jan 26, 2004 11:29 pm    Post subject: Re: Opening a database connection in new thread. Reply with quote




"Sean Veale" <Not a [email]Real (AT) Adress (DOT) sorr[/email]y> wrote


Quote:
TestDatabases->DB->Connected = true; // This throws an exception.

What is the actual message of the exception?


Gambit



Back to top
Sean Veale
Guest





PostPosted: Tue Jan 27, 2004 9:27 pm    Post subject: Re: Opening a database connection in new thread. Reply with quote




Quote:

TestDatabases->DB->Connected = true; // This throws an exception.

What is the actual message of the exception?

Sorry I should have posted that. The message is "CoInitialize has not

been called." and it is of type EOleException.

Thanks
Sean



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Jan 27, 2004 9:55 pm    Post subject: Re: Opening a database connection in new thread. Reply with quote


"Sean Veale" <Not a [email]Real (AT) Adress (DOT) sorr[/email]y> wrote


Quote:
Sorry I should have posted that. The message is
"CoInitialize has not been called." and it is of type EOleException.

Well, then there is your answer. ADO is COM-based. Every thread that wants
to access COM needs to initialize the COM library before making any other
COM-related calls. The error is telling you exactly that - your thread did
not initialize the COM library for itself by calling CoInitialize() before
then trying to access the COM system.


Gambit



Back to top
Sean Veale
Guest





PostPosted: Wed Jan 28, 2004 2:50 pm    Post subject: Re: Opening a database connection in new thread. Reply with quote


Quote:

Sorry I should have posted that. The message is
"CoInitialize has not been called." and it is of type EOleException.

Well, then there is your answer. ADO is COM-based. Every thread that
wants
to access COM needs to initialize the COM library before making any other
COM-related calls. The error is telling you exactly that - your thread
did
not initialize the COM library for itself by calling CoInitialize() before
then trying to access the COM system.


Ok, That makes sence to me. Can you give an example of how to call
CoInitialize(). Another question where in the main vcl thread is this done
for you? For example if I have this function in my data module

bool TTestDatabases::Connect()
{

try
{
DB->Connected=true;
if (DB->Connected==true) return true;
else return false;
}
catch (...)
{
ShowMessage("MJTest database alias may not be setup properly, database
use is disabled.");

return false;
}
}

I don't have a problem connecting to the database.

Thank you so far.

Sean



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed Jan 28, 2004 6:58 pm    Post subject: Re: Opening a database connection in new thread. Reply with quote


"Sean Veale" <Not a [email]Real (AT) Adress (DOT) sorr[/email]y> wrote


Quote:
Ok, That makes sence to me. Can you give an example of how
to call CoInitialize().

void __fastcall TOpenDatabaseThread::Execute()
{
CoInitialize(NULL);
try {
//...
}
__finally {
CoUninitialize();
}
}

Or:

void __fastcall TOpenDatabaseThread::Execute()
{
CoInitialize(NULL);
//...
}

void __fastcall TOpenDatabaseThread::DoTerminate()
{
CoUninitialize();
}

Or:

#include <utilcls.h>

void __fastcall TOpenDatabaseThread::Execute()
{
TInitOle ole;
//...
}

Quote:
Another question where in the main vcl thread is this done for you?

There are many places where the VCL may have already called CoInitialize()
for the main thread.


Gambit



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.