1. Introduction
In recent years, with the continuous development of computer LAN technology, architecture has developed into a complex and open client/server model. For client/server application development, now commonly used front-end development tools are: VisualBasic, Delphi, PowerBuilder and so on. They access the SQL Server database server by using an ODBC interface.
VisualBasic, Delphi, PowerBuilder and other development tools in the use of ODBC2. 0来 when writing a program, you typically provide three ways to design your database application:
• Using Data Control items
• Programming with Database object variables
• Direct call to ODBC2. 0API
When designing a database application in client/server mode, it is often not sufficient to use the first two methods alone. Because the first two methods are used, the execution mode is transparent to the programmer and ODBC2. 0 There is a synchronous and asynchronous execution mode when accessing the database, so it is easy to design improperly, and the system deadlock occurs. Therefore, in the actual programming, we need to adopt a third approach to solve the problem caused by synchronous and asynchronous execution patterns.
2. Synchronous and asynchronous execution modes
ODBC2. 0 when accessing a database, there are synchronous execution modes and asynchronous execution patterns.
The so-called synchronous execution mode, refers to the statement in the synchronous execution mode, will always maintain the control of the program flow until the end of the program. For example, a query operation, where the application on the client is instructed to send a query to the server, waits until the server returns the query results to the client side before proceeding to the next step, as shown in Figure 1.
Figure 1 Synchronous execution mode
The asynchronous execution pattern refers to the order in which statements are executed in an asynchronous execution mode, not necessarily in the order in which the statement execution begins. For example, a query operation, when an application on a client sends a query operation to the server, executes the next statement of the query immediately, without waiting until the server returns the query results to the client side before proceeding to the next step. As shown in Figure 2.
Figure 2 Asynchronous execution mode
In some application development tools, while providing programming with data control items and database object variables, there is no good consideration for the important difference between synchronous execution mode and asynchronous execution mode. In order for the program to run faster, the default mode for its statement execution is asynchronous mode. For the average programmer, if he does not understand the synchronous execution mode and the asynchronous execution pattern, he will often post an action statement to the server (query or read a record, etc.), immediately after the server returned to the results of the execution, or the results of the next action; in asynchronous execution mode, Subsequent statements on the client are executed after the action statement is issued, but for a variety of reasons the server may not necessarily be able to execute the action statement and return the result to the client before the subsequent statement executes. Therefore, subsequent statements, when referencing the execution result of the previous action statement, often refer to the wrong value because the execution result does not exist, resulting in a system error or deadlock.
3. Solution
To address the problems mentioned above, the following two options can be adopted:
① use ODBC2. 0API, the statement execution status is set to synchronous execution mode. ODBC2. In 0API, function SQLSetStmtOption () is the function of setting synchronous or asynchronous execution mode. We can use the following statement to set the statement execution status to synchronous execution mode:
Iretcodeκsqlsetstmtoption (hstmt,sql-async-enable,0)
Where HSTMT is a valid statement handle, the constant sql-async-enable is the option to set, and parameter 0 indicates that the option (that is, asynchronous execution mode) closes. If iRetCode returns sql-success, the statement execution state has been set to synchronous execution mode.
② use ODBC2. 0API, set the statement execution state to asynchronous execution mode, and then continuously query whether an action statement has finished executing in the program.
ODBC2. The 0API has more than 20 functions that support asynchronous execution, as shown in the previous table.
When these functions are first called, the value sql-still-executing is returned, and the application continues to execute subsequent statements. After a while, you should call the original function again, and note that the real argument should pass in the same statement handle as the first call, and the other arguments should be the same (but ignored). If the function returns a value of sql-success, the statement is completed, or if the function returns sql-still-executing, the statement is still in execution.
We can use a simple example to illustrate the following:
iRetCodeΚSQLSetStmtOption(hStmt,SQL-ASYNC-ENABLE,1)
′置语句执行模式为异步执行模式
iRetCodeΚSQLExecDirect(hStmt,″SELECT*FROMemployees″,23)
......′执行其他操作
iRetCodeΚSQLExecDirect(hStmt,″SELECT*FROMemployees″,23)
′判断SQLExecDirect()是否已执行完毕
If(iRetCodeΚSQL-STILL-EXECUTING)Then
......′该语句未执行完,继续执行其他操作
Else
If(iRetCodeΚSQL-SUCCESS)Then
......′该语句已执行完,可对语句操作结果进行处理
EndIf
EndIf
Synchronous execution mode simplifies the complexity of programming and ODBC2. 0API not very familiar programmer, can not be too much to understand the more complex ODBC2. 0API, instead of using Data control items and database object variables to write applications, the development efficiency is greatly improved, but the program can not run faster than the asynchronous execution mode.
Although the asynchronous execution pattern is very complex in the process of programming, multitasking can be performed in parallel in this mode, which greatly improves the execution efficiency.
In compiling the application, we should divide the use of these two modes according to our own situation, so as to improve the safety and reliability of the program running, and improve the efficiency of program execution.