From: Marco van Wieringen Date: Sun, 4 Apr 2010 17:46:24 +0000 (+0200) Subject: First attempt at making things multi-threaded and make it possible to X-Git-Tag: Release-5.0.2~108 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=69820d0776da2818115d90a2a73b790885c16aa5;p=bacula%2Fbacula First attempt at making things multi-threaded and make it possible to have multiple connections to the database without having the dreaded ERR=E_LQ002E The 'execute immediate' query has been issued outside of a DBMS session. This compiles but seems to give some problems so into the debugger it is. --- diff --git a/bacula/src/cats/cats.h b/bacula/src/cats/cats.h index 20351bd04b..5bbd4d7df4 100644 --- a/bacula/src/cats/cats.h +++ b/bacula/src/cats/cats.h @@ -576,6 +576,7 @@ struct B_DB { char *db_address; /* host address */ char *db_socket; /* socket for local access */ int db_port; /* port of host address */ + int session_id; /* unique session id */ int have_insert_id; /* do have insert_id() */ bool connected; POOLMEM *errmsg; /* nicely edited error message */ diff --git a/bacula/src/cats/ingres.c b/bacula/src/cats/ingres.c index 0dae5b060a..1d307167a3 100755 --- a/bacula/src/cats/ingres.c +++ b/bacula/src/cats/ingres.c @@ -56,7 +56,7 @@ * ----------------------------------------------------------------------- */ -/* List of open databases */ /* SRE: needed for ingres? */ +/* List of open databases */ static BQUEUE db_list = {&db_list, &db_list}; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; @@ -80,6 +80,7 @@ db_init_database(JCR *jcr, const char *db_name, const char *db_user, const char int mult_db_connections) { B_DB *mdb; + int next_session_id = 0; if (!db_user) { Jmsg(jcr, M_FATAL, 0, _("A user name for Ingres must be supplied.\n")); @@ -97,6 +98,20 @@ db_init_database(JCR *jcr, const char *db_name, const char *db_user, const char V(mutex); return mdb; /* already open */ } + + if (mdb->session_id > next_session_id) { + next_session_id = mdb->session_id; + } + } + } else { + /* + * See what the next available session_id is. + * We first see what the highest session_id is used now. + */ + foreach_dlist(mdb, db_list) { + if (mdb->session_id > next_session_id) { + next_session_id = mdb->session_id; + } } } Dmsg0(100, "db_open first time\n"); @@ -114,6 +129,7 @@ db_init_database(JCR *jcr, const char *db_name, const char *db_user, const char mdb->db_socket = bstrdup(db_socket); } mdb->db_port = db_port; + mdb->session_id = ++next_session_id; mdb->have_insert_id = TRUE; mdb->errmsg = get_pool_memory(PM_EMSG); /* get error message buffer */ *mdb->errmsg = 0; @@ -162,22 +178,6 @@ static bool check_database_encoding(JCR *jcr, B_DB *mdb) return true; } -/* - * Check for errors in DBMS work - */ -static int sql_check(B_DB *mdb) -{ - int errorcode; - - if ((errorcode = INGcheck()) < 0) { - /* TODO: fill mdb->errmsg */ - Mmsg(mdb->errmsg, "Something went wrong - still searching!\n"); - } else if (errorcode > 0) { - /* just a warning, proceed */ - } - return errorcode; -} - /* * Now actually open the database. This can generate errors, * which are returned in the errmsg @@ -212,13 +212,13 @@ db_open_database(JCR *jcr, B_DB *mdb) port = NULL; } - mdb->db = INGconnectDB(mdb->db_name, mdb->db_user, mdb->db_password); + mdb->db = INGconnectDB(mdb->db_name, mdb->db_user, mdb->db_password, mdb->session_id); Dmsg0(50, "Ingres real CONNECT done\n"); Dmsg3(50, "db_user=%s db_name=%s db_password=%s\n", mdb->db_user, mdb->db_name, mdb->db_password==NULL?"(NULL)":mdb->db_password); - if (sql_check(mdb)) { + if (!mdb->db) { Mmsg2(&mdb->errmsg, _("Unable to connect to Ingres server.\n" "Database=%s User=%s\n" "It is probably not running or your password is incorrect.\n"), @@ -390,8 +390,9 @@ bool db_sql_query(B_DB *mdb, const char *query, DB_RESULT_HANDLER *result_handle */ void my_ingres_close(B_DB *mdb) { - INGdisconnectDB(mdb->db); - //SRE: error handling? + Dmsg0(500, "my_ingres_close closing database connection\n"); + INGdisconnectDB(mdb->db); + //SRE: error handling? } INGRES_ROW my_ingres_fetch_row(B_DB *mdb) @@ -552,7 +553,7 @@ int my_ingres_query(B_DB *mdb, const char *query) /* TODO: differentiate between SELECTs and other queries */ - if ((cols = INGgetCols(new_query)) <= 0) { + if ((cols = INGgetCols(mdb->db, new_query)) <= 0) { if (cols < 0 ) { Dmsg0(500,"my_ingres_query: neg.columns: no DML stmt!\n"); } diff --git a/bacula/src/cats/myingres.c b/bacula/src/cats/myingres.c index 24b98ea4e1..7fe5a3977c 100644 --- a/bacula/src/cats/myingres.c +++ b/bacula/src/cats/myingres.c @@ -54,12 +54,13 @@ int INGcheck() { return (sqlca.sqlcode < 0) ? sqlca.sqlcode : 0; } -short INGgetCols(const char *query) +short INGgetCols(INGconn *conn, const char *query) { /* # line 57 "myingres.sc" */ + int sess_id; char *stmt; -/* # line 59 "myingres.sc" */ +/* # line 60 "myingres.sc" */ short number = 1; IISQLDA *sqlda; @@ -67,38 +68,54 @@ short INGgetCols(const char *query) memset(sqlda, 0, (IISQDA_HEAD_SIZE + (number * IISQDA_VAR_SIZE))); sqlda->sqln = number; stmt = bstrdup(query); -/* # line 71 "myingres.sc" */ /* prepare */ + /* + * Switch to the correct default session for this thread. + */ + sess_id = conn->session_id; +/* # line 76 "myingres.sc" */ /* set_sql */ + { + IILQssSetSqlio(11,(short *)0,1,30,sizeof(sess_id),&sess_id); + } +/* # line 78 "myingres.sc" */ /* prepare */ { IIsqInit(&sqlca); IIsqPrepare(0,(char *)"s1",(char *)0,0,stmt); } -/* # line 72 "myingres.sc" */ /* host code */ +/* # line 79 "myingres.sc" */ /* host code */ if (INGcheck() < 0) { number = -1; goto bail_out; } -/* # line 77 "myingres.sc" */ /* describe */ +/* # line 84 "myingres.sc" */ /* describe */ { IIsqInit(&sqlca); IIsqDescribe(0,(char *)"s1",sqlda,0); } -/* # line 78 "myingres.sc" */ /* host code */ +/* # line 85 "myingres.sc" */ /* host code */ if (INGcheck() < 0) { number = -1; goto bail_out; } number = sqlda->sqld; bail_out: + /* + * Switch to no default session for this thread. + */ +/* # line 96 "myingres.sc" */ /* set_sql */ + { + IILQssSetSqlio(11,(short *)0,1,30,sizeof(-97),(void *)IILQint(-97)); + } +/* # line 97 "myingres.sc" */ /* host code */ free(stmt); free(sqlda); return number; } static inline IISQLDA *INGgetDescriptor(short numCols, const char *query) { -/* # line 93 "myingres.sc" */ +/* # line 104 "myingres.sc" */ char *stmt; -/* # line 95 "myingres.sc" */ +/* # line 106 "myingres.sc" */ int i; IISQLDA *sqlda; @@ -106,12 +123,12 @@ static inline IISQLDA *INGgetDescriptor(short numCols, const char *query) memset(sqlda, 0, (IISQDA_HEAD_SIZE + (numCols * IISQDA_VAR_SIZE))); sqlda->sqln = numCols; stmt = bstrdup(query); -/* # line 107 "myingres.sc" */ /* prepare */ +/* # line 118 "myingres.sc" */ /* prepare */ { IIsqInit(&sqlca); IIsqPrepare(0,(char *)"s2",sqlda,0,stmt); } -/* # line 109 "myingres.sc" */ /* host code */ +/* # line 120 "myingres.sc" */ /* host code */ free(stmt); for (i = 0; i < sqlda->sqld; ++i) { /* @@ -336,32 +353,32 @@ static inline int INGfetchAll(const char *query, INGresult *ing_res) IISQLDA *desc; int check = -1; desc = ing_res->sqlda; -/* # line 363 "myingres.sc" */ /* host code */ +/* # line 374 "myingres.sc" */ /* host code */ if ((check = INGcheck()) < 0) { return check; } -/* # line 367 "myingres.sc" */ /* open */ +/* # line 378 "myingres.sc" */ /* open */ { IIsqInit(&sqlca); - IIcsOpen((char *)"c2",19318,7414); + IIcsOpen((char *)"c2",10631,20435); IIwritio(0,(short *)0,1,32,0,(char *)"s2"); - IIcsQuery((char *)"c2",19318,7414); + IIcsQuery((char *)"c2",10631,20435); } -/* # line 368 "myingres.sc" */ /* host code */ +/* # line 379 "myingres.sc" */ /* host code */ if ((check = INGcheck()) < 0) { return check; } /* for (linecount = 0; sqlca.sqlcode == 0; ++linecount) */ do { -/* # line 374 "myingres.sc" */ /* fetch */ +/* # line 385 "myingres.sc" */ /* fetch */ { IIsqInit(&sqlca); - if (IIcsRetScroll((char *)"c2",19318,7414,-1,-1) != 0) { + if (IIcsRetScroll((char *)"c2",10631,20435,-1,-1) != 0) { IIcsDaGet(0,desc); IIcsERetrieve(); } /* IIcsRetrieve */ } -/* # line 376 "myingres.sc" */ /* host code */ +/* # line 387 "myingres.sc" */ /* host code */ if ( (sqlca.sqlcode == 0) || (sqlca.sqlcode == -40202) ) { row = INGgetRowSpace(ing_res); /* alloc space for fetched row */ /* @@ -378,12 +395,12 @@ static inline int INGfetchAll(const char *query, INGresult *ing_res) ++linecount; } } while ( (sqlca.sqlcode == 0) || (sqlca.sqlcode == -40202) ); -/* # line 394 "myingres.sc" */ /* close */ +/* # line 405 "myingres.sc" */ /* close */ { IIsqInit(&sqlca); - IIcsClose((char *)"c2",19318,7414); + IIcsClose((char *)"c2",10631,20435); } -/* # line 396 "myingres.sc" */ /* host code */ +/* # line 407 "myingres.sc" */ /* host code */ ing_res->status = ING_COMMAND_OK; ing_res->num_rows = linecount; return linecount; @@ -450,34 +467,51 @@ short INGftype(const INGresult *res, int column_number) } int INGexec(INGconn *conn, const char *query) { - int check; -/* # line 477 "myingres.sc" */ +/* # line 487 "myingres.sc" */ + int sess_id; int rowcount; char *stmt; -/* # line 480 "myingres.sc" */ +/* # line 491 "myingres.sc" */ stmt = bstrdup(query); rowcount = -1; -/* # line 485 "myingres.sc" */ /* execute */ + /* + * Switch to the correct default session for this thread. + */ + sess_id = conn->session_id; +/* # line 500 "myingres.sc" */ /* set_sql */ + { + IILQssSetSqlio(11,(short *)0,1,30,sizeof(sess_id),&sess_id); + } +/* # line 501 "myingres.sc" */ /* execute */ { IIsqInit(&sqlca); IIsqExImmed(stmt); IIsyncup((char *)0,0); } -/* # line 487 "myingres.sc" */ /* host code */ +/* # line 503 "myingres.sc" */ /* host code */ free(stmt); - if ((check = INGcheck()) < 0) { - return check; + if ((rowcount = INGcheck()) < 0) { + goto bail_out; } -/* # line 493 "myingres.sc" */ /* inquire_ingres */ +/* # line 509 "myingres.sc" */ /* inquire_ingres */ { IILQisInqSqlio((short *)0,1,30,sizeof(rowcount),&rowcount,8); } -/* # line 494 "myingres.sc" */ /* host code */ - if ((check = INGcheck()) < 0) { - return check; +/* # line 510 "myingres.sc" */ /* host code */ + if ((rowcount = INGcheck()) < 0) { + goto bail_out; } +bail_out: + /* + * Switch to no default session for this thread. + */ +/* # line 518 "myingres.sc" */ /* set_sql */ + { + IILQssSetSqlio(11,(short *)0,1,30,sizeof(-97),(void *)IILQint(-97)); + } +/* # line 519 "myingres.sc" */ /* host code */ return rowcount; } INGresult *INGquery(INGconn *conn, const char *query) @@ -488,21 +522,45 @@ INGresult *INGquery(INGconn *conn, const char *query) IISQLDA *desc = NULL; INGresult *res = NULL; int rows = -1; - int cols = INGgetCols(query); + int cols = INGgetCols(conn, query); +/* # line 531 "myingres.sc" */ + + int sess_id; +/* # line 533 "myingres.sc" */ + + /* + * Switch to the correct default session for this thread. + */ + sess_id = conn->session_id; +/* # line 539 "myingres.sc" */ /* set_sql */ + { + IILQssSetSqlio(11,(short *)0,1,30,sizeof(sess_id),&sess_id); + } +/* # line 541 "myingres.sc" */ /* host code */ desc = INGgetDescriptor(cols, query); if (!desc) { - return NULL; + goto bail_out; } res = INGgetINGresult(desc); if (!res) { - return NULL; + goto bail_out; } rows = INGfetchAll(query, res); if (rows < 0) { - INGfreeDescriptor(desc); - INGfreeINGresult(res); - return NULL; + INGfreeDescriptor(desc); + INGfreeINGresult(res); + res = NULL; + goto bail_out; } +bail_out: + /* + * Switch to no default session for this thread. + */ +/* # line 564 "myingres.sc" */ /* set_sql */ + { + IILQssSetSqlio(11,(short *)0,1,30,sizeof(-97),(void *)IILQint(-97)); + } +/* # line 565 "myingres.sc" */ /* host code */ return res; } void INGclear(INGresult *res) @@ -513,7 +571,7 @@ void INGclear(INGresult *res) INGfreeDescriptor(res->sqlda); INGfreeINGresult(res); } -INGconn *INGconnectDB(char *dbname, char *user, char *passwd) +INGconn *INGconnectDB(char *dbname, char *user, char *passwd, int session_id) { INGconn *dbconn; if (dbname == NULL || strlen(dbname) == 0) { @@ -521,75 +579,78 @@ INGconn *INGconnectDB(char *dbname, char *user, char *passwd) } dbconn = (INGconn *)malloc(sizeof(INGconn)); memset(dbconn, 0, sizeof(INGconn)); -/* # line 553 "myingres.sc" */ +/* # line 589 "myingres.sc" */ char ingdbname[24]; char ingdbuser[32]; - char ingdbpasw[32]; - char conn_name[32]; + char ingdbpasswd[32]; int sess_id; -/* # line 559 "myingres.sc" */ +/* # line 594 "myingres.sc" */ + sess_id = session_id; bstrncpy(ingdbname, dbname, sizeof(ingdbname)); if (user != NULL) { bstrncpy(ingdbuser, user, sizeof(ingdbuser)); if (passwd != NULL) { - bstrncpy(ingdbpasw, passwd, sizeof(ingdbpasw)); + bstrncpy(ingdbpasswd, passwd, sizeof(ingdbpasswd)); } else { - memset(ingdbpasw, 0, sizeof(ingdbpasw)); + memset(ingdbpasswd, 0, sizeof(ingdbpasswd)); } -/* # line 570 "myingres.sc" */ /* connect */ +/* # line 606 "myingres.sc" */ /* connect */ { IIsqInit(&sqlca); + IILQsidSessID(sess_id); IIsqUser(ingdbuser); - IIsqConnect(0,ingdbname,(char *)"-dbms_password",ingdbpasw,(char *)0, + IIsqConnect(0,ingdbname,(char *)"-dbms_password",ingdbpasswd,(char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0); } -/* # line 574 "myingres.sc" */ /* host code */ +/* # line 611 "myingres.sc" */ /* host code */ } else { -/* # line 575 "myingres.sc" */ /* connect */ +/* # line 612 "myingres.sc" */ /* connect */ { IIsqInit(&sqlca); + IILQsidSessID(sess_id); IIsqConnect(0,ingdbname,(char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0, (char *)0); } -/* # line 576 "myingres.sc" */ /* host code */ +/* # line 615 "myingres.sc" */ /* host code */ } -/* # line 578 "myingres.sc" */ /* inquire_sql */ - { - IILQisInqSqlio((short *)0,1,32,31,conn_name,13); - } -/* # line 579 "myingres.sc" */ /* inquire_sql */ - { - IILQisInqSqlio((short *)0,1,30,sizeof(sess_id),&sess_id,11); - } -/* # line 581 "myingres.sc" */ /* host code */ + if (INGcheck() < 0) { + return NULL; + } bstrncpy(dbconn->dbname, ingdbname, sizeof(dbconn->dbname)); bstrncpy(dbconn->user, ingdbuser, sizeof(dbconn->user)); - bstrncpy(dbconn->password, ingdbpasw, sizeof(dbconn->password)); - bstrncpy(dbconn->connection_name, conn_name, sizeof(dbconn->connection_name)); + bstrncpy(dbconn->password, ingdbpasswd, sizeof(dbconn->password)); dbconn->session_id = sess_id; dbconn->msg = (char*)malloc(257); memset(dbconn->msg, 0, 257); + /* + * Switch to no default session for this thread undo default settings from SQL CONNECT. + */ +/* # line 627 "myingres.sc" */ /* set_sql */ + { + IILQssSetSqlio(11,(short *)0,1,30,sizeof(-97),(void *)IILQint(-97)); + } +/* # line 629 "myingres.sc" */ /* host code */ return dbconn; } void INGdisconnectDB(INGconn *dbconn) { -/* # line 594 "myingres.sc" */ +/* # line 634 "myingres.sc" */ int sess_id; -/* # line 596 "myingres.sc" */ +/* # line 636 "myingres.sc" */ sess_id = dbconn->session_id; -/* # line 600 "myingres.sc" */ /* disconnect */ +/* # line 639 "myingres.sc" */ /* disconnect */ { IIsqInit(&sqlca); IILQsidSessID(sess_id); IIsqDisconnect(); } -/* # line 602 "myingres.sc" */ /* host code */ +/* # line 641 "myingres.sc" */ /* host code */ if (dbconn != NULL) { free(dbconn->msg); free(dbconn); @@ -597,16 +658,16 @@ void INGdisconnectDB(INGconn *dbconn) } char *INGerrorMessage(const INGconn *conn) { -/* # line 610 "myingres.sc" */ +/* # line 649 "myingres.sc" */ char errbuf[256]; -/* # line 612 "myingres.sc" */ +/* # line 651 "myingres.sc" */ -/* # line 614 "myingres.sc" */ /* inquire_ingres */ +/* # line 653 "myingres.sc" */ /* inquire_ingres */ { IILQisInqSqlio((short *)0,1,32,255,errbuf,63); } -/* # line 615 "myingres.sc" */ /* host code */ +/* # line 654 "myingres.sc" */ /* host code */ memcpy(conn->msg, &errbuf, 256); return conn->msg; } @@ -618,5 +679,5 @@ char *INGcmdTuples(INGresult *res) int INGputCopyEnd(INGconn *conn, const char *errormsg); int INGputCopyData(INGconn *conn, const char *buffer, int nbytes); */ -/* # line 629 "myingres.sc" */ +/* # line 668 "myingres.sc" */ #endif diff --git a/bacula/src/cats/myingres.h b/bacula/src/cats/myingres.h index 7f450651f1..dbe2309bcc 100644 --- a/bacula/src/cats/myingres.h +++ b/bacula/src/cats/myingres.h @@ -65,13 +65,12 @@ typedef struct ing_conn { char dbname[24]; char user[32]; char password[32]; - char connection_name[32]; int session_id; char *msg; } INGconn; /* ---Prototypes--- */ int INGcheck(void); -short INGgetCols(const char *stmt); +short INGgetCols(INGconn *conn, const char *query); char *INGgetvalue(INGresult *res, int row_number, int column_number); bool INGgetisnull(INGresult *res, int row_number, int column_number); int INGntuples(const INGresult *res); @@ -81,7 +80,7 @@ short INGftype(const INGresult *res, int column_number); int INGexec(INGconn *db, const char *query); INGresult *INGquery(INGconn *db, const char *query); void INGclear(INGresult *res); -INGconn *INGconnectDB(char *dbname, char *user, char *passwd); +INGconn *INGconnectDB(char *dbname, char *user, char *passwd, int session_id); void INGdisconnectDB(INGconn *dbconn); char *INGerrorMessage(const INGconn *conn); char *INGcmdTuples(INGresult *res); diff --git a/bacula/src/cats/myingres.sc b/bacula/src/cats/myingres.sc index cf1678dce0..d21dfa40f3 100644 --- a/bacula/src/cats/myingres.sc +++ b/bacula/src/cats/myingres.sc @@ -52,9 +52,10 @@ int INGcheck() return (sqlca.sqlcode < 0) ? sqlca.sqlcode : 0; } -short INGgetCols(const char *query) +short INGgetCols(INGconn *conn, const char *query) { EXEC SQL BEGIN DECLARE SECTION; + int sess_id; char *stmt; EXEC SQL END DECLARE SECTION; @@ -67,6 +68,12 @@ short INGgetCols(const char *query) sqlda->sqln = number; stmt = bstrdup(query); + + /* + * Switch to the correct default session for this thread. + */ + sess_id = conn->session_id; + EXEC SQL SET_SQL (SESSION = :sess_id); EXEC SQL PREPARE s1 from :stmt; if (INGcheck() < 0) { @@ -83,6 +90,10 @@ short INGgetCols(const char *query) number = sqlda->sqld; bail_out: + /* + * Switch to no default session for this thread. + */ + EXEC SQL SET_SQL (SESSION = NONE); free(stmt); free(sqlda); return number; @@ -473,8 +484,8 @@ short INGftype(const INGresult *res, int column_number) int INGexec(INGconn *conn, const char *query) { - int check; EXEC SQL BEGIN DECLARE SECTION; + int sess_id; int rowcount; char *stmt; EXEC SQL END DECLARE SECTION; @@ -482,19 +493,29 @@ int INGexec(INGconn *conn, const char *query) stmt = bstrdup(query); rowcount = -1; + /* + * Switch to the correct default session for this thread. + */ + sess_id = conn->session_id; + EXEC SQL SET_SQL (SESSION = :sess_id); EXEC SQL EXECUTE IMMEDIATE :stmt; free(stmt); - if ((check = INGcheck()) < 0) { - return check; + if ((rowcount = INGcheck()) < 0) { + goto bail_out; } EXEC SQL INQUIRE_INGRES(:rowcount = ROWCOUNT); - if ((check = INGcheck()) < 0) { - return check; + if ((rowcount = INGcheck()) < 0) { + goto bail_out; } - + +bail_out: + /* + * Switch to no default session for this thread. + */ + EXEC SQL SET_SQL (SESSION = NONE); return rowcount; } @@ -506,26 +527,41 @@ INGresult *INGquery(INGconn *conn, const char *query) IISQLDA *desc = NULL; INGresult *res = NULL; int rows = -1; - int cols = INGgetCols(query); + int cols = INGgetCols(conn, query); + EXEC SQL BEGIN DECLARE SECTION; + int sess_id; + EXEC SQL END DECLARE SECTION; + + /* + * Switch to the correct default session for this thread. + */ + sess_id = conn->session_id; + EXEC SQL SET_SQL (SESSION = :sess_id); desc = INGgetDescriptor(cols, query); if (!desc) { - return NULL; + goto bail_out; } res = INGgetINGresult(desc); if (!res) { - return NULL; + goto bail_out; } rows = INGfetchAll(query, res); if (rows < 0) { - INGfreeDescriptor(desc); - INGfreeINGresult(res); - return NULL; + INGfreeDescriptor(desc); + INGfreeINGresult(res); + res = NULL; + goto bail_out; } +bail_out: + /* + * Switch to no default session for this thread. + */ + EXEC SQL SET_SQL (SESSION = NONE); return res; } @@ -539,7 +575,7 @@ void INGclear(INGresult *res) INGfreeINGresult(res); } -INGconn *INGconnectDB(char *dbname, char *user, char *passwd) +INGconn *INGconnectDB(char *dbname, char *user, char *passwd, int session_id) { INGconn *dbconn; @@ -553,39 +589,46 @@ INGconn *INGconnectDB(char *dbname, char *user, char *passwd) EXEC SQL BEGIN DECLARE SECTION; char ingdbname[24]; char ingdbuser[32]; - char ingdbpasw[32]; - char conn_name[32]; + char ingdbpasswd[32]; int sess_id; EXEC SQL END DECLARE SECTION; + sess_id = session_id; bstrncpy(ingdbname, dbname, sizeof(ingdbname)); if (user != NULL) { bstrncpy(ingdbuser, user, sizeof(ingdbuser)); if (passwd != NULL) { - bstrncpy(ingdbpasw, passwd, sizeof(ingdbpasw)); + bstrncpy(ingdbpasswd, passwd, sizeof(ingdbpasswd)); } else { - memset(ingdbpasw, 0, sizeof(ingdbpasw)); + memset(ingdbpasswd, 0, sizeof(ingdbpasswd)); } EXEC SQL CONNECT :ingdbname - identified by :ingdbuser - dbms_password = :ingdbpasw; + SESSION :sess_id + IDENTIFIED BY :ingdbuser + DBMS_PASSWORD = :ingdbpasswd; } else { - EXEC SQL CONNECT :ingdbname; + EXEC SQL CONNECT + :ingdbname + SESSION :sess_id; } - - EXEC SQL INQUIRE_SQL(:conn_name = connection_name); - EXEC SQL INQUIRE_SQL(:sess_id = session); + if (INGcheck() < 0) { + return NULL; + } bstrncpy(dbconn->dbname, ingdbname, sizeof(dbconn->dbname)); bstrncpy(dbconn->user, ingdbuser, sizeof(dbconn->user)); - bstrncpy(dbconn->password, ingdbpasw, sizeof(dbconn->password)); - bstrncpy(dbconn->connection_name, conn_name, sizeof(dbconn->connection_name)); + bstrncpy(dbconn->password, ingdbpasswd, sizeof(dbconn->password)); dbconn->session_id = sess_id; dbconn->msg = (char*)malloc(257); memset(dbconn->msg, 0, 257); + /* + * Switch to no default session for this thread undo default settings from SQL CONNECT. + */ + EXEC SQL SET_SQL (SESSION = NONE); + return dbconn; } @@ -596,7 +639,6 @@ void INGdisconnectDB(INGconn *dbconn) EXEC SQL END DECLARE SECTION; sess_id = dbconn->session_id; - EXEC SQL DISCONNECT SESSION :sess_id; if (dbconn != NULL) { diff --git a/bacula/src/cats/myingres.sh b/bacula/src/cats/myingres.sh index 88120e4d4d..a41b87ca1a 100644 --- a/bacula/src/cats/myingres.sh +++ b/bacula/src/cats/myingres.sh @@ -76,7 +76,6 @@ typedef struct ing_conn { char dbname[24]; char user[32]; char password[32]; - char connection_name[32]; int session_id; char *msg; } INGconn; @@ -84,7 +83,7 @@ typedef struct ing_conn { /* ---Prototypes--- */ int INGcheck(void); -short INGgetCols(const char *stmt); +short INGgetCols(INGconn *conn, const char *query); char *INGgetvalue(INGresult *res, int row_number, int column_number); bool INGgetisnull(INGresult *res, int row_number, int column_number); int INGntuples(const INGresult *res); @@ -94,7 +93,7 @@ short INGftype(const INGresult *res, int column_number); int INGexec(INGconn *db, const char *query); INGresult *INGquery(INGconn *db, const char *query); void INGclear(INGresult *res); -INGconn *INGconnectDB(char *dbname, char *user, char *passwd); +INGconn *INGconnectDB(char *dbname, char *user, char *passwd, int session_id); void INGdisconnectDB(INGconn *dbconn); char *INGerrorMessage(const INGconn *conn); char *INGcmdTuples(INGresult *res);