Oracle10g Pro C\C++ 编程例子

来源:互联网 发布:什么是网站源码 编辑:程序博客网 时间:2024/05/22 06:27
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <pthread.h>
#include <time.h>

#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<SCI;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

原创粉丝点击