OCILIB Tutorials v5.0.0
Beginner

The environment — init, flags & error handler

Objectives — by the end of this page you will be able to:

The environment

The environment is OCILIB's root object — the context in which everything else exists. Creating it does three jobs at once:

Think of it as "the library, initialized": nothing works until it exists, and freeing it ends the program's relationship with Oracle.

Most programs need exactly one environment, created at startup and freed at exit — the object tree makes it naturally process-wide. v5 does allow several independent environments in one process, but that is for specific cases (a plugin needing its own error handler, a component needing different flags) and handles never cross environment boundaries — the environments page in Part 1 covers when a second one is worth it.

Creating it takes three inputs — your error callback, an optional path to the Oracle client, and a set of flags:

OCI_Environment *OCI_EnvironmentCreate
(
    POCI_ERROR       handler,   /* your error callback (may be NULL) */
    const otext      *libpath,   /* Oracle client path (may be NULL) */
    unsigned int      mode       /* environment flags */
);
C enum C++ enum Meaning
OCI_ENV_DEFAULT Environment::Default plain single-threaded app
OCI_ENV_THREADED Environment::Threaded app uses multiple threads (Part 5)
OCI_ENV_EVENTS Environment::Events subscriptions/HA events (Part 7)

Destroying the environment releases everything — do it once, last, after every child handle is gone. In C++ destruction is automatic; in C it's explicit:

OCI_EnvironmentFree(env);

The error handler, minimal and realistic

The handler receives a filled error object — its fields are the subject of the next page. For now, printing the message is plenty:

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

static void err_handler(OCI_Error *err)
{
    fprintf(stderr, "[ocilib] %s\n", OCI_ErrorGetString(err));
}

int main(void)
{
    OCI_Environment *env = OCI_EnvironmentCreate(err_handler, NULL,
                                                 OCI_ENV_DEFAULT);
    if (!env)
    {
        fprintf(stderr, "Cannot initialize OCILIB — is the Oracle client on PATH?\n");
        return EXIT_FAILURE;
    }

    printf("OCILIB environment ready.\n");

    OCI_EnvironmentFree(env);
    return EXIT_SUCCESS;
}
OCILIB environment ready.

C++ has no error callback

The C++ layer converts every failure into ocilib::Exception — that is the error channel, so the Environment takes only flags and an optional lib path.

When does the handler fire?

On every failed public call — connection failures, SQL syntax errors, constraint violations, and even warnings once you enable them on the environment (next page shows the severity/category fields).

A handler that prints is great for learning; real apps usually log and let last-error polling carry details to the call site.

Putting it together — the typical sequence

Every OCILIB program — hello-world or production — runs the same five steps:

  1. Create the environment — OCI_EnvironmentCreate(handler, NULL, flags) or Environment env; in C++. Nothing works without it.
  2. Connect — OCI_ConnectionCreate with db string, user, password.
  3. Do work — statements, fetches, binds; this is where your code lives.
  4. Disconnect — OCI_ConnectionFree releases the session.
  5. Free the environment — last, after every child handle is gone.

Hints: order matters — environment first, free last, children in between; the C++ Environment is RAII — the ctor/dtor bracket the same calls; and the error handler you register at step 1 is what OCI_GetLastError consults throughout — write it before you need it.

Try it yourself

  1. Pass a bogus libpath — observe the environment creation fail cleanly.
  2. In C, pass NULL as the handler and poll the last error after a failed call: the same error is still retrievable.
  3. Skim demo/err.c, demo/err_ctx.c, demo/err_warning.c — three complete error-handling demos matching this page.

Common pitfalls

Recap

Next: put it to work — open your first connection.