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);
}
}
#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
- Oracle10g Pro C\C++ 编程例子
- Pro * C 简单例子
- Fedora13 安装 oracle10g 及pro*c 配置
- 使用PRO*C编程的一些说明和例子
- pro*c 编程学习
- pro*c编程视频教程
- Pro*C 程序编程
- pro*c编程基础
- Pro*c的简单例子
- Pro*C/C++ windows编程
- oracle PRO*C程序设计的一个例子
- pro*c初学2---简单多线程例子
- 编译pro*c 的makefile例子
- 关于使用PRO*C编程的一些简单说明和例子
- 关于使用PRO*C编程的一些简单说明和例子
- 关于使用PRO*C编程的一些简单说明和例子
- 关于使用PRO*C编程的一些简单说明和例子
- 关于使用PRO*C编程的一些简单说明和例子
- Java解释XML文件的小例子
- GDI+基础编程
- 日媒称中国攻岛威胁持续增高 日美应军演牵制-日美军演-钓鱼岛-攻岛
- PM俱乐部建设之旅3-第二次会议
- 利用状态图实现词法分析
- Oracle10g Pro C\C++ 编程例子
- 在iPhone安装OpenSSH和远程连接教程
- SVN的标准目录结构:trunk、branches、tags
- 钟表问题(常见面试笔试问题整理)
- js闭包
- 黑马程序员 Java代理类
- Android 数据库一次创建多个表
- 基于Apache的HttpClient进行HTTP网络访问
- Video Decode Acceleration Framework Reference