database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ㆍALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
ALTIBASE Devel 18 게시물 읽기
 News | Q&A | Columns | Tutorials | Devel | Files | Links
No. 18
[SQLCLI] 멀티쓰레드 사용 예제
작성자
최한열(검은호랑이)
작성일
2005-04-21 14:47
조회수
10,305

#include <sqlcli.h>

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

 

#define THR_NUM 10

 

/********************************************************

NAME:

main

 

ARGUMENT:

int argc

char** argv

 

DISCRIPTION:

Main Fuction

 

*********************************************************/

 

void thr_run(void *thr_num_);

 

int main(int argc, char** argv)

{

int i;

int thr_num[THR_NUM];

pthread_t tid[THR_NUM];

 

for (i=0; i<THR_NUM; i++)

{

thr_num[i] = i;

if ( 0 != pthread_create( &tid[i],

(pthread_attr_t *)NULL,

 

(void*(*)(void *))thr_run,

(void *)&thr_num[i]) )

{

fprintf(stderr,"Create thread[%d]

Error ...\n", i);

exit(-1);

}

}

 

for (i=0; i<THR_NUM; i++)

{

if ( 0 != pthread_join(tid[i], (void **)NULL) )

{

fprintf(stderr,"Join thread[%d]

Error ...\n", i);

}

printf("Join [%d]\n",i);

}

 

fprintf(stderr,"main() terminated ...\n");

exit(0);

}

 

/********************************************************

NAME:

connect_db

 

ARGUMENT:

SQLHDBC dbc - Handle of DBC

 

DESCRIPTION:

Connect to DB with dbc

 

*********************************************************/

 

SQLRETURN connect_db(SQLHDBC dbc, int thr_num_)

{

char connStr[1024];

SQLRETURN ret;

 

sprintf(connStr, "UID=SYS;PWD=MANAGER;CONNTYPE=2");

 

ret = SQLDriverConnect( dbc, NULL, connStr, SQL_NTS,

NULL, 0, NULL,

SQL_DRIVER_NOPROMPT );

 

if ( ret == SQL_ERROR )

{

fprintf(stderr,"[THR%d] Connection Error ...\n",

thr_num_);

SQLFreeConnect(dbc);

return ret;

}

 

return ret;

}

 

/********************************************************

NAME:

disconnect_db

 

ARGUMENT:

SQLHDBC dbc - Handle of DBC

 

DISCRIPTION:

Disconnect from DB and Free handle of DBC

 

*********************************************************/

 

void disconnect_db(SQLHDBC dbc)

{

if (SQL_ERROR == SQLDisconnect(dbc)) {

fprintf(stderr,"Disconnect Error ...\n");

}

 

SQLFreeConnect(dbc);

}

 

/********************************************************

NAME:

execute_err

 

ARGUMENT:

SQLHSTMT hdbc - Handle of Connection

SQLHSTMT stmt - Handle of Statement

const char *q - Query string

 

DISCRIPTION:

Print Error num & msg to stderr

 

*********************************************************/

 

void execute_err(SQLHDBC hdbc, SQLHSTMT stmt, const char* q)

{

fprintf(stderr, "##### Error : %s \n",q);

 

SQLINTEGER errNo;

SQLSMALLINT msgLength;

SQLCHAR errMsg[1024];

 

if (SQL_SUCCESS == SQLError ( SQL_NULL_HENV, hdbc, stmt,

NULL, &errNo,

errMsg, 1024,

&msgLength ))

{

fprintf(stderr, "##### Error : # %ld, %s \n",

errNo, errMsg);

}

}

 

/********************************************************

NAME:

thr_run

 

ARGUMENT:

void * thr_num_

 

DISCRIPTION:

Thread function.

Make Connection and Executin query

then Disconnect.

 

*********************************************************/

 

void thr_run(void *thr_num_)

{

SQLHENV henv = SQL_NULL_HENV;

SQLHDBC hdbc = SQL_NULL_HDBC;

SQLHSTMT stmt = SQL_NULL_HSTMT;

char query[1024];

int value;

int len;

int thr_num = *(int *)thr_num_;

 

/* 에러 발생시 저장 공간 할당 */

ideAllocErrorSpace();

 

if (SQL_ERROR == SQLAllocEnv(&henv))

{

fprintf(stderr,"SQLAllocEnv Error ...\n");

pthread_exit((void *)NULL);

}

fprintf(stdout,"SQLAllocEnv Success ...\n");

if (SQL_ERROR == SQLAllocConnect(henv, &hdbc))

{

fprintf(stderr,"[THR%d] SQLAllocConnect Error ...\n",

thr_num);

pthread_exit((void *)NULL);

}

fprintf(stdout,"[THR%d] SQLAllocConnect Success ...\n",

thr_num);

 

if (SQL_ERROR == connect_db(hdbc, thr_num))

{

SQLFreeConnect(hdbc);

pthread_exit((void *)NULL);

}

fprintf(stderr,"[THR%d] Connect Success ...\n", thr_num);

 

if (SQL_ERROR == SQLAllocStmt(hdbc, &stmt))

{

fprintf(stderr,"[THR%d] SQLAllocStmt Error ...\n",

thr_num);

goto exit_lb;

}

fprintf(stderr,"[THR%d] SQLAllocStmt Success ...\n",

thr_num);

 

sprintf(query,"DROP TABLE THR_%d",thr_num);

if (SQL_ERROR == SQLExecDirect(stmt, query, SQL_NTS))

{

execute_err(hdbc, stmt, query);

}

 

sprintf(query,"CREATE TABLE THR_%d (I1 NUMBER

(10))",thr_num);

if (SQL_ERROR == SQLExecDirect(stmt, query, SQL_NTS))

{

execute_err(hdbc, stmt, query);

goto exit_lb;

}

fprintf(stdout,"[THR%d] %s Success ...\n", thr_num, query);

 

sprintf(query,"INSERT INTO THR_%d VALUES(?)",thr_num);

if (SQL_ERROR == SQLPrepare(stmt, query, SQL_NTS))

{

execute_err(hdbc, stmt, query);

goto exit_lb;

}

 

if (SQL_ERROR == SQLBindParameter(stmt, 1,

SQL_PARAM_INPUT, ISP_C_SINT,

ISP_NUMERIC, 0, 0, &value,

0, &len))

{

execute_err(hdbc, stmt, "SQLBindParameter");

goto exit_lb;

}

 

for (value=0; value<5; value++)

{

if (SQL_ERROR == SQLExecute(stmt))

{

execute_err(hdbc, stmt, query);

goto exit_lb;

}

fprintf(stdout,"[THR%d] %s (?=%d) Success ...\n",

thr_num, query, value);

}

 

exit_lb:

if (stmt != NULL)

{

SQLFreeStmt(stmt, SQL_DROP);

}

 

SQLDisconnect(hdbc);

 

if ( hdbc != NULL)

{

SQLFreeConnect(hdbc);

}

if ( henv != NULL)

{

SQLFreeEnv(henv);

}

pthread_exit((void *)NULL);

}

 

[Top]
No.
제목
작성자
작성일
조회
18[SQLCLI] 멀티쓰레드 사용 예제
최한열
2005-04-21
10305
17tmax에서 간단하게 altibase쓰기.
최한열
2005-04-21
9322
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2023 DSN, All rights reserved.
작업시간: 0.052초, 이곳 서비스는
	PostgreSQL v16.1로 자료를 관리합니다