0

我们有一个 C++ 应用程序,它使用 ODBC 连接到 SQL Server 数据库。它在 CentOS 7 应用服务器上运行。它与 msodbcsql17 版本 17.4.2.1-1 完美配合,但与版本 17.6.1.1-1 不同。你能告诉我为什么吗?以下是相关代码:

#include "DBODBC.h"
#include "ODBCResultSet.h"
#include <sqlext.h>
#include <sqlncli.h>

/* ----------------------------------------------------------------------------- */
/** Creates environment handle and sets ODBC version
*   /returns True if intialization succeeds, false otherwise
*/
/* ----------------------------------------------------------------------------- */
bool DBODBC::Init()
{
    DB::Init();
    if (!Check(SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_Env), m_Env,SQL_HANDLE_ENV ))
    {
        LOG_E("DBODBC::Init - Failed to create environment handle");
        return false;
    };

    if (!Check(SQLSetEnvAttr(m_Env,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,0), m_Env,SQL_HANDLE_ENV))
    {
        LOG_E("DBODBC::Init - Failed to set ODBC version");
        return false;
    };

    return true;
}

/* ----------------------------------------------------------------------------- */
/** Creates a connection to the database based on the parameters passed
*   /param user Null terminated string containing username to be used to connect to the DB
*   /param password Null terminated string containing password to be used to connect to the DB
*   /param DBName Null terminated string containing ODBC datasource name of the DB to connect to
*   /returns True if succeeded to connect to the DB, false otherwise
*/
/* ----------------------------------------------------------------------------- */
bool DBODBC::Connect(const char * user, const char * password, const char * DBName)
{
    LOG_I(2, "DBODBC::Connect - Started");
    
    if (IsConnected()) 
    {
        LOG_I(5, "DBODBC::Connect - DB connection is already up");
        return false;
    }
    //allocate connection handle
    if (SQLAllocHandle(SQL_HANDLE_DBC, m_Env, &m_Conn)==SQL_ERROR) 
    {
        LOG_W("DBODBC::Connect - Failed to allocate connection handle");
        return false;
    }

    SQLSetConnectAttr(m_Conn, SQL_COPT_SS_MARS_ENABLED, (SQLPOINTER)SQL_MARS_ENABLED_YES, SQL_IS_UINTEGER);

    LOG_I(2, "DBODBC::Connect - Calling SQLConnect");

    if (!Check(SQLConnect(m_Conn, (SQLCHAR*)DBName, SQL_NTS,(SQLCHAR*)user,SQL_NTS, (SQLCHAR*)password, SQL_NTS), m_Conn, SQL_HANDLE_DBC))
    {
        LOG_W("DBODBC::Connect - Failed to open connection to datasource \"%s\"", DBName);
        return false;
    }

    LOG_I(5, "DBODBC::Connect - Successfully connected to %s", DBName);
    m_bConnectionOk=true;
    m_bIsConnected=true;
    return true;
}

/* ----------------------------------------------------------------------------- */
/** Checks value returned by ODBC and collects diagnostic info if failed
*/
/* ----------------------------------------------------------------------------- */
bool DBODBC::Check(SQLRETURN RetCode, SQLHANDLE hHandle, SQLSMALLINT  hType)
{
    LOG_I(2, "DBODBC::Check - Started");
    SQLSMALLINT iRec = 0;
    SQLINTEGER  iError;
    SQLCHAR     wszMessage[1000];
    SQLCHAR     wszState[SQL_SQLSTATE_SIZE+1];
    bool        retval;

    switch (RetCode)
    {
    case SQL_SUCCESS_WITH_INFO:
        while (SQLGetDiagRec(hType,
                                hHandle,
                                ++iRec,
                                wszState,
                                &iError,
                                wszMessage,
                                (SQLSMALLINT)(sizeof(wszMessage) / sizeof(WCHAR)),
                                (SQLSMALLINT *)NULL) == SQL_SUCCESS)
        {
            LOG_W("DBODBC::Check - [%5.5s] %s (%d)\n", wszState, wszMessage, iError);
        }
    case SQL_SUCCESS:
        return true;
    case SQL_INVALID_HANDLE:
        LOG_W("DBODBC::Check - SQL handle is invalid");
        return false;
        break;
    case SQL_ERROR:
        while (SQLGetDiagRec(hType,
                                hHandle,
                                ++iRec,
                                wszState,
                                &iError,
                                wszMessage,
                                (SQLSMALLINT)(sizeof(wszMessage) / sizeof(WCHAR)),
                                (SQLSMALLINT *)NULL) == SQL_SUCCESS)
        {
            LOG_W("DBODBC::Check - [%5.5s] %s (%d)\n", wszState, wszMessage, iError);
        }
        return false;
        break;
    case SQL_NO_DATA:
        //No data found, no need to log it, just return true;
        return true;
        break;
    default:
        LOG_W("DBODBC::Check - Unknown result %d", RetCode);
        return false;
    }
}

在日志中,我可以看到它挂在 SQLConnect 方法调用上:

2073163264 - INFO (2) - [2020.12.11 15:13:10.728] - DBODBC::Check - Started
2073163264 - INFO (2) - [2020.12.11 15:13:10.728] - DBODBC::Check - Started
2073163264 - INFO (2) - [2020.12.11 15:13:10.729] - DBODBC::Init - Done
2073163264 - INFO (2) - [2020.12.11 15:13:10.729] - DBODBC::Connect - Started
2073163264 - INFO (2) - [2020.12.11 15:13:10.729] - DBODBC::Connect - Calling SQLConnect

之后没有更多的日志,甚至 Check 方法中的“Started”消息也没有被记录。根据 msodbcsql 文档(https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlconnect-function?view=sql-server-ver15),我们正确使用了该库。此外,我在发行说明中找不到任何表明某些内容已更改的详细信息,我也应该升级我的应用程序。

编辑:

我删除了当前版本的 msodbcsql17 并安装了早期版本,甚至尝试了多个版本,它根本不起作用。我们有两台服务器,其中一台仍在 msodbcsql17 的 17.4.2.1-1 版本上,应用程序运行良好。其他服务器默认安装了新的 17.6.1.1-1 版本,应用程序无法运行,降级到 17.4.2.1-1 版本后无法运行。

4

1 回答 1

0

好吧,看来我错过了我以某种方式进行的检查之一:odbcinst.ini 实际上已更改!在 17.6 msodbc 安装期间,删除了一行。再次添加后,它立即开始工作:

在此处输入图像描述

为什么删除这条线,我不知道。或者,在全新安装的情况下,该行从未添加到 odbcinst.ini。无论如何,它现在有效。

于 2020-12-16T19:11:18.937 回答