passing connection loss indicator to DB event handler; minor Oracle driver refactoring
authorVictor Kirhenshtein <victor@netxms.org>
Tue, 13 Oct 2015 07:21:07 +0000 (10:21 +0300)
committerVictor Kirhenshtein <victor@netxms.org>
Tue, 13 Oct 2015 07:21:07 +0000 (10:21 +0300)
include/nxdbapi.h
src/db/dbdrv/oracle/oracle.cpp
src/db/dbdrv/oracle/oracledrv.h
src/db/libnxdb/drivers.cpp
src/db/libnxdb/libnxdb.h
src/db/libnxdb/session.cpp
src/server/core/main.cpp

index 8dd8eab..fe014ef 100644 (file)
@@ -103,7 +103,7 @@ struct PoolConnectionInfo
  */
 bool LIBNXDB_EXPORTABLE DBInit(DWORD logMsgCode, DWORD sqlErrorMsgCode);
 DB_DRIVER LIBNXDB_EXPORTABLE DBLoadDriver(const TCHAR *module, const TCHAR *initParameters,
-                                                                                                               bool dumpSQL, void (* fpEventHandler)(DWORD, const WCHAR *, const WCHAR *, void *),
+                                                                                                               bool dumpSQL, void (* fpEventHandler)(DWORD, const WCHAR *, const WCHAR *, bool, void *),
                                                                                                                void *userArg);
 void LIBNXDB_EXPORTABLE DBUnloadDriver(DB_DRIVER driver);
 const char LIBNXDB_EXPORTABLE *DBGetDriverName(DB_DRIVER driver);
index 3e5c612..eced489 100644 (file)
@@ -117,17 +117,15 @@ extern "C" void EXPORT DrvUnload()
 /**
  * Get error text from error handle
  */
