pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <hlinnakangas@vmware.com>
To: Jade Koskela <jkoskela0@gmail.com>
To: pgsql-odbc@postgresql.org
Subject: Re: Problem in SQLFreeHandle (Statement)
Date: Thu, 12 Jun 2014 16:30:13 +0300
Message-ID: <5399AB65.5040608@vmware.com> (raw)
In-Reply-To: <CAN5Zvqz3WRSWFzNAZ0J3ftEib+u85g1GBqb=GMve3tHFz0jkLA@mail.gmail.com>
References: <CAN5Zvqz3WRSWFzNAZ0J3ftEib+u85g1GBqb=GMve3tHFz0jkLA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-odbc>

On 06/12/2014 05:30 AM, Jade Koskela wrote:
> Hello,
> I am using psql-odbc 9.3 on OSX.

Which version, exactly? There were some changes to locking in 
SQLFreeHandle in 09.03.0200. Not sure if those changes might've fixed 
this, or caused it.

Can easily you test on another platform, or with different version of 
unixODBC?

> I have one thread running a query, and
> another thread cancels the query using SQLCancel. The thread running the
> query successfully cancels and exits the driver code. When we try to free
> the statement handle, it blocks on a mutex doing ENTER_CONN_CS, or
> something similar in SQLFreeHandle.
>
> Does anyone have any input on this problem? I don't see who is holding the
> mutex, the thread running the query is done.

I tried to reproduce this with the attached little test program. It 
works fine. Can you post the test program you're using, or modify the 
attached so that it exhibits the behavior you're seeing, please?

- Heikki

-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc

Attachments:

