典型应用场景配置
日志诊断场景
ODBC日志分为unixODBC驱动管理器日志和gsqlODBC驱动端日志。前者可以用于追溯应用程序API的执行是否成功,后者是底层实现过程中的一些DFX日志,用来帮助定位问题。
unixODBC日志需要在odbcinst.ini文件中配置:
1 2 3 4 5 6 7 |
[ODBC] Trace=Yes TraceFile=/path/to/odbctrace.log [GaussMPP] Driver64=/usr/local/lib/gsqlodbcw.so setup=/usr/local/lib/gsqlodbcw.so |
gsqlODBC日志只需要在odbc.ini加上如下内容:
[gaussdb] Driver=GaussMPP Servername=10.10.0.13 #数据库Server IP ... Debug=1 #打开驱动端debug日志
unixODBC日志将会生成在TraceFile配置的路径下,gsqlODBC会在系统/tmp/下生成mylog_xxx.log。
高性能场景
进行大量数据插入时,建议如下:
- 需要设置批量绑定:odbc.ini配置文件中设置UseBatchProtocol=1、数据库设置support_batch_bind=on。
- ODBC程序绑定类型要和数据库中类型一致。
- 客户端字符集和数据库字符集一致。
- 事务改成手动提交模式。
odbc.ini配置文件:
[gaussdb] Driver=GaussMPP Servername=10.10.0.13 #数据库Server IP ... UseBatchProtocol=1 #默认打开 ConnSettings=set client_encoding=UTF8 #设置客户端字符编码,保证和server端一致
#ifdef WIN32 #include <windows.h> #endif #include <stdio.h> #include <stdlib.h> #include <sql.h> #include <sqlext.h> #include <string.h> #include <sys/time.h> #define MESSAGE_BUFFER_LEN 128 SQLHANDLE h_env = NULL; SQLHANDLE h_conn = NULL; SQLHANDLE h_stmt = NULL; void print_error() { SQLCHAR Sqlstate[SQL_SQLSTATE_SIZE+1]; SQLINTEGER NativeError; SQLCHAR MessageText[MESSAGE_BUFFER_LEN]; SQLSMALLINT TextLength; SQLRETURN ret = SQL_ERROR; ret = SQLGetDiagRec(SQL_HANDLE_STMT, h_stmt, 1, Sqlstate, &NativeError, MessageText, MESSAGE_BUFFER_LEN, &TextLength); if ( SQL_SUCCESS == ret) { printf("\n STMT ERROR-%05d %s", NativeError, MessageText); return; } ret = SQLGetDiagRec(SQL_HANDLE_DBC, h_conn, 1, Sqlstate, &NativeError, MessageText, MESSAGE_BUFFER_LEN, &TextLength); if ( SQL_SUCCESS == ret) { printf("\n CONN ERROR-%05d %s", NativeError, MessageText); return; } ret = SQLGetDiagRec(SQL_HANDLE_ENV, h_env, 1, Sqlstate, &NativeError, MessageText, MESSAGE_BUFFER_LEN, &TextLength); if ( SQL_SUCCESS == ret) { printf("\n ENV ERROR-%05d %s", NativeError, MessageText); return; } return; } /* 期盼函数返回SQL_SUCCESS */ #define RETURN_IF_NOT_SUCCESS(func) \ {\ SQLRETURN ret_value = (func);\ if (SQL_SUCCESS != ret_value)\ {\ print_error();\ printf("\n failed line = %u: expect SQL_SUCCESS, but ret = %d", __LINE__, ret_value);\ return SQL_ERROR; \ }\ } /* 期盼函数返回SQL_SUCCESS */ #define RETURN_IF_NOT_SUCCESS_I(i, func) \ {\ SQLRETURN ret_value = (func);\ if (SQL_SUCCESS != ret_value)\ {\ print_error();\ printf("\n failed line = %u (i=%d): : expect SQL_SUCCESS, but ret = %d", __LINE__, (i), ret_value);\ return SQL_ERROR; \ }\ } /* 期盼函数返回SQL_SUCCESS_WITH_INFO */ #define RETURN_IF_NOT_SUCCESS_INFO(func) \ {\ SQLRETURN ret_value = (func);\ if (SQL_SUCCESS_WITH_INFO != ret_value)\ {\ print_error();\ printf("\n failed line = %u: expect SQL_SUCCESS_WITH_INFO, but ret = %d", __LINE__, ret_value);\ return SQL_ERROR; \ }\ } /* 期盼数值相等 */ #define RETURN_IF_NOT(expect, value) \ if ((expect) != (value))\ {\ printf("\n failed line = %u: expect = %u, but value = %u", __LINE__, (expect), (value)); \ return SQL_ERROR;\ } /* 期盼字符串相同 */ #define RETURN_IF_NOT_STRCMP_I(i, expect, value) \ if (( NULL == (expect) ) || (NULL == (value)))\ {\ printf("\n failed line = %u (i=%u): input NULL pointer !", __LINE__, (i)); \ return SQL_ERROR; \ }\ else if (0 != strcmp((expect), (value)))\ {\ printf("\n failed line = %u (i=%u): expect = %s, but value = %s", __LINE__, (i), (expect), (value)); \ return SQL_ERROR;\ } // prepare + execute SQL语句 int execute_cmd(SQLCHAR *sql) { if ( NULL == sql ) { return SQL_ERROR; } if ( SQL_SUCCESS != SQLPrepare(h_stmt, sql, SQL_NTS)) { return SQL_ERROR; } if ( SQL_SUCCESS != SQLExecute(h_stmt)) { return SQL_ERROR; } return SQL_SUCCESS; } // execute + commit 句柄 int commit_exec() { if ( SQL_SUCCESS != SQLExecute(h_stmt)) { return SQL_ERROR; } // 手动提交 if ( SQL_SUCCESS != SQLEndTran(SQL_HANDLE_DBC, h_conn, SQL_COMMIT)) { return SQL_ERROR; } return SQL_SUCCESS; } int begin_unit_test() { SQLINTEGER ret; /* 申请环境句柄 */ ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &h_env); if ((SQL_SUCCESS != ret) && (SQL_SUCCESS_WITH_INFO != ret)) { printf("\n begin_unit_test::SQLAllocHandle SQL_HANDLE_ENV failed ! ret = %d", ret); return SQL_ERROR; } /* 进行连接前必须要先设置版本号 */ if (SQL_SUCCESS != SQLSetEnvAttr(h_env, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0)) { print_error(); printf("\n begin_unit_test::SQLSetEnvAttr SQL_ATTR_ODBC_VERSION failed ! ret = %d", ret); SQLFreeHandle(SQL_HANDLE_ENV, h_env); return SQL_ERROR; } /* 申请连接句柄 */ ret = SQLAllocHandle(SQL_HANDLE_DBC, h_env, &h_conn); if (SQL_SUCCESS != ret) { print_error(); printf("\n begin_unit_test::SQLAllocHandle SQL_HANDLE_DBC failed ! ret = %d", ret); SQLFreeHandle(SQL_HANDLE_ENV, h_env); return SQL_ERROR; } /* 建立连接 */ ret = SQLConnect(h_conn, (SQLCHAR*) "gaussdb", SQL_NTS, (SQLCHAR*) NULL, 0, NULL, 0); if (SQL_SUCCESS != ret) { print_error(); printf("\n begin_unit_test::SQLConnect failed ! ret = %d", ret); SQLFreeHandle(SQL_HANDLE_DBC, h_conn); SQLFreeHandle(SQL_HANDLE_ENV, h_env); return SQL_ERROR; } /* 申请语句句柄 */ ret = SQLAllocHandle(SQL_HANDLE_STMT, h_conn, &h_stmt); if (SQL_SUCCESS != ret) { print_error(); printf("\n begin_unit_test::SQLAllocHandle SQL_HANDLE_STMT failed ! ret = %d", ret); SQLFreeHandle(SQL_HANDLE_DBC, h_conn); SQLFreeHandle(SQL_HANDLE_ENV, h_env); return SQL_ERROR; } return SQL_SUCCESS; } void end_unit_test() { /* 释放语句句柄 */ if (NULL != h_stmt) { SQLFreeHandle(SQL_HANDLE_STMT, h_stmt); } /* 释放连接句柄 */ if (NULL != h_conn) { SQLDisconnect(h_conn); SQLFreeHandle(SQL_HANDLE_DBC, h_conn); } /* 释放环境句柄 */ if (NULL != h_env) { SQLFreeHandle(SQL_HANDLE_ENV, h_env); } return; } int main() { // begin test if (begin_unit_test() != SQL_SUCCESS) { printf("\n begin_test_unit failed."); return SQL_ERROR; } // 句柄配置同前面用例 int i = 0; SQLCHAR* sql_drop = "drop table if exists test_bindnumber_001"; SQLCHAR* sql_create = "create table test_bindnumber_001(" "f4 number, f5 number(10, 2)" ")"; SQLCHAR* sql_insert = "insert into test_bindnumber_001 values(?, ?)"; SQLCHAR* sql_select = "select * from test_bindnumber_001"; SQLLEN RowCount; SQL_NUMERIC_STRUCT st_number; SQLCHAR getValue[2][MESSAGE_BUFFER_LEN]; /* step 1. 建表 */ RETURN_IF_NOT_SUCCESS(execute_cmd(sql_drop)); RETURN_IF_NOT_SUCCESS(execute_cmd(sql_create)); /* step 2.1 通过SQL_NUMERIC_STRUCT结构绑定参数 */ RETURN_IF_NOT_SUCCESS(SQLPrepare(h_stmt, sql_insert, SQL_NTS)); //第一行: 1234.5678 memset(st_number.val, 0, SQL_MAX_NUMERIC_LEN); st_number.precision = 8; st_number.scale = 4; st_number.sign = 1; st_number.val[0] = 0x4E; st_number.val[1] = 0x61; st_number.val[2] = 0xBC; RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_NUMERIC, SQL_NUMERIC, sizeof(SQL_NUMERIC_STRUCT), 4, &st_number, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_NUMERIC, SQL_NUMERIC, sizeof(SQL_NUMERIC_STRUCT), 4, &st_number, 0, NULL)); // 关闭自动提交 SQLSetConnectAttr(h_conn, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_OFF, 0); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); //第二行: 12345678 memset(st_number.val, 0, SQL_MAX_NUMERIC_LEN); st_number.precision = 8; st_number.scale = 0; st_number.sign = 1; st_number.val[0] = 0x4E; st_number.val[1] = 0x61; st_number.val[2] = 0xBC; RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_NUMERIC, SQL_NUMERIC, sizeof(SQL_NUMERIC_STRUCT), 0, &st_number, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_NUMERIC, SQL_NUMERIC, sizeof(SQL_NUMERIC_STRUCT), 0, &st_number, 0, NULL)); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); //第三行: 12345678 memset(st_number.val, 0, SQL_MAX_NUMERIC_LEN); st_number.precision = 0; st_number.scale = 4; st_number.sign = 1; st_number.val[0] = 0x4E; st_number.val[1] = 0x61; st_number.val[2] = 0xBC; RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_NUMERIC, SQL_NUMERIC, sizeof(SQL_NUMERIC_STRUCT), 4, &st_number, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_NUMERIC, SQL_NUMERIC, sizeof(SQL_NUMERIC_STRUCT), 4, &st_number, 0, NULL)); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); /* step 2.2 第四行通过SQL_C_CHAR字符串绑定参数 */ RETURN_IF_NOT_SUCCESS(SQLPrepare(h_stmt, sql_insert, SQL_NTS)); SQLCHAR* szNumber = "1234.5678"; RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_NUMERIC, strlen(szNumber), 0, szNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_NUMERIC, strlen(szNumber), 0, szNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); /* step 2.3 第五行通过SQL_C_FLOAT绑定参数 */ RETURN_IF_NOT_SUCCESS(SQLPrepare(h_stmt, sql_insert, SQL_NTS)); SQLREAL fNumber = 1234.5678; RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_NUMERIC, sizeof(fNumber), 4, &fNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_NUMERIC, sizeof(fNumber), 4, &fNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); /* step 2.4 第六行通过SQL_C_DOUBLE绑定参数 */ RETURN_IF_NOT_SUCCESS(SQLPrepare(h_stmt, sql_insert, SQL_NTS)); SQLDOUBLE dNumber = 1234.5678; RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_NUMERIC, sizeof(dNumber), 4, &dNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_NUMERIC, sizeof(dNumber), 4, &dNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); SQLBIGINT bNumber1 = 0xFFFFFFFFFFFFFFFF; SQLBIGINT bNumber2 = 12345; /* step 2.5 第七行通过SQL_C_SBIGINT绑定参数 */ RETURN_IF_NOT_SUCCESS(SQLPrepare(h_stmt, sql_insert, SQL_NTS)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_NUMERIC, sizeof(bNumber1), 4, &bNumber1, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_NUMERIC, sizeof(bNumber2), 4, &bNumber2, 0, NULL)); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); /* step 2.6 第八行通过SQL_C_UBIGINT绑定参数 */ RETURN_IF_NOT_SUCCESS(SQLPrepare(h_stmt, sql_insert, SQL_NTS)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_UBIGINT, SQL_NUMERIC, sizeof(bNumber1), 4, &bNumber1, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_UBIGINT, SQL_NUMERIC, sizeof(bNumber2), 4, &bNumber2, 0, NULL)); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); SQLLEN lNumber1 = 0xFFFFFFFFFFFFFFFF; SQLLEN lNumber2 = 12345; /* step 2.7 第九行通过SQL_C_LONG绑定参数 */ RETURN_IF_NOT_SUCCESS(SQLPrepare(h_stmt, sql_insert, SQL_NTS)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_NUMERIC, sizeof(lNumber1), 0, &lNumber1, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_LONG, SQL_NUMERIC, sizeof(lNumber2), 0, &lNumber2, 0, NULL)); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); /* step 2.8 第十行通过SQL_C_ULONG绑定参数 */ RETURN_IF_NOT_SUCCESS(SQLPrepare(h_stmt, sql_insert, SQL_NTS)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_NUMERIC, sizeof(lNumber1), 0, &lNumber1, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_NUMERIC, sizeof(lNumber2), 0, &lNumber2, 0, NULL)); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); SQLSMALLINT sNumber = 0xFFFF; /* step 2.9 第十一行通过SQL_C_SHORT绑定参数 */ RETURN_IF_NOT_SUCCESS(SQLPrepare(h_stmt, sql_insert, SQL_NTS)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_SHORT, SQL_NUMERIC, sizeof(sNumber), 0, &sNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_SHORT, SQL_NUMERIC, sizeof(sNumber), 0, &sNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); /* step 2.10 第十二行通过SQL_C_USHORT绑定参数 */ RETURN_IF_NOT_SUCCESS(SQLPrepare(h_stmt, sql_insert, SQL_NTS)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_USHORT, SQL_NUMERIC, sizeof(sNumber), 0, &sNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_USHORT, SQL_NUMERIC, sizeof(sNumber), 0, &sNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); SQLCHAR cNumber = 0xFF; /* step 2.11 第十三行通过SQL_C_TINYINT绑定参数 */ RETURN_IF_NOT_SUCCESS(SQLPrepare(h_stmt, sql_insert, SQL_NTS)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_TINYINT, SQL_NUMERIC, sizeof(cNumber), 0, &cNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_TINYINT, SQL_NUMERIC, sizeof(cNumber), 0, &cNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); /* step 2.12 第十四行通过SQL_C_UTINYINT绑定参数 */ RETURN_IF_NOT_SUCCESS(SQLPrepare(h_stmt, sql_insert, SQL_NTS)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 1, SQL_PARAM_INPUT, SQL_C_UTINYINT, SQL_NUMERIC, sizeof(cNumber), 0, &cNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(SQLBindParameter(h_stmt, 2, SQL_PARAM_INPUT, SQL_C_UTINYINT, SQL_NUMERIC, sizeof(cNumber), 0, &cNumber, 0, NULL)); RETURN_IF_NOT_SUCCESS(commit_exec()); RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(1, RowCount); /* 用字符串类型统一进行期盼 */ SQLCHAR* expectValue[14][2] = {{"1234.5678", "1234.57"}, {"12345678", "12345678"}, {"0", "0"}, {"1234.5678", "1234.57"}, {"1234.5677", "1234.57"}, {"1234.5678", "1234.57"}, {"-1", "12345"}, {"18446744073709551615", "12345"}, {"-1", "12345"}, {"4294967295", "12345"}, {"-1", "-1"}, {"65535", "65535"}, {"-1", "-1"}, {"255", "255"}, }; RETURN_IF_NOT_SUCCESS(execute_cmd(sql_select)); while ( SQL_NO_DATA != SQLFetch(h_stmt)) { RETURN_IF_NOT_SUCCESS_I(i, SQLGetData(h_stmt, 1, SQL_C_CHAR, &getValue[0], MESSAGE_BUFFER_LEN, NULL)); RETURN_IF_NOT_SUCCESS_I(i, SQLGetData(h_stmt, 2, SQL_C_CHAR, &getValue[1], MESSAGE_BUFFER_LEN, NULL)); //RETURN_IF_NOT_STRCMP_I(i, expectValue[i][0], getValue[0]); //RETURN_IF_NOT_STRCMP_I(i, expectValue[i][1], getValue[1]); i++; } RETURN_IF_NOT_SUCCESS(SQLRowCount(h_stmt, &RowCount)); RETURN_IF_NOT(i, RowCount); SQLCloseCursor(h_stmt); /* step final. 删除表还原环境 */ RETURN_IF_NOT_SUCCESS(execute_cmd(sql_drop)); end_unit_test(); }
上述用例中定义了number列,调用SQLBindParameter接口时,绑定SQL_NUMERIC会比SQL_LONG性能更高。因为如果是char,在数据库服务端插入数据时需要进行数据类型转换,从而引发性能瓶颈。
主备切换自动寻主
示例场景
数据库实例配备一主多备DN时,将所有DN的IP全部写入配置文件中,ODBC将会自动寻找主DN,并与之建连。当发生主备切换时,ODBC也可与新的主DN建连。
连接池场景
连接池允许应用程序重复使用预先建立的连接,无需每次都重新建立连接。连接一旦创建并放入连接池,应用程序即可重复利用,避免了重复执行完整的连接过程。
这种池化连接的使用可以显著提升性能,尤其对于需要频繁建立和断开连接的中间层应用程序或网络连接的应用程序而言,性能提升尤为明显。
除了性能优势外,连接池架构还能够实现环境中的连接在单个进程中被多个组件共享的目的。这意味着,同一进程中的不同组件可以在不互相干扰的情况下共享连接池中的连接,进一步提高了系统的效率和资源利用率。
在连接池中,打开的连接可能被多个用户重用,如果您的应用程序脚本会更改数据库连接的状态,可能会导致数据泄露,为了安全起见,请谨慎评估使用连接池。
Linux场景配置
在odbcinst.ini配置文件中开启连接池,连接池相关参考配置如下:
[ODBC] Pooling=Yes #开启连接池 [GaussMPP] CPTimeout=60 #一个连接在连接池中未被重用则会被释放的计时,默认为0,开启连接池需要设置为大于0 CPTimeToLive=60 #该驱动下连接池存在的计时 [GaussMPP2] CPTimeout=0 #关闭连接池
Windows场景配置
在打开的驱动管理器上,选择“连接池”后双击“GaussDB Unicode”驱动名称,选择“使用池连接此驱动程序”[默认为60s],该设置参数同Linux场景配置的CPTimeout,参考例图如下:
1、在应用程序中配置连接池参数,需要在环境句柄创建前通过调用SQLSetEnvAttr 设置连接池参数,其中的环境句柄应设置为 null,这使得 SQL_ATTR_CONNECTION_POOLING 成为进程级属性。
目前在Windows上支持配置SQL_ATTR_CONNECTION_POOLING为以下两种值:
SQL_CP_OFF:默认参数设置,禁用连接池。
SQL_CP_ONE_PER_DRIVER:开启连接池,每个驱动支持一个连接池,驱动其中的所有连接共享一个池。
2、应用程序调用 SQLConnect 或 SQLDriverConnect 时,会从连接池中提取连接,如果连接已超时,或者池中没有与请求匹配的连接,则会打开一个新连接,连接池对调用应用程序是透明的。
3、应用程序调用 SQLDisconnect 时,链接不会被释放,而是会将连接放回连接池用于下一次使用。
4、在环境上调用 SQLFreeHandle释放环境句柄前,应用程序成功为环境设置的所有环境属性都会持续存在。
5、应用程序的连接在一段时间内处于非活动状态(未在连接中使用),则会从池中删除该连接。连接池的大小仅受内存限制和服务器限制。
代码示例
#ifdef WIN32 #include <windows.h> #endif #include <stdio.h> #include <stdlib.h> #include <sql.h> #include <sqlext.h> #include <string.h> #include <sys/time.h> #include <pthread.h> #include <sqltypes.h> #include <time.h> SQLHENV env; SQLHDBC conn; struct timeval start, end; #define CONN_COUNT 15000 #define CHECK_ERROR(retcode, str, handle, htype) \ ({ \ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) { \ fprintf(stderr, "FAILED:\t"); \ extract_error(str, handle, htype); \ exit(-1); \ } else { \ printf("OK:\t%s\n", str); \ } \ }) void print_diag(char *msg, SQLSMALLINT htype, SQLHANDLE handle) { char sqlstate[32]; char message[1000]; SQLINTEGER nativeerror; SQLSMALLINT textlen; SQLRETURN ret; if (msg) { printf("%s\n", msg); } ret = SQLGetDiagRec(htype, handle, 1, sqlstate, &nativeerror, message, 256, &textlen); if (ret != SQL_ERROR) { printf("%s=%s\n", (CHAR *)sqlstate, (CHAR *)message); } } void extract_error(char *fn, SQLHANDLE handle, SQLSMALLINT type) { SQLINTEGER i = 0; SQLINTEGER NativeError; SQLCHAR SQLState[7]; SQLCHAR MessageText[256]; SQLSMALLINT TextLength; SQLRETURN ret; fprintf(stderr, "The driver reported the following error %s\n", fn); if (NULL == handle) return; do { ret = SQLGetDiagRec(type, handle, ++i, SQLState, &NativeError, MessageText, sizeof(MessageText), &TextLength); if (SQL_SUCCEEDED(ret)) { printf("[SQLState:%s]:[%ldth error]:[NativeError:%ld]: %s\n", SQLState, (long)i, (long)NativeError, MessageText); } } while (ret == SQL_SUCCESS); } void InitializeEnvironment() { /* Windows配置连接池参数,需要在分配环境句柄前完成,此处为开启连接池 */ SQLSetEnvAttr(env, SQL_ATTR_CONNECTION_POOLING, (SQLINTEGER *)SQL_CP_ONE_PER_DRIVER, 0); /* 在Win上关闭连接池 */ // SQLSetEnvAttr(env, SQL_ATTR_CONNECTION_POOLING, (SQLINTEGER*)SQL_CP_OFF, 0); // 分配环境句柄 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); // 配置ODBC版本 SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0); // 配置建连超时时间 SQLSetConnectAttr(conn, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0); } void test_connect() { SQLRETURN ret; SQLCHAR str[1024]; SQLSMALLINT strl; SQLCHAR dsn[1024]; SQLUINTEGER uIntVal; SQLAllocHandle(SQL_HANDLE_DBC, env, &conn); /* 根据场景需要调整连接串 */ char *config = "Driver=GaussMPP;DSN=gaussdb;"; ret = SQLSetConnectAttr(conn, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)(1), 0); ret = SQLDriverConnect(conn, 0, (SQLCHAR *)config, SQL_NTS, (SQLCHAR *)NULL, SQL_NTS, 0, SQL_DRIVER_NOPROMPT); if (SQL_SUCCEEDED(ret)) { // printf("Connected\n"); } else { print_diag("SQLDriverConnect failed.", SQL_HANDLE_DBC, conn); SQLFreeHandle(SQL_HANDLE_DBC, conn); SQLFreeHandle(SQL_HANDLE_ENV, env); exit(1); } /* 将连接放入连接池以重用连接 */ if (conn != SQL_NULL_HDBC) { SQLDisconnect(conn); SQLFreeHandle(SQL_HANDLE_DBC, conn); conn = SQL_NULL_HDBC; } } int main() { int count = 0; int timeuser; gettimeofday(&start, NULL); InitializeEnvironment(); for (int i = 0; i < CONN_COUNT; i++) { test_connect(); count++; } //释放环境句柄 SQLFreeHandle(SQL_HANDLE_ENV, env); printf("Connection count: %d\n", count); gettimeofday(&end, NULL); timeuser = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec; printf("Connection time: %.3f s \n", (double)timeuser / 1000000); return 0; }
不同环境下结果不一,在连接池打开的情况下,本示例运行结果如下:
Connection count: 15000 Connection time: 14.175 s
在连接池关闭的情况下,本示例运行结果如下:
Connection count: 15000 Connection time: 691.768 s
Windows环境应用代码同Linux环境如上,连接串需根据场景进行配置。