Tuesday, February 14, 2012

dbinit(), dblogin(), how often?

I'm a MS SQL newbie and am programming SQL using MS DS C++ 2003.

I'm writing sql code that will reside in a shared dll, used by many
processes and many threads in those processes.

So how often do I need to call dbinit()? Only the first time the DLL is
loaded, once per new process, once per thread, or once per database open?

Same question for dblogin().

Thanks very much for any help.
Bruce.Bruce. (noone@.nowhere.com) writes:

Quote:

Originally Posted by

I'm a MS SQL newbie and am programming SQL using MS DS C++ 2003.
>
I'm writing sql code that will reside in a shared dll, used by many
processes and many threads in those processes.
>
So how often do I need to call dbinit()? Only the first time the DLL is
loaded, once per new process, once per thread, or once per database open?
>
Same question for dblogin().


Zero times. At least unless you have some very special reason to use
DB-Library at all, like the need to support a legacy application. To wit,
DB-Library is a deprecated client API, and it lacks support for new features
added since SQL7, as Microsoft has not touched it for the last 8-10 years.

The recommended choice for a C++ application are ODBC and OLE DB. Of these
the ODBC is probably a lot easier to work with.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||"Erland Sommarskog" <esquel@.sommarskog.sewrote in message
news:Xns99425A6E7696FYazorman@.127.0.0.1...

Quote:

Originally Posted by

The recommended choice for a C++ application are ODBC and OLE DB. Of these
the ODBC is probably a lot easier to work with.


Not an option in this case but thanks for your reply anyway.

Bruce.|||Bruce. (noone@.nowhere.com) writes:

Quote:

Originally Posted by

"Erland Sommarskog" <esquel@.sommarskog.sewrote in message
news:Xns99425A6E7696FYazorman@.127.0.0.1...

Quote:

Originally Posted by

>The recommended choice for a C++ application are ODBC and OLE DB. Of
>these the ODBC is probably a lot easier to work with.


>
Not an option in this case but thanks for your reply anyway.


I'm sorry I was not able to answer your actual question at the time, but
I did not have access to some old source code that I have. Having looked
at that one, I see that I have this:

// Init DB-Library if we are the first player.
EnterCriticalSection(&CS);
if (no_of_threads++ == 0) {
if(dbinit() == FAIL) {
croak("Can't initialize dblibrary...");
}
// Set up the error handlers once for all.
dberrhandle(err_handler);
dbmsghandle(msg_handler);
}
LeaveCriticalSection(&CS);

// Set up LOGINREC struct for this thread.
td->login = dblogin();
DBSETLUSER(td->login, NULL);
DBSETLPWD(td->login, NULL);
DBSETLHOST(td->login, getenv("COMPUTERNAME"));

That is, call dbinit() when the DLL is initiated, but call dblogin once
for each thread. Then again, I guess the reason I did it this way was
to permit different threads to use the different login information. If
all threads will use the same login details, I can't see anything else
than that it would be sufficient to call dblogin() once, since LOGINREC
appears to only hold static data.

But permit me again to point the unsuitable in using DB-Library for new
development. Or to be more blunt: it's sheer silliness. If nothing else,
it's a waste of time for your professional development. The likelyhood
that you will get the oppurtunity to reuse the knowledge of DB-Library
programming are slim, whereas learning to master the ODBC API can be very
useful.

Why would ODBC or OLE DB not be an option in your case?

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||"Erland Sommarskog" <esquel@.sommarskog.sewrote in message
news:Xns99436DC7B5A82Yazorman@.127.0.0.1...

Quote:

Originally Posted by

That is, call dbinit() when the DLL is initiated, but call dblogin once
for each thread. Then again, I guess the reason I did it this way was
to permit different threads to use the different login information. If
all threads will use the same login details, I can't see anything else
than that it would be sufficient to call dblogin() once, since LOGINREC
appears to only hold static data.


That's very interesting and helpful. Thanks for the information.

Bruce.

No comments:

Post a Comment