INFORMIX
Informix-ESQL/C Programmer's Manual
Chapter 12: Working with the Database Server
Home Contents Index Master Index New Book

The timeout Program

The timeout program demonstrates how to set up a time-out interval. This program uses the sqlbreakcallback() function to perform the following actions:

If execution of an SQL request exceeds the time-out interval, the callback function uses the sqldone() function to ensure that the database server is still busy, prompts the user for confirmation of the interrupt, and then uses the sqlbreak() function to send an interrupt request to the database server.

Compiling the Program

Use the following command to compile the timeout program:

The -o timeout option causes the executable program to be named timeout. Without the -o option, the name of the executable program defaults to a.out. See "Using the esql Command" for more information on the esql command.

Guide to the timeout.ec File

1 /*

2 * timeout.ec *

3 */


	4	 #include <stdio.h>

5 #include <string.h>

6 #include <ctype.h>

7 #include <decimal.h>

8 #include <errno.h>

9 EXEC SQL include sqltypes;

10

11 #define LCASE(c) (isupper(c) ? tolower(c) : (c))

12 /* Defines for callback mechanism */

13 #define DB_TIMEOUT 200 /* number of milliseconds in time-out */

14 #define SQL_INTERRUPT -213 /* SQLCODE value for interrupted stmt */

15 /* These constants are used for the canceltst table, created by

16 * this program.

17 */

18 #define MAX_ROWS 10000 /* number of rows added to table */

19 EXEC SQL define CHARFLDSIZE 20; /* size of character columns in table */

20 /* Define for sqldone() return values */

21 #define SERVER_BUSY -439

22 /* These constants used by the exp_chk2() function to determine

23 * whether to display warnings.

24 */

25 #define WARNNOTIFY 1

26 #define NOWARNNOTIFY 0

27 long dspquery();

28 extern long exp_chk2();

29 void on_timeout();

30 main()

