oracle+pro+c+编程,Oracle10g Pro C\C++ 编程例子

#include #define NUM 254

#define SCI 100000000

void* thread_function(void *arg);

void* my_log(void *arg);

EXEC SQL INCLUDE sqlca;

EXEC SQL BEGIN DECLARE SECTION;

char dbuser[16],dbpass[16];

EXEC SQL END DECLARE SECTION;

void main()

{

int i,res=0;

pthread_t a_thread;

sprintf(dbuser,"admin_test");

sprintf(dbpass,"admin_test");

res = pthread_create(&a_thread,NULL,my_log,NULL);

if(res != 0)

{

perror("log creation failed");

exit(1);

}

for(i=1;i<=NUM;i++)

{

res = pthread_create(&a_thread,NULL,thread_function,(void*)&i);

if(res != 0)

{

perror("Thread creation failed");

exit(1);

}

printf("%dth thread created,thread_num=%d\n",i,a_thread);

usleep(20000);

}

sleep(1000000000);

}

void* thread_function(void *arg)

{

EXEC SQL BEGIN DECLARE SECTION;

sql_context context;

EXEC SQL END DECLARE SECTION;

struct sqlca sqlca;

pthread_t myid;

EXEC SQL ENABLE THREADS;

EXEC SQL CONTEXT ALLOCATE :context;

EXEC SQL CONTEXT USE :context;

sprintf(dbuser,"admin_test");

sprintf(dbpass,"admin_test");

pthread_detach(pthread_self());

myid=pthread_self();

int i;

int t_number=*(int*)arg;

while(1)

{

EXEC SQL CONNECT :dbuser IDENTIFIED BY :dbpass;

if (sqlca.sqlcode<0)

{

printf("%dth thread :%lu:connect to database faild,ORA-%d,err:%d\n",t_number,myid,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);

exit(-2);

}

else

{

printf("%dth thread :%lu:connect to database success\n",t_number,myid);

break;

}

}

for(i=0;i{

printf("%lu:%d:",myid,i);

EXEC SQL CONTEXT USE :context;

EXEC SQL INSERT INTO T_ONLINEHISTORYALL VALUES(

OnlineHistoryAll_Seq.nextval,

'test content',

to_char(sysdate,'yyyymmddhh24miss'));

printf("%dth thread :%lu:after insert,ORA-%d\n",t_number,myid,sqlca.sqlcode);

if(sqlca.sqlcode==0)

printf("%dth thread :%lu:insert ok!\n",t_number,myid);

if(sqlca.sqlcode<0)

{

EXEC SQL ROLLBACK;

printf("%dth thread :%lu:rollback!\n",t_number,myid);

}

EXEC SQL COMMIT;

printf("%dth thread :%lu:after commit,ORA-%d\n",t_number,myid,sqlca.sqlcode);

usleep(500000);

if(sqlca.sqlcode==0)

printf("%dth thread :%lu:commit ok!\n",t_number,myid);

else

{

printf("%lu:aaa\n",myid);

EXEC SQL COMMIT WORK RELEASE;

printf("%lu:bbb\n",myid);

sleep(2);

EXEC SQL CONNECT :dbuser IDENTIFIED BY :dbpass;

printf("%lu:ccc\n",myid);

}

}

EXEC SQL CONTEXT FREE :context;

}

void* my_log(void *arg)

{

pthread_detach(pthread_self());

FILE *f;

time_t rawtime;

struct tm* timeinfo;

char *time_str;

time(&rawtime);

117,1-8       88%

77,1-8        61%

timeinfo = localtime(&rawtime);

time_str = asctime(timeinfo);

if((f=fopen("my.log","w+"))==NULL)

printf("new log created ok!!\n");

while(1)

{

time(&rawtime);

timeinfo = localtime(&rawtime);

fprintf(f,"%s\n",asctime(timeinfo));

sleep(1);

}

}

配合oracle10g应该修改processes参数

sqlplus /nolog

conn /as sysdba

show parameter process

alter system set processes=1000 scope=spfile

shutdown abort

startup

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值