Interstage Shunsaku Data Manager Application Development Guide - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX -
Contents Index PreviousNext

Part 1 The Basic for Developing Application> Chapter 5 Transaction Function

5.4 Using Transactions with C APIs

This section provides an example of creating an application that uses C APIs.

The following example handles the deletion and updating of an XML document as a single transaction.

[Figure 5-3 Example of Handling the Deletion and Updating of an XML Document as a Single Transaction]

mark1Sample Code

/*This program assumes that the following variables have been declared
      */
/* query      :Search expression                                      */
/* returnQuery:Return expression                                    */
/* updateData :Update data                                         */
/* pResultId  :Structure array holding record ID                       */
/* pDeleteId  :Structure array for setting the record IDs to be deleted   */
/* pUpdateId  :Structure array for setting the record IDs to be updated   */

/* Allocate a connection handle */
ret = ShunAllocHandle(NULL, &ConH);
if(ret != SHUN_SUCCESS) {
   error("ShunAllocHandle ", ConH);
   return;
}
/* Establish a connection by specifying the host name and port number */
ret = ShunConnect(ConH, "DirSvr1", 33101, NULL);
if(ret != SHUN_SUCCESS) {
   error("ShunConnect ", ConH);
   return;
}
/* Allocate a data manipulation handle for searching data */
ret = ShunAllocHandle(ConH, &searchStmtH);
if(ret != SHUN_SUCCESS) {
   error("ShunAllocHandle ", ConH);
   return;
}
/* Execute search */
ret = ShunSearch(searchStmtH, 1, NULL, 0, 30, query, returnQuery,
                 NULL, &Hit_Cnt, &Return_Cnt, &Returnable_Cnt,
                 &Rec_Id_Out, &Data, &First_Pos, &Last_Pos);
if(ret != SHUN_SUCCESS) {
   error("ShunSearch ", searchStmtH);
   return;
}
/* Retain record ID */
for(i=0; i< Return_Cnt; i++) {
    pResultId[i] = Rec_Id_Out[i];
}

/* Release the data manipulation handle for searching data */
ret = ShunFreeHandle(searchStmtH);
if(ret != SHUN_SUCCESS) {
   error("ShunFreeHandle ", searchStmtH);
   return;
}

/* Turn auto commit off */
ret = ShunSetConnectAttr(ConH, SHUN_ATTR_AUTOCOMMIT, SHUN_FALSE);       (1)
if(ret != SHUN_SUCCESS) {
   error("ShunSetConnectAttr ", ConH);
   return;
}
/* Allocate a data manipulation handle for deleting data */
ret = ShunAllocHandle(ConH, &deleteStmtH);
if(ret != SHUN_SUCCESS) {
   error("ShunAllocHandle ", ConH);
   return;
}
/* Target the third item of data for deletion */
pDeleteId[0] = pResultId[2];
/* Execute deletion */
ret = ShunDeleteRecId(deleteStmtH, 1, pDeleteId);
if(ret != SHUN_SUCCESS) {
   error("ShunDeleteRecId ", deleteStmtH);
   return;
}
/* Release the data manipulation handle for deleting data */
ret = ShunFreeHandle(deleteStmtH);
if(ret != SHUN_SUCCESS) {
   error("ShunFreeHandle ",deleteStmtH);
   return;
}
/* Allocate a data manipulation handle for updating data */
ret = ShunAllocHandle(ConH, &updateStmtH);
if(ret != SHUN_SUCCESS) {
   error("ShunAllocHandle ", ConH);
   return;
}
/* Target the fifth item of data to be updated */
pUpdateId[0] = pResultId[4];
/* Update the data */
ret = ShunUpdateRecId(updateStmtH, 1, pUpdateId,updateData);
if(ret != SHUN_SUCCESS) {
  error("ShunUpdateRecId", updateStmtH);
  return;
}
/* Release the data manipulation handle for updating data */
ret = ShunFreeHandle(updateStmtH);
if(ret != SHUN_SUCCESS) {
   error("ShunFreeHandle ", updateStmtH);
   return;
}
/*  End the transactions */
ret = ShunCommit(ConH);                                    (2)
if(ret != SHUN_SUCCESS) {
   error("ShunCommit ", ConH);
   return;
}
/* Close the connection */
ret = ShunDisconnect(ConH);
if(ret != SHUN_SUCCESS) {
   error("ShunDisconnect", ConH);
   return;
}
/* Release the connection handle */
ret = ShunFreeHandle(ConH);
if(ret != SHUN_SUCCESS) {
   error("ShunAllocHandle ", ConH);
   return;
}

/* Error processing */
void error(char* func, SHUNHANDLE handle) {
  printf( "%s:abnormal end\n", func );
  ShunGetErrorMessage(handle, &Err_Level, &Err_Msg);
  printf("Error level =  %d\n", Err_Level);
  printf("Error message = %s\n", Err_Msg);
  if(ConH != NULL)
  {
    ret = ShunRollback(ConH);                (3)
  }
  ret = ShunFreeHandle(ConH); 
  return;
}

mark1(1) Switching auto commit to disabled state

The ShunSetConnectAttr function is used to disable auto commit after the connection handle has been allocated.

Switching is necessary because auto commit is enabled when a connection is established.
Use the ShunSetConnectAttr function to set SHUN_ATTR_AUTOCOMMIT to SHUN_TRUE to enable auto commit again.

mark1(2) Executing commit operation

The ShunCommit function is executed to terminate a transaction and update Shunsaku with the operations that have been performed.

mark1(3) Executing rollback

If an error occurs, the ShunRollback function is executed to roll back a transaction without updating Shunsaku with the operations that have been performed.

Refer to the C API Reference for more information on C APIs.

Down5.4.1 Error Handling

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006