31 {

32 char ques[80], prompt_ans();

33 long ret;

34 int create_tbl(), drop_tbl();

35 printf("TIMEOUT Sample ESQL Program running.\n\n");

36 /*

37 * Establish an explicit connection to the stores7 database

38 * on the default database server.

39 */

40 EXEC SQL connect to 'stores7';

Continued on page 12-73

Lines 4 to 9

Lines 4 to 8 include the UNIX header files from the /usr/include directory. The ESQL/C sqltypes.h header file (line 9) defines names for integer values that identify SQL and C data types.

Lines 11 to 21

Line 11 defines LCASE, a macro that converts an uppercase character to a lowercase character. The DB_TIMEOUT (line 13) constant defines the number of milliseconds in the time-out interval. The SQL_INTERRUPT constant (line 14) defines the SQLCODE value that the database server returns when it interrupts an SQL statement.

Lines 18 and 19 define constants that the create_tbl() function uses to create the canceltst table. This table holds the test data needed for the large query (lines 126 to 133). MAX_ROWS is the number of rows that create_tbl() inserts into canceltst. You can change this number if you find that the query does not run long enough for you to interrupt it. CHARFLDSIZE is the number of characters in the character fields (char_fld1 and char_fld2) of canceltst.

Line 21 defines the SERVER_BUSY constant to hold the sqldone() return value that indicates that the database server is busy processing an SQL request. Use of this constant makes code more readable and removes the explicit return value from the code.

Lines 25 and 26

The exp_chk2() exception-handling function uses the WARNNOTIFY and NOWARNNOTIFY constants (lines 25 and 26). Calls to exp_chk2() specify one of these as the second argument to indicate whether the function displays SQLSTATE and SQLCODE information for warnings (WARNNOTIFY) or does not display this information for warnings (NOWARNNOTIFY). For more information on the exp_chk2() function, see "Lines 349 to 356".

Lines 30 to 34

The main() program block begins on line 30. Lines 32 to 34 declare variables local to the main() program block.

41 if (exp_chk2("CONNECT to stores7", NOWARNNOTIFY) < 0)

42 exit(1);

43 printf("Connected to 'stores7' on default server\n");

44 /*

45 * Create the canceltst table to hold MAX_ROWS (10,000) rows.

46 */

47 if (!create_tbl())

48 {

49 printf("\nTIMEOUT Sample Program over.\n\n");

50 exit(1);

51 }

52 while(1)

53 {

54 /*

55 * Establish on_timeout() as callback function. The callback

56 * function is called with an argument value of 2 when the

57 * database server has executed a single SQL request for number

58 * of milliseconds specified by the DB_TIMEOUT constant

59 * (0.00333333 minutes by default). Call to sqlbreakcallback()

60 * must come after server connection is established and before

61 * the first SQL statement that can be interrupted.

62 */

63 if (sqlbreakcallback(DB_TIMEOUT, on_timeout))

64 {

65 printf("\nUnable to establish callback function.\n");

66 printf("TIMEOUT Sample Program over.\n\n");

67 exit(1);

68 }

69 /*

70 * Notify end user of time-out interval.

71 */

72 printf("Time-out interval for SQL requests is: ");

73 printf("%0.8f minutes\n", DB_TIMEOUT/60000.00);

74 stcopy("Are you ready to begin execution of the query?",

75 ques);

76 if (prompt_ans(ques) == 'n')

77 {

78 /*

79 * Unregister callback function so table cleanup will not

80 * be interrupted.

81 */

82 sqlbreakcallback(-1L, (void *)NULL);

83 break;

84 }


Continued on  page 12-75

Lines 44 to 51

The create_tbl() function creates the canceltst table in the stores7 database. It inserts MAX_ROWS number of rows into this table. If create_tbl() encounters some error while it creates canceltst, execution of the timeout program cannot continue. The program exits with a status value of 1 (line 50).

Line 52

This while loop (which ends on line 98), controls the execution of the query on the canceltst table. It allows the user to run this query multiple times to test various interrupt scenarios.

Lines 54 to 68

The first task of the while loop is to use sqlbreakcallback() to specify a time-out interval of DB_TIMEOUT (200) milliseconds and to register on_timeout() as the callback function. If this call to sqlbreakcallback() fails, the program exits with a status value of 1. To test different time-out intervals, you can change the DB_TIMEOUT constant value and recompile the timeout.ec source file.

Lines 69 to 73

These printf() functions notify the user of the time-out interval. Notice that the message displays this interval in minutes, not milliseconds. It divides the DB_TIMEOUT value by 60,000 (number of milliseconds in a minute).

Lines 74 to 84

The prompt_ans() function asks the user to indicate when to begin execution of the canceltst query. If the user enters n (no), the program calls the sqlbreakcallback() function to unregister the callback function. This call prevents the SQL statements in the drop_tbl() function (lines 314 to 323) from initiating the callback function. For a description of the prompt_ans() function, see "Lines 338 to 348".

85 /*

86 * Start display of query output

87 */

88 printf("\nBeginning execution of query...\n\n");

89 if ((ret = dspquery()) == 0)

90 {

91 if (prompt_ans("Try another run?") == 'y')

92 continue;

93 else

94 break;

95 }

96 else /* dspquery() encountered an error */

97 exit(1);

98 } /* end while */

99 /*

100 * Drop the table created for this program

101 */

102 drop_tbl();

103 EXEC SQL disconnect current;

104 if (exp_chk2("DISCONNECT for stores7", WARNNOTIFY) != 0)

105 exit(1);

106 printf("\nDisconnected stores7 connection\n");

107 printf("\nTIMEOUT Sample Program over.\n\n");

108 }

109 /* This function performs the query on the canceltst table. */

110 long dspquery()

