Recommendations for using multiple threads

Multithreaded applications might be difficult to maintain and use if you do not carefully plan the application in advance. When you are writing a multithreaded application, you must consider how you handle data structures.
Follow these guidelines when accessing a database from multiple thread applications:
Serialize alteration of data structures.
Applications must ensure that user-defined data structures used by SQL statements and database manager routines are not altered by one thread while an SQL statement or database manager routine is being processed in another thread. For example, do not allow a thread to reallocate an SQLDA while it is being used by an SQL statement in another thread.
Consider using separate data structures.
It may be easier to give each thread its own user-defined data structures to avoid having to serialize their usage. This guideline is especially true for the SQLCA, which is used not only by every executable SQL statement, but also by all of the database manager routines. There are three alternatives for avoiding this problem with the SQLCA:
  • Use EXEC SQL INCLUDE SQLCA, but add struct sqlca sqlca at the beginning of any routine that is used by any thread other than the first thread.
  • Place EXEC SQL INCLUDE SQLCA inside each routine that contains SQL, instead of in the global scope.
  • Replace EXEC SQL INCLUDE SQLCA with #include "sqlca.h", then add "struct sqlca sqlca" at the beginning of any routine that uses SQL.