-static void GetErrorTextFromHandle(OCIError *handle, WCHAR *errorText)
+static void GetErrorFromHandle(OCIError *handle, sb4 *errorCode, WCHAR *errorText)
 {
-       sb4 nCode;
-
 #if UNICODE_UCS2
-       OCIErrorGet(handle, 1, NULL, &nCode, (text *)errorText, DBDRV_MAX_ERROR_TEXT, OCI_HTYPE_ERROR);
+       OCIErrorGet(handle, 1, NULL, errorCode, (text *)errorText, DBDRV_MAX_ERROR_TEXT, OCI_HTYPE_ERROR);
        errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
 #else
        UCS2CHAR buffer[DBDRV_MAX_ERROR_TEXT];
 
-       OCIErrorGet(handle, 1, NULL, &nCode, (text *)buffer, DBDRV_MAX_ERROR_TEXT, OCI_HTYPE_ERROR);
+       OCIErrorGet(handle, 1, NULL, errorCode, (text *)buffer, DBDRV_MAX_ERROR_TEXT, OCI_HTYPE_ERROR);
        buffer[DBDRV_MAX_ERROR_TEXT - 1] = 0;
        ucs2_to_ucs4(buffer, ucs2_strlen(buffer) + 1, errorText, DBDRV_MAX_ERROR_TEXT);
        errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
@@ -138,20 +136,18 @@ static void GetErrorTextFromHandle(OCIError *handle, WCHAR *errorText)
 /**
  * Set last error text
  */
-static void SetLastErrorText(ORACLE_CONN *pConn)
+static void SetLastError(ORACLE_CONN *pConn)
 {
-       GetErrorTextFromHandle(pConn->handleError, pConn->szLastError);
+       GetErrorFromHandle(pConn->handleError, &pConn->lastErrorCode, pConn->lastErrorText);
 }
 
 /**
  * Check if last error was caused by lost connection to server
  */
-static DWORD IsConnectionError(ORACLE_CONN *pConn)
+static DWORD IsConnectionError(ORACLE_CONN *conn)
 {
        ub4 nStatus = 0;
-
-       OCIAttrGet(pConn->handleServer, OCI_HTYPE_SERVER, &nStatus,
-                  NULL, OCI_ATTR_SERVER_STATUS, pConn->handleError);
+       OCIAttrGet(conn->handleServer, OCI_HTYPE_SERVER, &nStatus, NULL, OCI_ATTR_SERVER_STATUS, conn->handleError);
        return (nStatus == OCI_SERVER_NOT_CONNECTED) ? DBERR_CONNECTION_LOST : DBERR_OTHER_ERROR;
 }
 
@@ -221,7 +217,8 @@ extern "C" DBDRV_CONNECTION EXPORT DrvConnect(const char *host, const char *logi
                                                   OCI_ATTR_SESSION, pConn->handleError);
                                        pConn->mutexQueryLock = MutexCreate();
                                        pConn->nTransLevel = 0;
-                                       pConn->szLastError[0] = 0;
+                                       pConn->lastErrorCode = 0;
+                                       pConn->lastErrorText[0] = 0;
                pConn->prefetchLimit = 10;
 
                                        if ((schema != NULL) && (schema[0] != 0))
@@ -237,7 +234,7 @@ extern "C" DBDRV_CONNECTION EXPORT DrvConnect(const char *host, const char *logi
                                }
                                else
                                {
-                                       GetErrorTextFromHandle(pConn->handleError, errorText);
+                                       GetErrorFromHandle(pConn->handleError, &pConn->lastErrorCode, errorText);
                                        OCIHandleFree(pConn->handleEnv, OCI_HTYPE_ENV);
                                        free(pConn);
                                        pConn = NULL;
@@ -245,7 +242,7 @@ extern "C" DBDRV_CONNECTION EXPORT DrvConnect(const char *host, const char *logi
                        }
                        else
                        {
-                               GetErrorTextFromHandle(pConn->handleError, errorText);
+                               GetErrorFromHandle(pConn->handleError, &pConn->lastErrorCode, errorText);
                                OCIHandleFree(pConn->handleEnv, OCI_HTYPE_ENV);
                                free(pConn);
                                pConn = NULL;
@@ -392,13 +389,13 @@ extern "C" ORACLE_STATEMENT EXPORT *DrvPrepare(ORACLE_CONN *pConn, WCHAR *pwszQu
        }
        else
        {
-               SetLastErrorText(pConn);
+               SetLastError(pConn);
                *pdwError = IsConnectionError(pConn);
        }
 
        if (errorText != NULL)
        {
-               wcsncpy(errorText, pConn->szLastError, DBDRV_MAX_ERROR_TEXT);
+               wcsncpy(errorText, pConn->lastErrorText, DBDRV_MAX_ERROR_TEXT);
                errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
        }
        MutexUnlock(pConn->mutexQueryLock);
@@ -787,13 +784,13 @@ extern "C" DWORD EXPORT DrvExecute(ORACLE_CONN *pConn, ORACLE_STATEMENT *stmt, W
        }
        else
        {
-               SetLastErrorText(pConn);
+               SetLastError(pConn);
                dwResult = IsConnectionError(pConn);
        }
 
        if (errorText != NULL)
        {
-               wcsncpy(errorText, pConn->szLastError, DBDRV_MAX_ERROR_TEXT);
+               wcsncpy(errorText, pConn->lastErrorText, DBDRV_MAX_ERROR_TEXT);
                errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
        }
        MutexUnlock(pConn->mutexQueryLock);
@@ -850,19 +847,19 @@ extern "C" DWORD EXPORT DrvQuery(ORACLE_CONN *pConn, const WCHAR *pwszQuery, WCH
                }
                else
                {
-                       SetLastErrorText(pConn);
+                       SetLastError(pConn);
                        dwResult = IsConnectionError(pConn);
                }
                OCIStmtRelease(handleStmt, pConn->handleError, NULL, 0, OCI_DEFAULT);
        }
        else
        {
-               SetLastErrorText(pConn);
+               SetLastError(pConn);
                dwResult = IsConnectionError(pConn);
        }
        if (errorText != NULL)
        {
-               wcsncpy(errorText, pConn->szLastError, DBDRV_MAX_ERROR_TEXT);
+               wcsncpy(errorText, pConn->lastErrorText, DBDRV_MAX_ERROR_TEXT);
                errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
        }
        MutexUnlock(pConn->mutexQueryLock);
@@ -939,14 +936,14 @@ static ORACLE_RESULT *ProcessQueryResults(ORACLE_CONN *pConn, OCIStmt *handleStm
             }
             if (nStatus != OCI_SUCCESS)
                           {
-                                  SetLastErrorText(pConn);
+                                  SetLastError(pConn);
                                   *pdwError = IsConnectionError(pConn);
                           }
                                OCIDescriptorFree(handleParam, OCI_DTYPE_PARAM);
                        }
                        else
                        {
-                               SetLastErrorText(pConn);
+                               SetLastError(pConn);
                                *pdwError = IsConnectionError(pConn);
                        }
                }
@@ -965,7 +962,7 @@ static ORACLE_RESULT *ProcessQueryResults(ORACLE_CONN *pConn, OCIStmt *handleStm
                                }
                                if ((nStatus != OCI_SUCCESS) && (nStatus != OCI_SUCCESS_WITH_INFO))
                                {
-                                       SetLastErrorText(pConn);
+                                       SetLastError(pConn);
                                        *pdwError = IsConnectionError(pConn);
                                        break;
                                }
