#include
#include
#include
#include
#include
#include
#include
#include
char USER[31];
char PASS_WORD[31];
char SERVERNAME[31];
unsigned int errora = 0;
unsigned int errorb = 0;
unsigned int sucess = 0;
void error_proc(dvoid *errhp, sword status)
{
text errbuf[512];
sb4 errcode;
switch (status)
{
case OCI_SUCCESS:
// printf("OCI_SUCCESS\n");
break;
case OCI_SUCCESS_WITH_INFO:
printf("OCI error: OCI_SUCCESS_WITH_INFO\n");
break;
case OCI_NEED_DATA:
printf("OCI error: OCI_NEED_DATA\n");
break;
case OCI_NO_DATA:
printf("OCI error: OCI_NO_DATA\n");
break;
case OCI_ERROR:
(void)OCIErrorGet((dvoid *)errhp,(ub4)1,NULL,&errcode,
errbuf,(ub4)sizeof(errbuf),OCI_HTYPE_ERROR);
printf("错误号:%d\n错误信息:%s\n",errcode,errbuf);
break;
case OCI_INVALID_HANDLE:
printf("OCI error: OCI_INVALID_HANDLE\n");
break;
case OCI_STILL_EXECUTING:
printf("OCI error: OCI_STILL_EXECUTING\n");
break;
default:
break;
}
}
void fun(){
// pthread_detach(pthread_self());
OCIEnv *envhp = NULL; //环境句柄
OCIError *errhp = NULL; //错误句柄
OCIServer *srvhp = NULL; //服务器句柄
OCISvcCtx *svchp = NULL; //服务环境句柄
OCIStmt *stmthp = NULL; //语句句柄
// OCIBind *bnd1p = (OCIBind *)0; //结合句柄
OCIDefine *defhp ;
sword status;
int sum;
char* SqlStat = "select count(*) from t_dbsync where rownum = 1";
strcpy(USER,"*");
strcpy(PASS_WORD,"*");
strcpy(SERVERNAME,"ip:port/database");
//创建环境句柄
OCIEnvCreate(&envhp, OCI_DEFAULT, (dvoid *)0,0,0,0,0, (dvoid **)0);
//申请错误句柄
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0);
//申请服务器句柄
error_proc(errhp, OCIHandleAlloc((dvoid *)envhp, (dvoid **)&srvhp, OCI_HTYPE_SERVER, (size_t)0, (dvoid **)0));
//申请服务环境句柄
error_proc(errhp, OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, (size_t)0, (dvoid **)0));
//设置服务环境的服务器属性
error_proc(errhp, OCIAttrSet((dvoid *)svchp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp, (ub4)0, OCI_ATTR_SERVER, (OCIError *)errhp));
printf("time[%lu]\n",time((time_t *)0));
int i = 0;
time_t begin_time = time((time_t *)0);
// while (1){
status = OCILogon(envhp,errhp,&svchp,USER,(ub4)strlen((char *)USER),
(text *)PASS_WORD,(ub4)strlen((char *)PASS_WORD),(text *)SERVERNAME,(ub4)strlen((char *)SERVERNAME));
if(status==OCI_SUCCESS) {
// printf("成功连接 %s 数据库,已为%s用户建立会话.\n",SERVERNAME,USER);
}
else{
printf("连接失败connect fail!\n");
printf("-----ORA_search,ERROR in OCILogon-----i:[%d]\n",i);
printf("time[%lu]\n",time((time_t *)0));
error_proc(errhp,status);
// errora++;
// continue;
}
error_proc(errhp, OCIHandleAlloc((dvoid *)envhp, (dvoid **)&stmthp, OCI_HTYPE_STMT, (size_t)0, (dvoid **)0));
error_proc(errhp,OCIStmtPrepare(stmthp,errhp,SqlStat,(ub4)strlen((char *)SqlStat),OCI_NTV_SYNTAX,OCI_DEFAULT));
error_proc(errhp, OCIDefineByPos(stmthp,&defhp,errhp,1,&sum, sizeof(sum),SQLT_INT,(sb2 *)0,0,(ub2 *)0,OCI_DEFAULT));
//执行SQL语句
while (1){
if ((status=OCIStmtExecute(svchp,stmthp,errhp,(ub4)0,
(ub4)0,(CONST OCISnapshot *)NULL, (OCISnapshot *)NULL,OCI_DEFAULT))
&& status != OCI_SUCCESS_WITH_INFO)
{ //失败则退出
error_proc(errhp,status);
printf("-----ORA_search,ERROR in OCIStmtExecute-----\n");
errorb++;
error_proc(errhp,OCILogoff(svchp,errhp));
continue;
}
error_proc(errhp, OCIStmtFetch(stmthp,errhp,1, OCI_FETCH_NEXT, OCI_DEFAULT));
sucess++;
if ( (time((time_t *)0)-begin_time) > 60){
break;
}
}
// printf("The sum is %d\n",sum);
//断开连接
error_proc(errhp,OCILogoff(svchp,errhp));
// sucess++;
// if ( (time((time_t *)0)-begin_time) > 60){
// break;
// }
//// printf("time[%lu]\n",time((time_t *)0));
// }
printf("error:[%u][%u],success:[%d]\n",errora,errorb,sucess);
fflush(stdout);
// pthread_exit(NULL);
}
int main(int argc,char *argv[]){
// signal(SIGPIPE,SIG_IGN);
int i = 0;
int max = atoi(argv[1]) - 1;
for (;i < max ;i++){
if (fork() == 0 ){
break;
}
}
fun();
while(1){
sleep(100);
}
return 0;
}
阅读(1943) | 评论(0) | 转发(0) |