访问www.tomcoding.com网站,学习Oracle内部数据结构,详细文档说明,下载Oracle的exp/imp,DUL,logminer,ASM工具的源代码,学习高技术含量的内容。
这一节我们用代码实现一个实际的例子,来演示连接池的用法。例子中我们仅使用单线程来演示用法,例子的步骤如下。
1. 创建一个OCI环境,分配错误句柄。
2. 创建一个连接池。
3. 从连接池中获取一个会话。
4. 通过这个会话从数据库中查询一个数字,SQL语句为select 10 from dual。
5. 释放会话。
6. 销毁连接池。
7. 释放OCI环境。
例子代码如下,关键处有详细注释。
#include "stdio.h"
#include "stdlib.h"
#include "stdint.h"
#include "memory.h"
#include "string.h"
#include "time.h"
#include "errno.h"
#include "oci.h"
/* global variable defination */
OCIEnv *envhp = NULL; /* env handle */
OCIError *errhp = NULL; /* error handle */
OCISvcCtx *svchp = NULL; /* service context handle */
OCICPool *poolhp = NULL; /* connection pool handle */
OCIAuthInfo *authp = NULL; /* authentication information handle */
OCIStmt *stmthp = NULL; /* statement handle */
text errbuf[512]; /* error message text */
struct {
char uname[32]; /* 认证用户名称 */
char upwd[32]; /* 认证用户密码 */
} inputs;
static void usage(const char *prg)
{
fprintf(stderr,
"Usage: %s user/password\n\n"
" user oracle user\n"
" password user password\n"
, prg
);
}
/* 解析命令行中的认证用户和密码 */
static int parse_inputs(int argc, char *argv[])
{
char *p;
if (argc < 2) {
usage(argv[0]);
return (-1);
}
if ((p=strchr(argv[1], '/')) == NULL) {
usage(argv[0]);
return (-1);
}
*p++ = '\0';
memset(&inputs, 0, sizeof(inputs));
strncpy(inputs.uname, argv[1], 31);
strncpy(inputs.upwd, p, 31);
return (0);
}
static char ora_env[][16] =
{
"ORACLE_HOME",
"ORACLE_SID",
""
};
/* 检查Oracle运行的环境变量 */
static int check_ora_env(void)
{

最低0.47元/天 解锁文章
943

被折叠的 条评论
为什么被折叠?