111 {

112 int cnt = 0;

113 long ret = 0;

114 long sqlcode = 0;

115 int sqlerr_code, sqlstate_err();

116 void disp_exception(), disp_error(), disp_warning();

117 EXEC SQL BEGIN DECLARE SECTION;

118 char fld1_val[ CHARFLDSIZE + 1 ];

119 char fld2_val[ CHARFLDSIZE + 1 ];

120 long int_val;

121 EXEC SQL END DECLARE SECTION;

122 /* This query contains an artifically complex WHERE clause to

123 * keep the database server busy long enough for an interrupt

124 * to occur.

125 */

126 EXEC SQL declare cancel_curs cursor for

127 select sum(int_fld), char_fld1, char_fld2

128 from canceltst

129 where char_fld1 matches "*f*"

130 or char_fld1 matches "*h*"

131 or char_fld2 matches "*w*"

132 or char_fld2 matches "*l*"

133 group by char_fld1, char_fld2;


Continued on  page 12-77

Lines 85 to 98

If the user chooses to continue the query, the program calls the dspquery() function (line 89) to run the canceltst query. The prompt_ans() function displays a prompt so the user can decide whether to run the program again.

Lines 99 to 102

The drop_tbl() function drops the canceltst table from the stores7 database to clean up after the program.

Lines 109 to 121

The dspquery() function runs a query of the canceltst table and displays the results. It returns zero (success) or the negative value of SQLCODE (failure) to indicate the result of the canceltst query.

Lines 122 to 133

Line 126 declares the cancel_curs cursor for the query. The actual SELECT (lines 127 to 133) obtains the sum of the int_fld column and the values of the two character columns (char_fld1 and char_fld2). The WHERE clause uses the MATCHES operator to specify matching rows, as follows:

    These criteria match rows with a char_fld1 value of Informix or "4100 Bohannon Dr."

    These criteria match rows with a char_fld2 value of Software or "Menlo Park, CA".

This SELECT is artificially complex to ensure that the query takes a long time to execute. Without a reasonably complex query, the database server finishes execution before the user has a chance to interrupt it. In a production application, only use the sqlbreakcallback() feature with queries that take a long time to execute.

134 EXEC SQL open cancel_curs;


	135	 	sqlcode = SQLCODE;

136 sqlerr_code = sqlstate_err(); /* check SQLSTATE for exception */

137 if (sqlerr_code != 0) /* if exception found */

138 {

139 if (sqlerr_code == -1) /* runtime error encountered */

140 {

141 if (sqlcode == SQL_INTERRUPT) /* user interrupt */

142 {

143 /* This is where you would clean up resources */

144 printf("\n TIMEOUT INTERRUPT PROCESSED\n\n");

145 sqlcode = 0;

146 }

147 else /* serious runtime error */

148 disp_error("OPEN cancel_curs");

149 EXEC SQL close cancel_curs;

150 EXEC SQL free cancel_curs;

151 return(sqlcode);

152 }

153 else if (sqlerr_code == 1) /* warning encountered */

154 disp_warning("OPEN cancel_curs");

155 }

Continued on page 12-79

Line 134

This OPEN statement causes the database server to execute the SELECT that is associated with the cancel_curs cursor. Because the database server executes the canceltst query at this point, this OPEN is the statement that the user would be most likely to interrupt. When the FETCH executes, the database server just sends matching rows to the application, an operation that is not usually time intensive.

Lines 135 to 155

This block of code checks the success of the OPEN. Since the OPEN can be interrupted, this exception checking must include an explicit check for the interrupt value of -213. The database server sets SQLCODE to -213 when it has interrupted an SQL request. On line 141, the program uses the SQL_INTERRUPT defined constant (which line 14 defines), for this SQLCODE value.

The sqlstate_err() function (line 136) uses the GET DIAGNOSTICS statement to analyze the value of the SQLSTATE variable. If this function returns a non-zero value, SQLSTATE indicates a warning, a runtime error, or the NOT FOUND condition. Before the call to sqlstate_err(), line 135 saves the SQLCODE value so that execution of any other SQL statements (such as GET DIAGNOSTICS in sqlstate_err()) does not overwrite it. The function returns the value of SQLCODE if the OPEN encounters a runtime error (line 151).

The first if statement (line 137) checks if the OPEN encounters any type of exception (sqlstate_err() returns a nonzero value). The second if (line 139) checks if the OPEN has generated a runtime error (return value of -1). However, if the database server has interrupted the OPEN, sqlstate_err() also returns -1. Since ESQL/C does not handle an interrupted SQL statement as a runtime error, the third if checks explicitly for the SQL_INTERRUPT value (line 141). If the OPEN was interrupted, line 144 notifies the user that the interrupt request was successful and then the function resets the saved SQLCODE value (in sqlcode) to zero to indicate that the OPEN did not generate a runtime error.

Lines 147 and 148 execute only if the OPEN generates a runtime error other than SQL_INTERRUPT (-213). The disp_error() function displays the exception information in the diagnostics area and the SQLCODE value. Lines 149 to 151 clean up after the OPEN. They close and free the cancel_curs cursor and then return the SQLCODE value. The dspquery() function does not continue with the FETCH (line 159) if the OPEN has been interrupted.

If sqlstate_err() returns one (1), the OPEN has generated a warning. Lines 153 and 154 call the disp_warning() function to display warning information from the diagnostics area. For more information on the disp_error() and disp_warning() functions, see Lines 342 to 349 on page 12-88.

156 printf("Displaying data...\n");

157 while(1)

158 {

159 EXEC SQL fetch cancel_curs into :int_val, :fld1_val, :fld2_val;

160 if ((ret = exp_chk2("FETCH from cancel_curs", NOWARNNOTIFY)) == 0)

161 {

162 printf(" sum(int_fld) = %ld\n", int_val);

163 printf(" char_fld1 = %s\n", fld1_val);

164 printf(" char_fld2 = %s\n\n", fld2_val);

165 }


	166	 		/*

167 * Will display warning messages (WARNNOTIFY) but continue

168 * execution when they occur (exp_chk2() == 1)

169 */

170 else

171 {

172 if (ret==100) /* NOT FOUND condition */

173 {

174 printf("\nNumber of rows found: %d\n\n", cnt);

175 break;

176 }

177 if (ret < 0) /* Runtime error */

178 {

179 EXEC SQL close cancel_curs;

180 EXEC SQL free cancel_curs;

181 return(ret);

182 }

183 }

184 cnt++;

185 } /* end while */

186 EXEC SQL close cancel_curs;

187 EXEC SQL free cancel_curs;

188 return(0);

189 }

