多线程的情况下,使用同一个数据库连接,程序会出错。
我觉得一个数据库连接池可以解决这个问题。
因为没有写过这个东西,所以望得到一段代码用于借鉴。
谢谢大家。

解决方案 »

  1.   

    Using ODBC Connection Pooling with CDatabase (under MFC)#if !defined( __CPDATABASE_H_ )
    #define __CPDATABASE_H_#pragma warning (disable : 4127) // conditional expression is constantclass CCPDatabase : public CDatabase
    {
    public:
    /**/ CCPDatabase()
    {
    };
    //
    // Code Is Copied From CDatabase Source Code...
    //
    virtual BOOL OpenEx( LPCTSTR lpszConnectString, DWORD dwOptions = 0 )
    {
    dwOptions |= noOdbcDialog; // Force NoDialog Flag For Connection Pooling ASSERT_VALID(this);
    ASSERT(lpszConnectString == NULL || AfxIsValidString(lpszConnectString));
    ASSERT(!(dwOptions & noOdbcDialog && dwOptions & forceOdbcDialog)); // Exclusive access not supported.
    ASSERT(!(dwOptions & openExclusive)); m_bUpdatable = !(dwOptions & openReadOnly); TRY
    {
    m_strConnect = lpszConnectString; // Allocate the HDBC and make connection
    AllocConnect(dwOptions);
    if(!Connect(dwOptions))
    return FALSE; // Verify support for required functionality and cache info
    VerifyConnect();
    GetConnectInfo();
    }
    CATCH_ALL(e)
    {
    Free();
    THROW_LAST();
    }
    END_CATCH_ALL return TRUE;
    }
    };
    #pragma warning (disable : 4127) // conditional expression is constant
    #endif // __CPDATABASE_H_
      

  2.   

    Connection Pool in a Static Library