@@ -1061,19 +1058,19 @@ extern "C" DBDRV_RESULT EXPORT DrvSelect(ORACLE_CONN *pConn, WCHAR *pwszQuery, D
                }
                else
                {
-                       SetLastErrorText(pConn);
+                       SetLastError(pConn);
                        *pdwError = IsConnectionError(pConn);
                }
                OCIStmtRelease(handleStmt, pConn->handleError, NULL, 0, OCI_DEFAULT);
        }
        else
        {
-               SetLastErrorText(pConn);
+               SetLastError(pConn);
                *pdwError = IsConnectionError(pConn);
        }
        if (errorText != NULL)
        {
-               wcsncpy(errorText, pConn->szLastError, DBDRV_MAX_ERROR_TEXT);
+               wcsncpy(errorText, pConn->lastErrorText, DBDRV_MAX_ERROR_TEXT);
                errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
        }
        MutexUnlock(pConn->mutexQueryLock);
@@ -1100,13 +1097,13 @@ extern "C" DBDRV_RESULT EXPORT DrvSelectPrepared(ORACLE_CONN *pConn, ORACLE_STAT
        }
        else
        {
-               SetLastErrorText(pConn);
+               SetLastError(pConn);
                *pdwError = IsConnectionError(pConn);
        }
 
        if (errorText != NULL)
        {
-               wcsncpy(errorText, pConn->szLastError, DBDRV_MAX_ERROR_TEXT);
+               wcsncpy(errorText, pConn->lastErrorText, DBDRV_MAX_ERROR_TEXT);
                errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
        }
        MutexUnlock(pConn->mutexQueryLock);
@@ -1273,14 +1270,14 @@ extern "C" DBDRV_ASYNC_RESULT EXPORT DrvAsyncSelect(ORACLE_CONN *pConn, WCHAR *p
                   }
                   else
                                 {
-                                        SetLastErrorText(pConn);
+                                        SetLastError(pConn);
                                         *pdwError = IsConnectionError(pConn);
                                 }
                                      OCIDescriptorFree(handleParam, OCI_DTYPE_PARAM);
                                        }
                                        else
                                        {
-                                               SetLastErrorText(pConn);
+                                               SetLastError(pConn);
                                                *pdwError = IsConnectionError(pConn);
                                        }
                                }
@@ -1288,13 +1285,13 @@ extern "C" DBDRV_ASYNC_RESULT EXPORT DrvAsyncSelect(ORACLE_CONN *pConn, WCHAR *p
                }
                else
                {
-                       SetLastErrorText(pConn);
+                       SetLastError(pConn);
                        *pdwError = IsConnectionError(pConn);
                }
        }
        else
        {
-               SetLastErrorText(pConn);
+               SetLastError(pConn);
                *pdwError = IsConnectionError(pConn);
        }
 
@@ -1312,7 +1309,7 @@ extern "C" DBDRV_ASYNC_RESULT EXPORT DrvAsyncSelect(ORACLE_CONN *pConn, WCHAR *p
        safe_free(pConn->pBuffers);
        if (errorText != NULL)
        {
-               wcsncpy(errorText, pConn->szLastError, DBDRV_MAX_ERROR_TEXT);
+               wcsncpy(errorText, pConn->lastErrorText, DBDRV_MAX_ERROR_TEXT);
                errorText[DBDRV_MAX_ERROR_TEXT - 1] = 0;
        }
        MutexUnlock(pConn->mutexQueryLock);
@@ -1336,7 +1333,7 @@ extern "C" bool EXPORT DrvFetch(ORACLE_CONN *pConn)
        }
        else
        {
-               SetLastErrorText(pConn);
+               SetLastError(pConn);
                success = false;
        }
        return success;
@@ -1496,7 +1493,7 @@ extern "C" DWORD EXPORT DrvCommit(ORACLE_CONN *pConn)
                }
                else
                {
-                       SetLastErrorText(pConn);
+                       SetLastError(pConn);
                        dwResult = IsConnectionError(pConn);
                }
        }
@@ -1528,7 +1525,7 @@ extern "C" DWORD EXPORT DrvRollback(ORACLE_CONN *pConn)
                }
                else
                {
-                       SetLastErrorText(pConn);
+                       SetLastError(pConn);
                        dwResult = IsConnectionError(pConn);
                }
        }
index 7c8dcd3..233e89f 100644 (file)
@@ -66,7 +66,8 @@ struct ORACLE_CONN
        OCIError *handleError;
        MUTEX mutexQueryLock;
        int nTransLevel;
-       WCHAR szLastError[DBDRV_MAX_ERROR_TEXT];
+       sb4 lastErrorCode;
+       WCHAR lastErrorText[DBDRV_MAX_ERROR_TEXT];
        ORACLE_FETCH_BUFFER *pBuffers;
        int nCols;
        char **columnNames;
index b5dd396..16960d4 100644 (file)
@@ -83,7 +83,7 @@ bool LIBNXDB_EXPORTABLE DBInit(DWORD logMsgCode, DWORD sqlErrorMsgCode)
  * @return driver handle on success, NULL on failure
  */
 DB_DRIVER LIBNXDB_EXPORTABLE DBLoadDriver(const TCHAR *module, const TCHAR *initParameters,
-                                                                                                               bool dumpSQL, void (* fpEventHandler)(DWORD, const WCHAR *, const WCHAR *, void *),
+                                                                                                               bool dumpSQL, void (* fpEventHandler)(DWORD, const WCHAR *, const WCHAR *, bool, void *),
                                                                                                                void *userArg)
 {
    static DWORD dwVersionZero = 0;
index c976822..1fe2f3b 100644 (file)
@@ -73,7 +73,7 @@ struct db_driver_t
        DWORD (* m_fpDrvCommit)(DBDRV_CONNECTION);
        DWORD (* m_fpDrvRollback)(DBDRV_CONNECTION);
        void (* m_fpDrvUnload)();
-       void (* m_fpEventHandler)(DWORD, const WCHAR *, const WCHAR *, void *);
+       void (* m_fpEventHandler)(DWORD, const WCHAR *, const WCHAR *, bool, void *);
        int (* m_fpDrvGetColumnCount)(DBDRV_RESULT);
        const char* (* m_fpDrvGetColumnName)(DBDRV_RESULT, int);
        int (* m_fpDrvGetColumnCountAsync)(DBDRV_ASYNC_RESULT);
index b4882a2..792f38b 100644 (file)
@@ -183,7 +183,7 @@ static void DBReconnect(DB_HANDLE hConn)
       {
                        MutexLock(hConn->m_driver->m_mutexReconnect);
          if ((hConn->m_driver->m_reconnect == 0) && (hConn->m_driver->m_fpEventHandler != NULL))
-                               hConn->m_driver->m_fpEventHandler(DBEVENT_CONNECTION_LOST, NULL, NULL, hConn->m_driver->m_userArg);
+                               hConn->m_driver->m_fpEventHandler(DBEVENT_CONNECTION_LOST, NULL, NULL, true, hConn->m_driver->m_userArg);
          hConn->m_driver->m_reconnect++;
          MutexUnlock(hConn->m_driver->m_mutexReconnect);
       }
@@ -194,7 +194,7 @@ static void DBReconnect(DB_HANDLE hConn)
       MutexLock(hConn->m_driver->m_mutexReconnect);
       hConn->m_driver->m_reconnect--;
       if ((hConn->m_driver->m_reconnect == 0) && (hConn->m_driver->m_fpEventHandler != NULL))
-                       hConn->m_driver->m_fpEventHandler(DBEVENT_CONNECTION_RESTORED, NULL, NULL, hConn->m_driver->m_userArg);
+                       hConn->m_driver->m_fpEventHandler(DBEVENT_CONNECTION_RESTORED, NULL, NULL, false, hConn->m_driver->m_userArg);
       MutexUnlock(hConn->m_driver->m_mutexReconnect);
    }
 }
@@ -271,7 +271,7 @@ bool LIBNXDB_EXPORTABLE DBQueryEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *
                if (hConn->m_driver->m_logSqlErrors)
                        nxlog_write(g_sqlErrorMsgCode, EVENTLOG_ERROR_TYPE, "ss", szQuery, errorText);
                if (hConn->m_driver->m_fpEventHandler != NULL)
-                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, pwszQuery, wcErrorText, hConn->m_driver->m_userArg);
+                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, pwszQuery, wcErrorText, dwResult == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
        }
 
 #ifndef UNICODE
@@ -336,7 +336,7 @@ DB_RESULT LIBNXDB_EXPORTABLE DBSelectEx(DB_HANDLE hConn, const TCHAR *szQuery, T
                if (hConn->m_driver->m_logSqlErrors)
                        nxlog_write(g_sqlErrorMsgCode, EVENTLOG_ERROR_TYPE, "ss", szQuery, errorText);
                if (hConn->m_driver->m_fpEventHandler != NULL)
-                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, pwszQuery, wcErrorText, hConn->m_driver->m_userArg);
+                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, pwszQuery, wcErrorText, dwError == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
        }
 
 #ifndef UNICODE
@@ -800,7 +800,7 @@ DB_ASYNC_RESULT LIBNXDB_EXPORTABLE DBAsyncSelectEx(DB_HANDLE hConn, const TCHAR
                if (hConn->m_driver->m_logSqlErrors)
         nxlog_write(g_sqlErrorMsgCode, EVENTLOG_ERROR_TYPE, "ss", szQuery, errorText);
                if (hConn->m_driver->m_fpEventHandler != NULL)
-                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, pwszQuery, wcErrorText, hConn->m_driver->m_userArg);
+                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, pwszQuery, wcErrorText, dwError == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
    }
 
 #ifndef UNICODE
@@ -1069,7 +1069,7 @@ DB_STATEMENT LIBNXDB_EXPORTABLE DBPrepareEx(DB_HANDLE hConn, const TCHAR *query,
                if (hConn->m_driver->m_logSqlErrors)
         nxlog_write(g_sqlErrorMsgCode, EVENTLOG_ERROR_TYPE, "ss", query, errorText);
                if (hConn->m_driver->m_fpEventHandler != NULL)
-                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, pwszQuery, wcErrorText, hConn->m_driver->m_userArg);
+                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, pwszQuery, wcErrorText, errorCode == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
        }
 
    if (hConn->m_driver->m_dumpSql)
@@ -1219,7 +1219,7 @@ void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const T
 }
 
 /**
- * Bind string parameter with length valiadtion
+ * Bind string parameter with length validation
  */
 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const TCHAR *value, int allocType, int maxLen)
 {
@@ -1355,10 +1355,10 @@ bool LIBNXDB_EXPORTABLE DBExecuteEx(DB_STATEMENT hStmt, TCHAR *errorText)
                if (hConn->m_driver->m_fpEventHandler != NULL)
                {
 #ifdef UNICODE
-                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, hStmt->m_query, wcErrorText, hConn->m_driver->m_userArg);
+                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, hStmt->m_query, wcErrorText, dwResult == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
 #else
                        WCHAR *query = WideStringFromMBString(hStmt->m_query);
-                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, query, wcErrorText, hConn->m_driver->m_userArg);
+                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, query, wcErrorText, dwResult == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
                        free(query);
 #endif
                }
@@ -1434,10 +1434,10 @@ DB_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedEx(DB_STATEMENT hStmt, TCHAR *error
                if (hConn->m_driver->m_fpEventHandler != NULL)
                {
 #ifdef UNICODE
-                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, hStmt->m_query, wcErrorText, hConn->m_driver->m_userArg);
+                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, hStmt->m_query, wcErrorText, dwError == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
 #else
                        WCHAR *query = WideStringFromMBString(hStmt->m_query);
-                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, query, wcErrorText, hConn->m_driver->m_userArg);
+                       hConn->m_driver->m_fpEventHandler(DBEVENT_QUERY_FAILED, query, wcErrorText, dwError == DBERR_CONNECTION_LOST, hConn->m_driver->m_userArg);
                        free(query);
 #endif
                }
index 25881ad..bcc1ed2 100644 (file)
@@ -455,7 +455,7 @@ static BOOL IsNetxmsdProcess(UINT32 dwPID)
 /**
  * Database event handler
  */
-static void DBEventHandler(DWORD dwEvent, const WCHAR *pszArg1, const WCHAR *pszArg2, void *userArg)
+static void DBEventHandler(DWORD dwEvent, const WCHAR *pszArg1, const WCHAR *pszArg2, bool connLost, void *userArg)
 {
        if (!(g_flags & AF_SERVER_INITIALIZED))
                return;     // Don't try to do anything if server is not ready yet
@@ -473,7 +473,7 @@ static void DBEventHandler(DWORD dwEvent, const WCHAR *pszArg1, const WCHAR *psz
                        NotifyClientSessions(NX_NOTIFY_DBCONN_STATUS, TRUE);
                        break;
                case DBEVENT_QUERY_FAILED:
-                       PostEvent(EVENT_DB_QUERY_FAILED, g_dwMgmtNode, "uu", pszArg1, pszArg2);
+                       PostEvent(EVENT_DB_QUERY_FAILED, g_dwMgmtNode, "uud", pszArg1, pszArg2, connLost ? 1 : 0);
                        break;
                default:
                        break;