190 /*

191 * The on_timeout() function is the callback function. If the user

192 * confirms the cancellation, this function uses sqlbreak() to

193 * send an interrupt request to the database server.

194 */

195 void on_timeout(when_called)

196 int when_called;

197 {

198 int ret;

199 static intr_sent;

Continued on page 12-81

Lines 156 to 183

This while loop executes for each row that the cancel_curs cursor contains. The FETCH statement (line 159) retrieves one row from the cancel_curs cursor. If the FETCH generates an error, the function releases the cursor resources and returns the SQLCODE error value (lines 177 to 182). Otherwise, the function displays the retrieved data to the user. On the last row (ret = 100), the function displays the number of rows that it retrieved (line 174).

Lines 186 to 188

After the FETCH has retrieved the last row from the cursor, the function releases resources allocated to the cancel_curs cursor and returns a success value of zero.

Lines 190 to 199

The on_timeout() function is the callback function for the timeout program. The sqlbreakcallback() call on line 63 registers this callback function and establishes a time-out interval of 200 milliseconds. This function is called every time the database server begins and ends an SQL request. For long-running requests, the application also calls on_timeout() each time the time-out interval elapses.

200 /* Determine when callback function has been called. */

201 switch(when_called)

202 {

203 case 0: /* Request to server completed */

204 printf("+------SQL Request ends");

205 printf("-------------------------------+\n\n");


	206	 			/*

207 * Unregister callback function so no further SQL statements

208 * can be interrupted.

209 */

210 if (intr_sent)

211 sqlbreakcallback(-1L, (void *)NULL);

212 break;

213 case 1: /* Request to server begins */

214 printf("+------SQL Request begins");

215 printf("-----------------------------+\n");

216 printf("| ");

217 printf(" |\n");

218 intr_sent = 0;

219 break;

220 case 2: /* Time-out interval has expired */

221 /*

222 * Is the database server still processing the request?

223 */

224 if (sqldone() == SERVER_BUSY)

225 if (!intr_sent) /* has interrupt already been sent? */

226 {

227 printf("| An interrupt has been received ");

228 printf("by the application.|\n");

229 printf("| ");

230 printf(" |\n");

231 /*

232 * Ask user to confirm interrupt

233 */

234 if (cancel_request())

235 {

236 printf("| TIMEOUT INTERRUPT ");

237 printf("REQUESTED |\n");

238 /*

239 * Call sqlbreak() to issue an interrupt request for

240 * current SQL request to be cancelled.

241 */

242 sqlbreak();

243 }

244 intr_sent = 1;

245 }

246 break;

Continued on page 12-83

Lines 200 to 250

This switch statement uses the callback function argument, when_called, to determine the actions of the callback function, as follows:

To handle the elapsed time-out interval, the callback function first calls the ESQL/C sqldone() function (line 224) to determine whether the database server is still busy processing the SQL request. If the database server is idle, the application does not need to send an interrupt. If sqldone() returns SERVER_BUSY (-439), the database server is still busy.

Line 225 checks if the user has already attempted to interrupt the SQL request that is currently executing. If an interrupt has been sent, intr_sent is 1, and the program does not need to send another request. If an interrupt request has not yet been sent, the callback function notifies the user that the time-out interval has elapsed (lines 227 to 230). It then uses the cancel_request() function (line 234) to allow the user to confirm the interrupt. For more information on cancel_request(), see "Lines 252 to 262".

247 default:

248 printf("Invalid status value in callback: %d\n", when_called);

249 break;

250 }

251 }


	252	 /* This function prompts the user to confirm the sending of an

253 * interrupt request for the current SQL request.

254 */

