在多个类中声明SQLCA时,要用以下两种方式之一,否则的话,在程序链接的时候出错
A.
#define SQLCA_NONE
#include "sqlca.h"
这种方式没有定义sqlca 全局变量,需要自己定义一个sqlca ,建议为每个sql_context定义一个局部的sqlca;
void ClassA::FuncA()
{
struct sqlca sqlca;
EXEC SQL WHENEVER SQLERROR GOTO SQLERR;
EXEC SQL WHENEVER NOT FOUND CONTINUE;
EXEC SQL CONTEXT USE :m_ctx;
EXEC SQL select * from tableName;
//process data
return ;
SQLERR:
SqlError(sqlca);
return -1;
}
B.
#define SQLCA_STORAGE_CLASS extern
#include "sqlca.h"
此种方式会有一个全局的sqlca区,建议为每个sql_context定义一个同名的局部的sqlca覆盖,以保证sqlca 是多线程安全的.
附sqlca定义
//*****************start sqlca.h**********************
#ifndef SQLCA
#define SQLCA 1
struct sqlca
{
/* ub1 */ char sqlcaid[8];
/* b4 */ int sqlabc;
/* b4 */ int sqlcode;
struct
{
/* ub2 */ unsigned short sqlerrml;
/* ub1 */ char sqlerrmc[70];
} sqlerrm;
/* ub1 */ char sqlerrp[8];
/* b4 */ int sqlerrd[6];
/* ub1 */ char sqlwarn[8];
/* ub1 */ char sqlext[8];
};
#ifndef SQLCA_NONE
#ifdef SQLCA_STORAGE_CLASS
SQLCA_STORAGE_CLASS struct sqlca sqlca
#else
struct sqlca sqlca
#endif
#ifdef SQLCA_INIT
= {
{'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '},
sizeof(struct sqlca),
0,
{ 0, {0}},
{'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
}
#endif
;
#endif
#endif
//*****************end sqlca.h*****************************************
11.4.4 Programming Considerations
While Oracle ensures that the SQLLIB code is thread-safe, you are responsible for ensuring that your Pro*C/C++ source code is designed to work properly with threads; for example, carefully consider your use of static and global variables.
In addition, multithreaded applications require design decisions regarding the following:
Declaring the SQLCA as a thread-safe struct, typically an auto variable and one for each runtime context
Declaring the SQLDA as a thread-safe struct, like the SQLCA, typically an auto variable and one for each runtime context
Declaring host variables in a thread-safe fashion, in other words, carefully consider your use of static and global host variables.
Avoiding simultaneous use of a runtime context in multiple threads
Whether or not to use default database connections or to explicitly define them using the AT clause
Also, no more than one executable embedded SQL statement, for example, EXEC SQL UPDATE, may be outstanding on a runtime context at a given time.
Existing requirements for precompiled applications also apply. For example, all references to a given cursor must appear in the same source
A.
#define SQLCA_NONE
#include "sqlca.h"
这种方式没有定义sqlca 全局变量,需要自己定义一个sqlca ,建议为每个sql_context定义一个局部的sqlca;
void ClassA::FuncA()
{
struct sqlca sqlca;
EXEC SQL WHENEVER SQLERROR GOTO SQLERR;
EXEC SQL WHENEVER NOT FOUND CONTINUE;
EXEC SQL CONTEXT USE :m_ctx;
EXEC SQL select * from tableName;
//process data
return ;
SQLERR:
SqlError(sqlca);
return -1;
}
B.
#define SQLCA_STORAGE_CLASS extern
#include "sqlca.h"
此种方式会有一个全局的sqlca区,建议为每个sql_context定义一个同名的局部的sqlca覆盖,以保证sqlca 是多线程安全的.
附sqlca定义
//*****************start sqlca.h**********************
#ifndef SQLCA
#define SQLCA 1
struct sqlca
{
/* ub1 */ char sqlcaid[8];
/* b4 */ int sqlabc;
/* b4 */ int sqlcode;
struct
{
/* ub2 */ unsigned short sqlerrml;
/* ub1 */ char sqlerrmc[70];
} sqlerrm;
/* ub1 */ char sqlerrp[8];
/* b4 */ int sqlerrd[6];
/* ub1 */ char sqlwarn[8];
/* ub1 */ char sqlext[8];
};
#ifndef SQLCA_NONE
#ifdef SQLCA_STORAGE_CLASS
SQLCA_STORAGE_CLASS struct sqlca sqlca
#else
struct sqlca sqlca
#endif
#ifdef SQLCA_INIT
= {
{'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '},
sizeof(struct sqlca),
0,
{ 0, {0}},
{'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
}
#endif
;
#endif
#endif
//*****************end sqlca.h*****************************************
11.4.4 Programming Considerations
While Oracle ensures that the SQLLIB code is thread-safe, you are responsible for ensuring that your Pro*C/C++ source code is designed to work properly with threads; for example, carefully consider your use of static and global variables.
In addition, multithreaded applications require design decisions regarding the following:
Declaring the SQLCA as a thread-safe struct, typically an auto variable and one for each runtime context
Declaring the SQLDA as a thread-safe struct, like the SQLCA, typically an auto variable and one for each runtime context
Declaring host variables in a thread-safe fashion, in other words, carefully consider your use of static and global host variables.
Avoiding simultaneous use of a runtime context in multiple threads
Whether or not to use default database connections or to explicitly define them using the AT clause
Also, no more than one executable embedded SQL statement, for example, EXEC SQL UPDATE, may be outstanding on a runtime context at a given time.
Existing requirements for precompiled applications also apply. For example, all references to a given cursor must appear in the same source