ODBC example

The following example demonstrates program logic that offers improved performance when a matching cache is available and that still functions when no matching cache has been configured. The example first calls JagCmGetCachebyUser to obtain a cache handle for a cache that has matching values for the user name (“myrtle”), password (“secret”), and server name (“tsingtao”) and that uses ODBC. If such a cache exists, the call sets the cache variable to the cache handle.

The example then calls JagCmGetConnection, passing the cache value as set by JagCmGetCachebyUser, and passing explicit values for the user name, server name, password, and connectivity library. If the cache variable contains a valid cache reference, JagCmGetConnection looks directly in the cache for an available connection. If cache was set to NULL or the indicated cache has no available connections, JagCmGetConnection creates and opens a new, uncached connection.

Code that follows the implementation strategy illustrated here can achieve better performance when there are many configured caches. Passing the cache handle explicitly in JagCmGetConnection eliminates repeated internal table searches.

/* ODBC includes */
#include <sql.h>
#include <sqlext.h>
/* Connection Manager includes */
#include <jagpublic.h>

SQLRETURN ret;        /* Return code catcher    */
SQLHDBC *hdbc;        /* ODBC connection handle */
JagCmCache cache;     /* Cache handle           */
/*
** Retrieve a cache handle if a matching cache is configured.
** If not, our cache variable will be set to NULL.
*/
cache = NULL;
ret = JagCmGetCachebyUser (“myrtle”, “secret”,    “tsingtao”, “ODBC”, &cache);
/*
** Ignore the return code. If the call failed, cache will be 
** NULL and we can keep going.
*/
/*
** Obtain a connection. If we have a cache handle, the connection
** will be taken from the cache (if one is available). Otherwise,
** the call creates a new connection.
*/
ret = JagCmGetConnection (&cache, “myrtle”, “secret”,
   “tsingtao”, “ODBC”,(SQLPOINTER *)&hdbc,
   JAG_CM_FORCE);

if (ret != SQL_SUCCESS)
{
   ... log the error ...
}
... code that uses the connection goes here ...
ret = JagCmReleaseConnection (&cache, “myrtle”,
   “secret”,“tsingtao”, “ODBC”,
   hdbc, JAG_CM_UNUSED);

if (ret != SQL_SUCCESS)
{
   ... log the error ...
}

You can call JagCmGetCachebyName rather than JagCmGetCachebyUser. For an example, see the reference page for JagCmGetCachebyName in Chapter 5 of the EAServer API Reference.