255 int cancel_request()

256 {

257 char prompt_ans();

258 if (prompt_ans("Do you want to confirm this interrupt?") == 'n')

259 return(0); /* don't interrupt SQL request */

260 else

261 return(1); /* interrupt SQL request */

262 }

263 /* This function creates a new table in the current database. It

264 * populates this table with MAX_ROWS rows of data. */

265 int create_tbl()

266 {

267 char st_msg[15];

268 int ret = 1;

269 EXEC SQL BEGIN DECLARE SECTION;

270 int cnt;

271 int pa;

272 int i;

273 char fld1[ CHARFLDSIZE + 1 ], fld2[ CHARFLDSIZE + 1 ];

274 EXEC SQL END DECLARE SECTION;

275 /*

276 * Create canceltst table in current database

277 */

278 EXEC SQL create table canceltst (char_fld1 char(20),

279 char_fld2 char(20), int_fld integer);

280 if (exp_chk2("CREATE TABLE", WARNNOTIFY) < 0)

281 return(0);

282 printf("Created table 'canceltst'\n");

283 /*

284 * Insert MAX_ROWS of data into canceltst

285 */

286 printf("Inserting rows into 'canceltst'...\n");

287 for (i = 0; i < MAX_ROWS; i++)

288 {

Continued on page 12-85

Lines 200 to 250 (continued)

If the user confirms the interrupt, the callback function calls the sqlbreak() function to send the interrupt request to the database server. The callback function does not wait for the database server to respond to the interrupt request. Execution continues to line 244 and sets the intr_sent flag to 1, to indicate that the interrupt request was sent. If the callback function was called with an invalid argument value (a value other than 0, 1, or 2), the function displays an error message (line 248).

Lines 252 to 262

The cancel_request() function asks the user to confirm the interrupt request. It displays the prompt:

If the user answers y (yes), cancel_request() returns 0. If the user answers n (no), cancel_request() returns 1.

Lines 263 to 282

The create_tbl() function creates the canceltst table and inserts the test data into this table. The CREATE TABLE statement (lines 278 and 279) creates the canceltst table with three columns: int_fld, char_fld1, and char_fld2. If the CREATE TABLE encounters an error, the exp_chk2() function (line 280) displays the diagnostics-area information and create_tbl() returns zero to indicate that an error has occurred.

Lines 283 to 288

This for loop controls the insertion of the canceltst rows. The MAX_ROWS constant determines the number of iterations for the loop, and hence the number of rows that the function inserts into the table. If you cannot interrupt the canceltst query (lines 127 to 133) because it executes too quickly, increase the value of MAX_ROWS and recompile the timeout.ec file.

289 if (i%2 == 1) /* odd-numbered rows */

290 {

291 stcopy("4100 Bohannan Dr", fld1);

292 stcopy("Menlo Park, CA", fld2);

293 }

294 else /* even-numbered rows */

295 {

296 stcopy("Informix", fld1);

297 stcopy("Software", fld2);

298 }

299 EXEC SQL insert into canceltst

300 values (:fld1, :fld2, :i);

301 if ( (i+1)%1000 == 0 ) /* every 1000 rows */

302 printf(" Inserted %d rows\n", i+1);

303 sprintf(st_msg, "INSERT #%d", i);

304 if (exp_chk2(st_msg, WARNNOTIFY) < 0)

305 {

306 ret = 0;

307 break;

308 }

309 }

310 printf("Inserted %ld rows into 'canceltst'.\n", MAX_ROWS);


	311	 /*

312 * Verify that MAX_ROWS rows have added to canceltst

313 */

314 printf("Counting number of rows in 'canceltst' table...\n");

315 EXEC SQL select count(*) into :cnt from canceltst;

316 if (exp_chk2("SELECT count(*)", WARNNOTIFY) < 0)

317 return(0);

318 printf("Number of rows = %ld\n\n", cnt);

319 return (ret);

320 }

321 /* This function drops the 'canceltst' table */

322 int drop_tbl()

