Using ODBC Connection pooling with cdatabase (under MFC)

Source: Internet
Author: User
Tags odbc connection
Document directory
  • Downloads
Using ODBC Connection pooling with cdatabase (under MFC)
Rating:None

James R. twine()
August 3, 1999.

Environment:Visual c ++ 6

This article shows how you can use ODBC Connection pooling with MFC's cdatabase and crecordset objects. it will explain the way in which I did it, which I am sure is not the only way it can be done. also, I am no database expert, so I cannot explain all of the internal details of what is going on.
(Continued)

Note that in order for this to work correctly (the way I did it), you are going to have to derive a class from cdatabase, and override the openex (...) method. more details on this follow...

Brief overview

Connection pooling is a feature of the (some ?) ODBC drivers that holds onto connections to a database server behind-the-scenes. when a connection is requested, and the driver already is holding on to one, it hands over the connection it already has. this prevents constant round-trips to the server to create and tear down connections.

The good news

Connection pooling can speed up your database-related operations. You will see a marked increase in speed if you are constantly constructing and destructing crecordset-derived objects and running queries with them.

How to do it

To enable Connection pooling, you need to make the following callbefore you create your first cdatabase object (I put the following callinto cwinapp: initinstance (), and the m_shsqlenv data member a member of my cwinapp-derived class ). this tells the ODBC driver to set the global (process-level) ODBC environment to use Connection Pooling (if available), and to maintain one connection per driver (In production code, you will check the return values, of course !) :

// //   m_shSQLEnv Is A SQLHANDLE Member That Must Be Freed When Your//   Application Terminated// SQLRETURN srRetCode = 0;srRetCode = SQLSetEnvAttr( NULL, SQL_ATTR_CONNECTION_POOLING,         (SQLPOINTER)SQL_CP_ONE_PER_DRIVER, 0 );                  // Enable Connection PoolingsrRetCode = SQLAllocHandle( SQL_HANDLE_ENV, NULL, &m_shSQLEnv ); // Get Global HandleLater on, I free the m_shSQLEnv handle in my CWinApp::ExitInstance() function:if( m_shSQLEnv )                                                 // If Environment Handle For Connection Pooling Obtained{    SQLFreeHandle( SQL_HANDLE_ENV, m_shSQLEnv );                 // Free It}

In order to use this correctly, you need to keep a globally accessable (read: Shared) single instance of a cdatabase object. (actually, this is going to be a cdatabase-derived object, but more on that later .) again, this cdatabase-derived member is a member of my cwinapp-derived class, And I provide an accessor function for it that returns it address. this accessor function will be used by all CREC Ordset objects when they are constructed, so that they do not create their own cdatabase class internally. Note that this is a good idea anyway! Try it and you will see an improvement, even if you do not use Connection Pooling!

The bad news

Now here is the kicker... connection Pooling requires the ODBC version of The cdatabase: noodbcdialog flag. since you do not (have to) handle opening and closing the cdatabase object (that is where Connection Pooling comes in), you are going to find an interesting problem. when crecordset-derived objects call open (...) on the Internal cdatabase object that they have, there is no way (that I have found) to pass over the cdatabase: noodbcdialog flag, which is required when using Connection pooling.

My solution to this problem was to derive a class from cdatabase, and overide the openex (...) method, which open (...) CILS, to always add in the cdatabase: noodbcdialog flag. that solved my problem. you can download the code for the cdatabase-derived class that I used. it is very simple.

Now that you have all this information, here are the steps required to use it:

  • Create (or download) The cdatabase-derived class, and add it to your project
  • Create a member of The cdatabase-derived class in your cwinapp-derived (Application) Class
  • Create a sqlhandle data member in your application class for the enviroment handle
  • Add an accessor function that returns the address of the cdatabase-derived member you added in the previous step
  • Make the necessary sqlsetenvattr (...) and sqlallochandle (...) CILS as specified above
  • Initially open your cdatabase-derived object (if you are using SQL authentication)
  • Whenever you instantiate a crecordset-derived object, pass the address of the cdatabase-derived class into its constructor

That shoshould be all that is required, as that is what I am doing, and (so far) It seems to be working for me.

Downloads

Download source-1 KB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.