ibm-information-center/dist/eclipse/plugins/i5OS.ic.cli_5.4.0.1/rzadpexusing.htm

242 lines
9.4 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Use the CLI XA transaction connection attributes" />
<meta name="DC.subject" content="CLI XA transaction" />
<meta name="keywords" content="CLI XA transaction" />
<meta name="DC.Relation" scheme="URI" content="rzadphdxmp.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1999, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1999, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rzadpexusing" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Use the CLI XA transaction connection attributes</title>
</head>
<body id="rzadpexusing"><a name="rzadpexusing"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Use the CLI XA transaction connection attributes</h1>
<div><div class="section"><p>This example shows how to use the CLI XA transaction connection
attributes.</p>
<div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms of
the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
<pre>/*************************************************************************
** file = CLIXAEXMP1.c
**
** Example of a typical flow of work in an XA transaction using the CLI.
**
** XA Functions used:
**
** xa_open() -- Open an XA resource for use in a transaction
** xa_prepare() -- Prepare for commitment of work in the transaction
** xa_commit() -- Commit work done in the transaction
**
** CLI Functions used:
**
** SQLAllocHanle SQLBindParameter SQLDisconnect
** SQLError SQLExecute SQLFreeHandle
** SQLPrepare SQLSetConnectAttr SQLSetEnvAttr
**
** This example will:
** - Open the XA transaction manager
** - Open a CLI connection and start a transaction for it using SQL_TXN_CREATE
** - Do some commitable CLI work under this transaction
** - End the transaction on the first connection using SQL_TXN_END
** - Close the first CLI connection and open a second connection
** - Use the SQL_TXN_FIND option to find the previous transaction
** - Do more commitable work on this transaction and end the transaction
** - Use the XA APIs to prepare and commit the work
************************************************************************************/
#define _XA_PROTOTYPES
#define _MULTI_THREADED
#include &lt;xa.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;sqlcli.h&gt;
#include &lt;time.h&gt;
#include &lt;stdlib.h&gt;
void genXid(XID *xid) {
time_t t;
memset(xid, 0, sizeof(xid));
xid-&gt;formatID = 69;
xid-&gt;gtrid_length = 4;
xid-&gt;bqual_length = 4;
/* xid-&gt;data must be a globally unique naming identifier
when taking gtrid and bqual together - the example below
is most likely not unique */
/* gtrid contents */
xid-&gt;data[0] = 0xFA;
xid-&gt;data[1] = 0xED;
xid-&gt;data[2] = 0xFA;
xid-&gt;data[3] = 0xED;
time(&amp;t);
/* bqual contents */
xid-&gt;data[4] = (((int)t) &gt;&gt; 24) &amp; 0xFF;
xid-&gt;data[5] = (((int)t) &gt;&gt; 16) &amp; 0xFF;
xid-&gt;data[6] = (((int)t) &gt;&gt; 8) &amp; 0xFF;
xid-&gt;data[7] = (((int)t) &gt;&gt; 0) &amp; 0xFF;
}
int main(int argc, char **argv)
{
/***************************************************/
/* Declarations Section */
/***************************************************/
SQLHENV henv;
SQLHDBC hdbc;
SQLHSTMT hstmt;
SQLRETURN rtnc;
SQLINTEGER attr;
SQLINTEGER int_buffer;
SQLINTEGER rlength;
SQLINTEGER buffint;
SQLINTEGER ilen;
SQLCHAR s[80];
SQLCHAR state[10];
SQLCHAR buffer[600];
SQLCHAR sqlstr[600];
SQLINTEGER natErr;
SQLSMALLINT len;
/* Declare local XA variables */
struct TXN_STRUCT new;
XID xid;
char xaOpenFormat[128];
int mainRmid = 1;
int xaRc;
/* Initialize the XA structure variable's (defined in sqlcli.h) */
strcpy(new.tminfo,"MYPRODUCT");
strcpy(new.reserved1,"");
new.timeoutval = 0;
new.locktimeout = 0;
strcpy(new.reserved2,"");
genXid(&amp;xid);
new.XID = &amp;xid;
/* Use the XA APIs to start the transaction manager */
/* The xa_info argument for xa_open MUST include the THDCTL=C keyword
and value when using using CLI with XA transactions */
sprintf(xaOpenFormat, "RDBNAME=*LOCAL THDCTL=C");
xaRc = xa_open(xaOpenFormat, mainRmid, TMNOFLAGS);
printf("xa_open(%s, %d, TMNOFLAGS) = %d\n",
xaOpenFormat, mainRmid, xaRc);
/* Setup the CLI resources */
attr=SQL_TRUE;
rtnc=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&amp;henv);
rtnc=SQLSetEnvAttr(henv,SQL_ATTR_SERVER_MODE,&amp;attr,0); /* set server mode */
rtnc=SQLAllocHandle(SQL_HANDLE_DBC,henv,&amp;hdbc);
/* Mark the connection as an external transaction and connect */
rtnc=SQLSetConnectAttr(hdbc,SQL_ATTR_TXN_EXTERNAL,&amp;attr,0);
rtnc=SQLConnect(hdbc,NULL,0,NULL,0,NULL,0);
/* Start the transaction */
new.operation = SQL_TXN_CREATE;
rtnc=SQLSetConnectAttr(hdbc,SQL_ATTR_TXN_INFO,&amp;new,0);
/* Do some CLI work */
rtnc=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&amp;hstmt);
strcpy(sqlstr,"insert into tab values(?)");
rtnc=SQLPrepare(hstmt,sqlstr,SQL_NTS);
rtnc=
SQLBindParameter(hstmt,1,1,SQL_INTEGER,SQL_INTEGER,10,2,&amp;buffint,0,&amp;ilen);
buffint=10; /* set the integer value to insert */
rtnc=SQLExecute(hstmt);
if (rtnc!=SQL_SUCCESS)
{
printf("SQLExecute failed with return code: %i \n", rtnc);
rtnc=SQLError(0, 0,hstmt, state, &amp;natErr, buffer, 600, &amp;len);
printf("%i is the SQLCODE\n",natErr);
printf("%i is the length of error text\n",len);
printf("%s is the state\n",state );
printf("%s \n",buffer);
}
else
printf("SQLExecute succeeded, value %i inserted \n", buffint);
/* End the transaction */
new.operation = SQL_TXN_END;
rtnc=SQLSetConnectAttr(hdbc,SQL_ATTR_TXN_INFO,&amp;new,0);
/* Cleanup and disconnect from the first connection */
rtnc=SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
rtnc=SQLDisconnect(hdbc);
/* Mark the second connection as an external transaction and connect */
attr=SQL_TRUE;
rtnc=SQLSetConnectAttr(hdbc,SQL_ATTR_TXN_EXTERNAL,&amp;attr,0);
rtnc=SQLConnect(hdbc,NULL,0,NULL,0,NULL,0);
/* Find the open transaction from the first connection */
new.operation = SQL_TXN_FIND;
rtnc=SQLSetConnectAttr(hdbc,SQL_ATTR_TXN_INFO,&amp;new,0);
/* Do some CLI work on the second connection */
rtnc=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&amp;hstmt);
strcpy(sqlstr,"insert into tab values(?)");
rtnc=SQLPrepare(hstmt,sqlstr,SQL_NTS);
rtnc=
SQLBindParameter(hstmt,1,1,SQL_INTEGER,SQL_INTEGER,10,2,&amp;buffint,0,&amp;ilen);
buffint=15; /* set the integer value to insert */
rtnc=SQLExecute(hstmt);
if (rtnc!=SQL_SUCCESS)
{
printf("SQLExecute failed with return code: %i \n", rtnc);
rtnc=SQLError(0, 0,hstmt, state, &amp;natErr, buffer, 600, &amp;len);
printf("%i is the SQLCODE\n",natErr);
printf("%i is the length of error text\n",len);
printf("%s is the state\n",state );
printf("%s \n",buffer);
}
else
printf("Second SQLExecute succeeded, value %i inserted \n", buffint);
/* End the transaction */
new.operation = SQL_TXN_END;
rtnc=SQLSetConnectAttr(hdbc,SQL_ATTR_TXN_INFO,&amp;new,0);
/* Now, use XA to prepare/commit transaction */
/* Prepare to commit */
xaRc = xa_prepare(&amp;xid, mainRmid, TMNOFLAGS);
printf("xa_prepare(xid, %d, TMNOFLAGS) = %d\n",mainRmid, xaRc);
/* Commit */
if (xaRc != XA_RDONLY) {
xaRc = xa_commit(&amp;xid, mainRmid, TMNOFLAGS);
printf("xa_commit(xid, %d, TMNOFLAGS) = %d\n", mainRmid, xaRc);
}
else {
printf("xa_commit() skipped for read only TX\n");
}
/* Cleanup the CLI resources */
rtnc=SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
rtnc=SQLDisconnect(hdbc);
rtnc=SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
rtnc=SQLFreeHandle(SQL_HANDLE_ENV,henv);
return 0;
}</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzadphdxmp.htm" title="This topic provides complete examples of DB2 UDB CLI applications.">Examples: DB2 UDB CLI applications</a></div>
</div>
</div>
</body>
</html>