323 {

324 printf("\nCleaning up...\n");

325 EXEC SQL drop table canceltst;

326 if (exp_chk2("DROP TABLE", WARNNOTIFY) < 0)

327 return(0);

328 printf("Dropped table 'canceltst'\n");

329 return(1);

330 }

Continued on page 12-87

Lines 289 to 293

This if statement generates the values for the char_fld1 and char_fld2 columns of the canceltst table. Lines 291 and 292 execute for odd-numbered rows. They store the strings "4100 Bohannon Dr" and "Menlo Park, CA" in the fld1 and fld2 variables, respectively.

Lines 294 to 298

Lines 296 and 297 execute for even-numbered rows. They store the strings Informix and Software in the fld1 and fld2 variables, respectively.

Lines 299 to 308

The INSERT statement inserts a row into the canceltst table. It takes the value for the int_fld column from the :i host variable (the row number), and the values for the char_fld1 and char_fld2 columns from the :fld1 and :fld2 host variables, respectively. The function notifies the user after it inserts every 1000 rows (lines 301 and 302). If the INSERT encounters an error, the exp_chk2() function (line 304) displays the diagnostics-area information and create_tbl() returns zero to indicate that an error has occurred.

Lines 311 to 318

These lines verify that the program has added the rows to the canceltst table and that it can access them. The program does a SELECT on the newly created canceltst table and returns the number of rows found. The program checks whether this number matches the number that the function has added, which line 310 displays. If the SELECT encounters an error, the exp_chk2() function (line 316) displays the diagnostics-area information, and create_tbl() returns 0 to indicate that an error has occurred.

Lines 321 to 330

The drop_tbl() function drops the canceltst table from the current database. If the DROP TABLE statement (line 325) encounters an error, the exp_chk2() function displays the diagnostics-area information and drop_tbl() returns 0 to indicate that an error has occurred.

331 /*

332 * The inpfuncs.c file contains the following functions used in this

333 * program:

334 * getans(ans, len) - accepts user input, up to 'len' number of

335 * characters and puts it in 'ans'

336 */

337 #include "inpfuncs.c"


	338	 char prompt_ans(question)

339 char * question;

340 {

341 char ans = ` `;

342 while(ans != 'y' && ans != 'n')

343 {

344 printf("\n*** %s (y/n): ", question);

345 getans(&ans,1);

346 }

347 return ans;

348 }

349 /*

350 * The exp_chk() file contains the exception handling functions to

351 * check the SQLSTATE status variable to see if an error has occurred

352 * following an SQL statement. If a warning or an error has

353 * occurred, exp_chk2() executes the GET DIAGNOSTICS statement and

354 * displays the detail for each exception that is returned.

355 */

356 EXEC SQL include exp_chk.ec;

Lines 331 to 337

Several of the ESQL/C demonstration programs also call the getans() function. Therefore, this function is broken out into a separate C source file and included in the appropriate demonstration program. Because this function does not contain ESQL/C, the program can use the C #include preprocessor statement to include the file. For a description of this function, see "Guide to the inpfuncs.c File".

Lines 338 to 348

The prompt_ans() function displays the string in the question argument and waits for the user to enter y (yes) or n (no) as a response. It returns the single-character response.

Lines 349 to 356

The timeout program uses the exp_chk2(), sqlstate_err(), disp_error(), and disp_warning() functions to perform its exception handling. Because several demonstration programs use these functions, the exp_chk2() function and its supporting functions have been placed in a separate exp_chk.ec source file. The timeout program must include this file with the ESQL/C include directive because the exception-handling functions use ESQL/C statements. For a description of the exp_chk.ec file, see "Guide to the exp_chk.ec File".

Tip: In a production environment, you would put functions such as getans(), exp_chk2(), sqlstate_err(), disp_error(), and disp_warning() into a library and include this library on the command line of the ESQL/C-program compilation.

Example Output

This section includes a sample output of the timeout demonstration program. This program performs two runs of the canceltst query, as follows:

The numbers that appear in the following output are for explanation only. They do not appear in the actual program output.

1 TIMEOUT Sample ESQL Program running.

2

3 Connected to 'stores7' on default server

4 Created table 'canceltst'

5 Inserting rows into 'canceltst'...

6 Inserted 1000 rows

7 Inserted 2000 rows

8 Inserted 3000 rows

9 Inserted 4000 rows

10 Inserted 5000 rows

11 Inserted 6000 rows

12 Inserted 7000 rows

13 Inserted 8000 rows

14 Inserted 9000 rows

15 Inserted 10000 rows

