> # Threading

> The ODBC specification requires that all ODBC drivers must be thread-safe; that is, they must not fail when database requests are made on separate threads.

> ### Threading Architecture

> An ODBC driver can be based on one of the following architectures:

> - Not thread-safe. The ODBC driver should not be used in a multi-threaded environment.
> - Thread-impaired. The ODBC driver serializes all ODBC calls. All requests are handled one by one, without concurrent processing.
> - Thread per connection. The ODBC driver processes requests concurrently with statement handles that do not share the same connection handle; however requests on the same connection are serialized.
> - Fully threaded. All requests fully use the threaded model. The ODBC driver processes all requests on multiple statements concurrently.

> The ODBC driver supports multithreaded applications on all platforms.

> ### Cancelling Functions in Multithreaded Applications

> In a multithreaded application, the application can cancel a function that is running synchronously on a statement. To cancel the function, the application calls SQLCancel with the same statement handle as that used by the target function, but on a different thread. Whether SQLCancel actually cancels the running function depends on the data store being accessed as shown in Table 2-7.

> - OK means that SQLCancel can interrupt the running function.
> - Ignored means that SQLCancel will have no affect on the running function.

> In both cases, SQLCancel returns `SQL_SUCCESS`. If SQLCancel has been called from a different thread while there is a pending request, the original statement will return `SQL_ERROR` with the error message `Operation cancelled`.

> Table 2-7. Using SQLCancel in Multithreaded Applications
> Data Store | SQLCancel
> --- | ---
> DB2 UDB on Windows | OK
> DB2 UDB on Linux and UNIX | Ignored
> Informix | OK
> Microsoft SQL Server | OK
> Oracle on Windows | Ignored
> Oracle on Linux and UNIX | OK
> Sybase | OK