Using ODBC Connection Pooling with CDatabase (under MFC)
来源:互联网 发布:东南大学考研分数算法 编辑:程序博客网 时间:2024/04/30 20:09
Rating: none
James R. Twine () August 3, 1999 |
Environment: Visual C++ 6 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... My solution to this problem was to derive a class from CDatabase, and overide the OpenEx(...) method, which Open(...) calls, 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:
(continued) 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 calls before you create your first CDatabase object (I put the following calls into 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 CRecordset 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.
That should 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
- Using ODBC Connection Pooling with CDatabase (under MFC)
- Database Connection Pooling with Tomcat
- Using COM+ object pooling with Delphi 6
- Using COM+ object pooling with Delphi 6
- Connection Pooling
- Using OpenGL with MFC
- Using OpenGL with MFC
- Using OpenCV with MFC
- Using Beyond Compare with Version Control Systems under OSX
- Connection Pooling Proxool
- Connection Pooling Proxool
- HBase Connection Pooling
- Using ActiveX Controls with MFC
- Using Cairo together with MFC
- Using DataAdapters and stored procedures with Sybase and ODBC.NET
- CDataBase
- CDatabase
- 第三方connection pooling 组件
- [002]酸菜鱼
- 陈省身去世了
- Internet Explorer 6 Service Pack 1 企业版的累积安全更新 - Windows XP, Windows 2000 (889669)
- Internet Explorer 6 Service Pack 1 累积安全更新程序 - Windows XP, Windows 2000 (KB889293)
- 设计模式-建造模式(一碗牛肉面的制造过程)
- Using ODBC Connection Pooling with CDatabase (under MFC)
- 04CDatabase类
- 05CRecordset类
- 06CRecordView类
- 学习Enroll例程
- 01数据库、DBMS和SQL
- 02ODBC基本概念
- 03MFC的ODBC类简介
- 08 DAO