Client-Library example

The following example calls JagCmGetConnection to obtain a connection that has a user name of “myrtle,” the password “secret,” connects to the server “tsingtao,” and uses Client-Library:

   #include <ctpublic.h>
   #include <jagpublic.h>

   CS_RETCODE ret;
   CS_CONNECTION *connection;
   JagCmCache cache;

   /*
   ** Get a connection.
   */
   cache = NULL;
   ret = JagCmGetConnection (&cache, “myrtle”, “secret”, “tsingtao”,       “CTLIB”,(SQLPOINTER *)&connection, JAG_CM_FORCE);

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

... code that uses the connection goes here ...

   ret = JagCmReleaseConnection (&cache, “myrtle”, “secret”, “tsingtao”,
      “CTLIB”, (SQLPOINTER)connection, JAG_CM_UNUSED);

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

In the example, the call to JagCmGetConnection looks for a data source that includes matching values for the user name (“myrtle”), password (“secret”), and server name (“tsingtao”) and that uses Client-Library. The last parameter value, JAG_CM_FORCE, indicates that the call should open a new connection if no cached connection is available. JagCmReleaseConnection releases control of the connection: a connection that was taken from a cache is returned to that cache; an uncached connection is closed and deallocated.

Note that JagCmGetConnection attempts to open a connection even when no matching data source is configured. In this case, JagCmGetConnection attempts to create a new connection using the specified values.

In this example, JagCmGetConnection and JagCmReleaseConnection return Client-Library return codes since both calls use “CTLIB” as the connection library parameter.

NoteBeginning in EAServer 6.0, you can use CTLIB as the connection library for Open Client 11.0, 12.0, and 12.5 connections. Version-specific CTLIB_x connection libraries are still provided for backward compatibility.

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