Re: pqlib in c++: PQconnectStart PQconnectPoll

From: "madhtr" <madhtr(at)schif(dot)org>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: pqlib in c++: PQconnectStart PQconnectPoll
Date: 2007-08-16 18:33:17
Message-ID: 011401c7e033$ea877bd0$7b55503f@useronewin2klt
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ok, i figured something out anyway ... hopefully it will be helpful to the
group ...
The following works on my WinXP pro machine most of the time. I have
confirmed
that I have the latest versions of everything.

///////////////////////////////////
#pragma once

#include <stdlib.h>
#include <libpq-fe.h>
#include <windows.h>
#include <winsock2.h>

PGconn* asyncconnect(const char* cs,bool* lp_USER_cancel){
int pqcp = 0;
PGconn* lpcn = 0;
if (lpcn = PQconnectStart(cs)){
if (CONNECTION_BAD != PQstatus(lpcn)){
bool keepon = true;
while (keepon && !*lp_USER_cancel){
switch(pqcp = PQconnectPoll(lpcn)){
case PGRES_POLLING_READING:
{
SOCKET s = PQsocket(lpcn);
timeval tv = {0,100000};
int ns = 0;
while (!*lp_USER_cancel && !ns){
fd_set fdr,fde;
FD_ZERO(&fdr);
FD_ZERO(&fde);
FD_SET(s,&fdr);
FD_SET(s,&fde);
ns = select(0,&fdr,0,&fde,&tv);
};
};
break;
case PGRES_POLLING_FAILED:
case PGRES_POLLING_OK:
keepon = false;
break;
};

// NO!
// Sleep(1);

/******************
I'm guessing the connection gets discoed while the
thread is sleeping, resetting
the error message buffer to something other than
the initial error?

if we use a select(), (the right way) we do not
have to sleep. Still, even the select does NOT always
allow for catching it in time.

IMHO, the error that causes
the disco should never be overwritten by the disco
message itself:

'server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.'

within the internal connection thread, IF this is
indeed what is happening.
*********************/
};
};
};
return lpcn;
};

int main(int na,char** sa){
char* host = "localhost";
unsigned short port = 5432;
char* dbname = "nonexistantdb";
char* user = "";
char* password = "";

bool cancel = false;
char cs[1024];

sprintf(
cs,
"host=%s port=%u dbname=%s user=%s password=%s",
host,port,dbname,user,password
);

if (PGconn* lpcn = asyncconnect(cs,&cancel)){
printf("PQerrorMessage(lpcn) returns:\n\n%s\n\nPQstatus(lpcn) returns
%d\n",PQerrorMessage(lpcn),PQstatus(lpcn));
PQfinish(lpcn);
};

return 0;

};
/////////////////////////////////////////////////

cheers, madhtr

----- Original Message -----
From: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "madhtr" <madhtr(at)schif(dot)org>
Cc: <pgsql-general(at)postgresql(dot)org>
Sent: Tuesday, August 14, 2007 21:42
Subject: Re: [GENERAL] pqlib in c++: PQconnectStart PQconnectPoll

> "madhtr" <madhtr(at)schif(dot)org> writes:
>> From: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
>>> ... although it takes a good long while (several seconds) because of the
>>> "sleep(1)" in the interaction with the postmaster.
>
>> Sleep(1) should be only 1/1000 of a second. I do that so I don't hammer
>> the
>> processor with my while loop when i am not using a select().
>
> Ah. I was interpreting it in Unix terms, where sleep() measures in
> seconds. With a wait of a few msec it might not be too intolerable.
>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
> choose an index scan if your joining column's datatypes do not
> match

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ranjan Kumar Baisak 2007-08-16 18:36:46 How to use Integer array in where IN clause parameter
Previous Message Michael Glaesemann 2007-08-16 17:47:13 Re: automatic rollback?