Product Documentation

c-treeDB API API for C

Previous Topic

Next Topic

Session Logon and Logout

To perform any database operations, it is necessary to log on to a c-tree session. A session is terminated with a session logout.

To log on to a session, a session handle must be allocated with ctdbAllocSession() and then ctdbLogon() is called to perform the session logon.

CTHANDLE hSession = ctdbAllocSession(CTSESSION_CTDB);

if (!hSession)

FatalError("Session handle allocation failed\n");

if (ctdbLogon(hSession, "FAIRCOMS", "ADMIN", "ADMIN") != CTDBRET_OK)

FatalError("Session logon failed\n");

The parameters for the ctdbLogon() function are the same as for the ctdbCreateSession(). If the session dictionary doesn't exist and the session type is CTSESSION_CTDB or CTSESSION_SQL, ctdbLogon() function will fail returning error FNOP_ERR (12), indicating that c-treeDB API could not locate the session dictionary file.

Tip: A useful sequence is to try to log on to the session, and if it fails with error FNOP_ERR (12), create the session dictionary and then log on again.

CTSESSION hSession;

hSession = ctdbAllocSession(CTSESSION_CTDB);

if (!hSession)

FatalError("Session handle allocation failed\n");

if (ctdbLogon(hSession, "FAIRCOMS", "ADMIN", "ADMIN") == FNOP_ERR)

{

if (ctdbCreateSession(hSession, "FAIRCOMS", "ADMIN", "ADMIN")!= CTDBRET_OK)

FatalError("Error creating session dictionary file\n");

if (ctdbLogon(hSession, "FAIRCOMS", "ADMIN", "ADMIN") != CTDBRET_OK)

FatalError("Session logon failed\n");

}

When operations with the session are no longer needed, it is necessary to logout from the session by calling ctdbLogout().

CTHANDLE hSession;

hSession = ctdbAllocSession(CTSESSION_CTDB);

if (!hSession)

FatalError("Session handle allocation failed\n");

if (ctdbLogon(hSession, "FAIRCOMS", "ADMIN", "ADMIN") != CTDBRET_OK)

FatalError("Session logon failed\n");

/* perform some other operations on databases and tables */

if (ctdbLogout(hSession) != CTDBRET_OK)

FatalError("Session logout failed\n");

ctdbFreeSession(hSession);

TOCIndex