home *** CD-ROM | disk | FTP | other *** search
- ...
-
- /*
- ** Deadlock detection:
- ** In the DBPROCESS structure, we save a pointer to a DBBOOL variable.
- ** The message handler sets the variable when deadlock occurs.
- ** The result processing logic checks the variable and resends the
- ** transaction in case of deadlock.
- */
-
- /*
- ** Allocate the space for the DBBOOL variable and save it in
- ** the DBPROCESS structure.
- */
- dbsetuserdata(dbproc, malloc(sizeof(DBBOOL)));
-
- /* Initialize the variable to FALSE. */
- *((DBBOOL *) dbgetuserdata(dbproc)) = FALSE;
-
- ...
-
- /* Run queries and check for deadlock. */
- deadlock:
- /*
- ** Did we get here via deadlock?
- ** If so, the server has already aborted the transaction.
- ** We'll just start it again. In a real application, the
- ** deadlock handling may need to be somewhat more
- ** sophisticated. For instance, you may want to keep a
- ** counter and retry the transaction just a fixed number
- ** of times.
- */
-
- if (*((DBBOOL *) dbgetuserdata(dbproc)) == TRUE)
- {
- /* Reset the variable to FALSE. */
- *((DBBOOL *) dbgetuserdata(dbproc)) = FALSE;
- }
-
- /* Start the transaction. */
- dbcmd(dbproc, "begin transaction ");
-
- /* Run the first UPDATE command. */
- dbcmd(dbproc, "update ......");
- dbsqlexec(dbproc);
- while (dbresults(dbproc) != NO_MORE_RESULTS)
- {
- /* application code */
- }
-
- /* Did we deadlock? */
- if (*((DBBOOL *) dbgetuserdata(dbproc)) == TRUE)
- goto deadlock;
-
- /* Run the second UPDATE command. */
- dbcmd(dbproc, "update ......");
- dbsqlexec(dbproc);
- while (dbresults(dbproc) != NO_MORE_RESULTS)
- {
- /* application code */
- }
-
- /* Did we deadlock? */
- if (*((DBBOOL *) dbgetuserdata(dbproc)) == TRUE)
- goto deadlock;
-
- /* No deadlock -- Commit the transaction. */
- dbcmd(dbproc, "commit transaction");
- dbsqlexec(dbproc);
- dbresults(dbproc);
-
- ...
-
- /*
- ** SERVERMSGS
- ** This is the server message handler. Assume that the dbmsghandle()
- ** routine installed it earlier in the program.
- */
- servermsgs(dbproc, msgno, msgstate, severity, msgtext, srvname, procname, line)
- DBPROCESS *dbproc;
- DBINT msgno;
- int msgstate;
- int severity;
- char *msgtext;
- char *srvname;
- char *procname;
- DBUSMALLINT line;
- {
-
- /* Is this a deadlock message? */
- if (msgno == 1205)
- {
- /* Set the deadlock indicator. */
- *((DBBOOL *) dbgetuserdata(dbproc)) = TRUE;
- return (0);
- }
-
- /* Normal message handling code here. */
- }
-