The following example demonstrates program logic that offers improved performance when a matching data source is available and that still functions when no matching data source has been configured. The example first calls JagCmGetCachebyUser to obtain a CM_CACHE handle to a temporary ODBC data source using the values: user name (“myrtle”), password (“secret”), and server name (“tsingtao”). The code sets the cache variable to the CM_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 data source reference, JagCmGetConnection looks directly in the data source for an available connection. If cache was set to NULL or the indicated data source has no available connections, JagCmGetConnection creates and opens a new connection.
Code that follows the implementation strategy illustrated here can achieve better performance when there are many configured data sources. Passing the CM_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 CM_CACHE handle */ cache = NULL; ret = JagCmGetCachebyUser (“myrtle”,“secret”,“tsingtao”,“ODBC”,&cache); /* ** Ignore the return value. If the call fails, cache is NULL and we can keep ** going. */ /* ** Get a connection. If we have a cache handle, use it to get the connection. ** Otherwise, create 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.