home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer) / NeXT_Developer-3.3.iso / NextLibrary / Documentation / Sybase / DBLIB / Section2 / dbsetuserdata.ex < prev    next >
Encoding:
Text File  |  1993-04-22  |  2.4 KB  |  100 lines

  1.  ...
  2.  
  3. /*
  4. **  Deadlock detection:
  5. **     In the DBPROCESS structure, we save a pointer to a DBBOOL variable.
  6. **     The message handler sets the variable when deadlock occurs.  
  7. **     The result processing logic checks the variable and resends the 
  8. **     transaction in case of deadlock.
  9. */
  10.  
  11.     /*
  12.     **  Allocate the space for the DBBOOL variable and save it in
  13.     **  the DBPROCESS structure.
  14.     */
  15.     dbsetuserdata(dbproc, malloc(sizeof(DBBOOL)));
  16.  
  17.     /*  Initialize the variable to FALSE.  */
  18.     *((DBBOOL *) dbgetuserdata(dbproc)) = FALSE;
  19.  
  20.  ...
  21.  
  22.     /*  Run queries and check for deadlock.  */
  23. deadlock:
  24.     /*
  25.     **  Did we get here via deadlock?
  26.     **  If so, the server has already aborted the transaction.
  27.     **  We'll just start it again.  In a real application, the
  28.     **  deadlock handling may need to be somewhat more 
  29.     **  sophisticated.  For instance, you may want to keep a
  30.     **  counter and retry the transaction just a fixed number
  31.     **  of times.
  32.     */
  33.  
  34.     if (*((DBBOOL *) dbgetuserdata(dbproc)) == TRUE)
  35.     {
  36.         /* Reset the variable to FALSE. */
  37.         *((DBBOOL *) dbgetuserdata(dbproc)) = FALSE;
  38.     }
  39.  
  40.     /* Start the transaction.  */
  41.     dbcmd(dbproc, "begin transaction ");
  42.  
  43.     /* Run the first UPDATE command.  */
  44.     dbcmd(dbproc, "update ......");
  45.     dbsqlexec(dbproc);
  46.     while (dbresults(dbproc) != NO_MORE_RESULTS)
  47.     {
  48.         /* application code */
  49.     }
  50.  
  51.     /*  Did we deadlock? */
  52.     if (*((DBBOOL *) dbgetuserdata(dbproc)) == TRUE)
  53.         goto deadlock;
  54.  
  55.     /*  Run the second UPDATE command.  */
  56.     dbcmd(dbproc, "update ......");
  57.     dbsqlexec(dbproc);
  58.     while (dbresults(dbproc) != NO_MORE_RESULTS)
  59.     {
  60.         /* application code */
  61.     }
  62.  
  63.     /*  Did we deadlock? */
  64.     if (*((DBBOOL *) dbgetuserdata(dbproc)) == TRUE)
  65.         goto deadlock;
  66.  
  67.     /*  No deadlock -- Commit the transaction.  */
  68.     dbcmd(dbproc, "commit transaction");
  69.     dbsqlexec(dbproc);
  70.     dbresults(dbproc);
  71.  
  72.  ...
  73.  
  74. /*
  75. **  SERVERMSGS
  76. **    This is the server message handler.  Assume that the dbmsghandle()
  77. **      routine installed it earlier in the program.
  78. */
  79. servermsgs(dbproc, msgno, msgstate, severity, msgtext, srvname, procname, line)
  80. DBPROCESS        *dbproc;
  81. DBINT            msgno;
  82. int              msgstate;
  83. int              severity;
  84. char             *msgtext;
  85. char             *srvname;
  86. char             *procname;
  87. DBUSMALLINT      line;
  88. {
  89.  
  90.     /*  Is this a deadlock message? */
  91.     if (msgno == 1205)
  92.     {
  93.         /* Set the deadlock indicator. */
  94.         *((DBBOOL *) dbgetuserdata(dbproc)) = TRUE;
  95.         return (0);
  96.     }
  97.  
  98.     /*  Normal message handling code here.  */
  99. }
  100.