cancel-race.c (text/x-csrc, 1.7K) download #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include "common.h" struct thread_data { SQLHDBC conn; }; SQLHSTMT hstmt; /* this thread runs a long query */ void *thread1_main(void *d) { SQLRETURN rc; printf("1: calling SQLExecute\n"); rc = SQLExecute(hstmt); CHECK_STMT_RESULT(rc, "1: SQLExecute failed", hstmt); print_result(hstmt); rc = SQLFreeStmt(hstmt, SQL_CLOSE); CHECK_STMT_RESULT(rc, "1:SQLFreeStmt failed", hstmt); } /* this thread cancels the first thread's query */ void *thread2_main(void *d) { SQLRETURN rc; sleep(2); printf("2: canceling first query\n"); rc = SQLCancel(hstmt); if (rc == SQL_SUCCESS) printf("2: canceled\n"); else if (rc == SQL_ERROR) printf("2: SQLCancel failed\n"); else printf("2: unexpected return code from SQLCancel: %d\n", rc); } int main(int argc, char **argv) { SQLRETURN rc; char *param1; SQLLEN cbParam1; int i; #define NTHREADS 2 char errstr[100]; pthread_t thread1; pthread_t thread2; void *retval; test_connect(); rc = SQLAllocStmt(conn, &hstmt); if (!SQL_SUCCEEDED(rc)) { print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); exit(1); } rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT pg_sleep(1000)", SQL_NTS); CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt); if (pthread_create(&thread1, NULL, thread1_main, NULL)) { printf("error creating thread\n"); exit(1); } if (pthread_create(&thread2, NULL, thread2_main, NULL)) { printf("error creating thread\n"); exit(1); } if (pthread_join(thread1, &retval) != 0) { printf("error joining thread\n"); exit(1); } if (pthread_join(thread2, &retval) != 0) { printf("error joining thread\n"); exit(1); } return 0; }
common.c (text/x-csrc, 5.5K) download #include "common.h" SQLHENV env; SQLHDBC conn; 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_INVALID_HANDLE) printf("Invalid handle\n"); else if (ret != SQL_ERROR) printf("%s=%s\n", (CHAR *)sqlstate, (CHAR *)message); } void test_connect_ext(char *extraparams) { SQLRETURN ret; SQLCHAR str[1024]; SQLSMALLINT strl; SQLCHAR dsn[1024]; snprintf(dsn, sizeof(dsn), "DSN=psqlodbc_test_dsn;%s", extraparams ? extraparams : ""); SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0); SQLAllocHandle(SQL_HANDLE_DBC, env, &conn); ret = SQLDriverConnect(conn, NULL, dsn, SQL_NTS, str, sizeof(str), &strl, SQL_DRIVER_COMPLETE); if (SQL_SUCCEEDED(ret)) { printf("connected\n"); } else { print_diag("SQLDriverConnect failed.", SQL_HANDLE_DBC, conn); exit(1); } } void test_connect(void) { test_connect_ext(NULL); } void test_disconnect(void) { SQLRETURN rc; printf("disconnecting\n"); rc = SQLDisconnect(conn); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLDisconnect failed", SQL_HANDLE_DBC, conn); exit(1); } rc = SQLFreeHandle(SQL_HANDLE_DBC, conn); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLFreeHandle failed", SQL_HANDLE_DBC, conn); exit(1); } conn = NULL; rc = SQLFreeHandle(SQL_HANDLE_ENV, env); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLFreeHandle failed", SQL_HANDLE_ENV, env); exit(1); } env = NULL; } const char * datatype_str(SQLSMALLINT datatype) { static char buf[100]; switch (datatype) { case SQL_CHAR: return "CHAR"; case SQL_VARCHAR: return "VARCHAR"; case SQL_LONGVARCHAR: return "LONGVARCHAR"; case SQL_WCHAR: return "WCHAR"; case SQL_WVARCHAR: return "WVARCHAR"; case SQL_WLONGVARCHAR: return "WLONGVARCHAR"; case SQL_DECIMAL: return "DECIMAL"; case SQL_NUMERIC: return "NUMERIC"; case SQL_SMALLINT: return "SMALLINT"; case SQL_INTEGER: return "INTEGER"; case SQL_REAL: return "REAL"; case SQL_FLOAT: return "FLOAT"; case SQL_DOUBLE: return "DOUBLE"; case SQL_BIT: return "BIT"; case SQL_TINYINT: return "TINYINT"; case SQL_BIGINT: return "BIGINT"; case SQL_BINARY: return "BINARY"; case SQL_VARBINARY: return "VARBINARY"; case SQL_LONGVARBINARY: return "LONGVARBINARY"; case SQL_TYPE_DATE: return "TYPE_DATE"; case SQL_TYPE_TIME: return "TYPE_TIME"; case SQL_TYPE_TIMESTAMP: return "TYPE_TIMESTAMP"; case SQL_GUID: return "GUID"; default: snprintf(buf, sizeof(buf), "unknown sql type %d", datatype); return buf; } } const char *nullable_str(SQLSMALLINT nullable) { static char buf[100]; switch(nullable) { case SQL_NO_NULLS: return "not nullable"; case SQL_NULLABLE: return "nullable"; case SQL_NULLABLE_UNKNOWN: return "nullable_unknown"; default: snprintf(buf, sizeof(buf), "unknown nullable value %d", nullable); return buf; } } void print_result_meta_series(HSTMT hstmt, SQLSMALLINT *colids, SQLSMALLINT numcols) { int i; printf("Result set metadata:\n"); for (i = 0; i < numcols; i++) { SQLRETURN rc; SQLCHAR colname[50]; SQLSMALLINT colnamelen; SQLSMALLINT datatype; SQLULEN colsize; SQLSMALLINT decdigits; SQLSMALLINT nullable; rc = SQLDescribeCol(hstmt, colids[i], colname, sizeof(colname), &colnamelen, &datatype, &colsize, &decdigits, &nullable); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLDescribeCol failed", SQL_HANDLE_STMT, hstmt); return; } printf("%s: %s(%u) digits: %d, %s\n", colname, datatype_str(datatype), (unsigned int) colsize, decdigits, nullable_str(nullable)); } } void print_result_meta(HSTMT hstmt) { SQLRETURN rc; SQLSMALLINT numcols, i; SQLSMALLINT *colids; rc = SQLNumResultCols(hstmt, &numcols); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLNumResultCols failed", SQL_HANDLE_STMT, hstmt); return; } colids = (SQLSMALLINT *) malloc(numcols * sizeof(SQLSMALLINT)); for (i = 0; i < numcols; i++) colids[i] = i + 1; print_result_meta_series(hstmt, colids, numcols); free(colids); } /* * Print result only for the selected columns. */ void print_result_series(HSTMT hstmt, SQLSMALLINT *colids, SQLSMALLINT numcols) { SQLRETURN rc; printf("Result set:\n"); while(1) { rc = SQLFetch(hstmt); if (rc == SQL_NO_DATA) break; if (rc == SQL_SUCCESS) { char buf[40]; int i; SQLLEN ind; for (i = 0; i < numcols; i++) { rc = SQLGetData(hstmt, colids[i], SQL_C_CHAR, buf, sizeof(buf), &ind); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLGetData failed", SQL_HANDLE_STMT, hstmt); return; } if (ind == SQL_NULL_DATA) strcpy(buf, "NULL"); printf("%s%s", (i > 0) ? "\t" : "", buf); } printf("\n"); } else { print_diag("SQLFetch failed", SQL_HANDLE_STMT, hstmt); exit(1); } } } /* * Print result on all the columns */ void print_result(HSTMT hstmt) { SQLRETURN rc; SQLSMALLINT numcols, i; SQLSMALLINT *colids; rc = SQLNumResultCols(hstmt, &numcols); if (!SQL_SUCCEEDED(rc)) { print_diag("SQLNumResultCols failed", SQL_HANDLE_STMT, hstmt); return; } colids = (SQLSMALLINT *) malloc(numcols * sizeof(SQLSMALLINT)); for (i = 0; i < numcols; i++) colids[i] = i + 1; print_result_series(hstmt, colids, numcols); free(colids); }
common.h (text/x-chdr, 976B) download #include <stdio.h> #include <string.h> #include <stdlib.h> #ifdef WIN32 #include <windows.h> #endif #include <sql.h> #include <sqlext.h> #ifdef WIN32 #define snprintf _snprintf #endif extern SQLHENV env; extern SQLHDBC conn; #define CHECK_STMT_RESULT(rc, msg, hstmt) \ if (!SQL_SUCCEEDED(rc)) \ { \ print_diag(msg, SQL_HANDLE_STMT, hstmt); \ exit(1); \ } extern void print_diag(char *msg, SQLSMALLINT htype, SQLHANDLE handle); extern void test_connect_ext(char *extraparams); extern void test_connect(void); extern void test_disconnect(void); extern void print_result_meta_series(HSTMT hstmt, SQLSMALLINT *colids, SQLSMALLINT numcols); extern void print_result_series(HSTMT hstmt, SQLSMALLINT *colids, SQLSMALLINT numcols); extern void print_result_meta(HSTMT hstmt); extern void print_result(HSTMT hstmt); extern const char *datatype_str(SQLSMALLINT datatype); extern const char *nullable_str(SQLSMALLINT nullable);

view thread (7+ messages)  latest in thread

Message-ID: <5399AB65.5040608@vmware.com>
Permalink:  ../5399AB65.5040608@vmware.com/
Also on:    postgresql.org/message-id/5399AB65.5040608@vmware.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-odbc@postgresql.org
  Cc: hlinnakangas@vmware.com, jkoskela0@gmail.com
  Subject: Re: Problem in SQLFreeHandle (Statement)
  In-Reply-To: <5399AB65.5040608@vmware.com>
  References: <CAN5Zvqz3WRSWFzNAZ0J3ftEib+u85g1GBqb=GMve3tHFz0jkLA@mail.gmail.com> <5399AB65.5040608@vmware.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox