samedi 2 juillet 2016

C - Set timeout for stored procedure call (ocilib)

I'm calling an Oracle stored procedure which is in another server. So, if there is a network problem, the application "gets stuck" in the OCI_Execute(statement).

I need to set a timeout to this SP execution or maybe check the connection status, I don't know what's the usual approach.

Here is an example of what I'm doing:

#include <stdio.h>
#include <ocilib.h>

OCI_Connection *dbConn;
OCI_Statement *storedProcStmt;
otext *storedProcQuery = "BEGIN TEST_PROC (:param1,:param2,:result); END;";

int main()
{
    if(connectDB() != 0){
        return (1);
    }

    storedProcStmt = OCI_StatementCreate(dbConn);
    OCI_Prepare(storedProcStmt, storedProcQuery);

    char paramResult[3] = "";
    OCI_BindString(storedProcStmt, ":param1", "123", strlen("123"));
    OCI_BindString(storedProcStmt, ":param2", "abc", strlen("abc"));
    OCI_BindString(storedProcStmt, ":result", paramResult, 2);

    execStoredProc(storedProcStmt);

    printf("RESULT:n");
    printf("%sn", paramResult);

    return (0);
}

int connectDB()
{
    if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT))
        return (EXIT_FAILURE);

    dbConn = OCI_ConnectionCreate("xe", "user", "password", OCI_SESSION_DEFAULT);

    if (dbConn == NULL)
    {
        printf("Could not connect to DBn");
        return (1);
    }

    printf("Connected to DBn");

    return (0);
}

int execStoredProc(OCI_Statement *stmt)
{
    OCI_Execute(stmt);
    OCI_Commit(dbConn);
    OCI_StatementFree(stmt);
}

As I said, it's in the first line of the function execStoredProc -OCI_Execute(stmt);- where the program gets stuck when the network fails, how can I set a limit of, for example, 5 seconds for this function to execute. I just need a hint of what to search or a code example, thanks!

Aucun commentaire:

Enregistrer un commentaire