16 Inserted 10000 rows into 'canceltst'.

17 Counting number of rows in 'canceltst' table...

18 Number of rows = 10000

19

20 Time-out interval for SQL requests is: 0.00333333 minutes

21

22 *** Are you ready to begin execution of the query? (y/n): y

23

24 Beginning execution of query...

25

26 +------SQL Request begins-----------------------------+

27 | |

28 +------SQL Request ends-------------------------------+

29

30 +------SQL Request begins-----------------------------+

31 | |

32 | An interrupt has been received by the application.|

33 | |

34

35 *** Do you want to confirm this interrupt? (y/n): y

36 | TIMEOUT INTERRUPT REQUESTED |

37 +------SQL Request ends-------------------------------+

Continued on page 12-91

Lines 4 to 18

The create_tbl() function generates these lines. They indicate that the function has successfully created the canceltst table, inserted the MAX_ROWS number of rows (1,000), and confirmed that a SELECT statement can access these rows. For a description of the create_tbl() function, see the annotation beginning with "Lines 263 to 282".

Lines 20 to 22

Line 20 displays the time-out interval to indicate that sqlbreakcallback() has successfully registered the callback function and established the time-out interval of 200 milliseconds (0.00333333 minutes). Line 22 asks the user to indicate the beginning of the query execution. This prompt prepares the user for the confirmation prompt (lines 35 and 59), which must be answered quickly to send an interrupt while the database server is still executing the query.

Line 24

This line indicates the beginning of the dspquery() function, the point at which the database server begins the canceltst query.

Lines 26 to 37

The program output uses a message-request box to indicate client-server communication:

Each box represents a single message request sent between the client and the server. The callback function displays the text for a message-request box. (For a description of which parts of the function display the text, see "Lines 200 to 250".) To execute the OPEN statement, the client and server exchanged two message requests, which the two message-request boxes in the output indicate. For more information on message requests, see "Interruptible SQL Statements".

The first message-request box (lines 26 to 28) indicates that the first message request completes before the time-out interval elapses. The second message-request box (lines 30 to 37) indicates that execution of this message request exceeds the time-out interval and calls the callback function with a status value of 2. The callback function prompts the user to confirm the interrupt request (line 35).

Line 36 indicates that the sqlbreak() function has requested an interrupt. The message request then completes (line 37).

38

39

40 TIMEOUT INTERRUPT PROCESSED

41

42

43 *** Try another run? (y/n): y

44 Time-out interval for SQL requests is: 0.00333333 minutes

45

46 *** Are you ready to begin execution of the query? (y/n): y

47

48 Beginning execution of query...

49

50 +------SQL Request begins-----------------------------+

51 | |

52 +------SQL Request ends-------------------------------+

53

54 +------SQL Request begins-----------------------------+

55 | |

56 | An interrupt has been received by the application.|

57 | |

58

59 *** Do you want to confirm this interrupt? (y/n): n

60 +------SQL Request ends-------------------------------+

61

62 Displaying data...

63 sum(int_fld) = 25000000

64 char_fld1 = 4100 Bohannan Dr

65 char_fld2 = Menlo Park, CA

66

67 sum(int_fld) = 24995000

68 char_fld1 = Informix

69 char_fld2 = Software

70

71

72 Number of rows found: 2

73

74

75 *** Try another run? (y/n): n

76

77 Cleaning up...

78 Dropped table 'canceltst'

79

80 Disconnected stores7 connection

81

82 TIMEOUT Sample Program over.

83

Line 40

When the database server actually processes the interrupt request, it sets SQLCODE to -213. Line 40 indicates that the application program has responded to this status.

Line 43

This prompt indicates the end of the first run of the canceltst query. The user responds y to the prompt to run the query a second time.

Lines 50 to 56

The message-request box indicates that the first message request completes before the time-out interval elapses. The second message-request box (lines 54 to 60) indicates that execution of this message request again exceeds the time-out interval and calls the callback function (with when_called = 2). The callback function prompts the user to confirm the interrupt request (line 59). This time the user answers n.

Lines 62 to 72

Because the user has not interrupted the canceltst query, the program displays the row information that the query returns.

Lines 77 and 78

The drop_tbl() function generates these lines. They indicate that the function has successfully dropped the canceltst table from the database. For a description of the drop_tbl() function, see the annotation beginning with "Lines 321 to 330".




Informix-ESQL/C Programmer's Manual, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.