pqlib in c++: PQconnectStart PQconnectPoll
when i intentioally try to connect asynchronously to a database that does
not exist, i get
"server closed the connection unexpectedly"
My intention is to create the database if it does not exist ... Is there any
way retrive the actual error so i can know when to create the database?
thanx:)
Import Notes
Reference msg id not found: e373d31e0708140718l1869e794ie884e8830758a6eb@mail.gmail.comReference msg id not found: 874pj26um5.fsf@oxford.xeocode.comReference msg id not found: e373d31e0708140809h177d374dg6720317a5512d171@mail.gmail.comReference msg id not found: 46C1D069.5000801@magproductions.nlReference msg id not found: e373d31e0708141027v32249939u6df47d70e447494a@mail.gmail.com
ok thanx:)
... here's the source ... can u tell me whats wrong? (the purpose of this
function is to allow for thread safety on the connection, and allow for
cancellation if the connection takes too long)
BTW ...
- coninfo is "host=localhost port=5432 dbname=testdb user=localuser
password=localpassword"
- I am using VC++ and compiling a windows execuatble ...
PGconn* PQconnectStartCS(const char* coninfo,LPCRITICAL_SECTION lpcs,bool*
lpcancel,int* lppge){
int& pge = *lppge;
bool& cancel = *lpcancel;
bool keepon = true;
PGconn* pr = 0;
pge = 0;
EnterCriticalSection(lpcs);
pr = PQconnectStart(coninfo);
while (!killthread(&cancel) && keepon){
switch(pge = PQconnectPoll(pr)){
case PGRES_POLLING_FAILED:
keepon = false;
break;
case PGRES_POLLING_OK:
pge = 0;
keepon = false;
break;
default:
break;
};
if (keepon)
Sleep(1);
};
LeaveCriticalSection(lpcs);
if (!pge && pr){
switch(pge = PQstatus(pr)){
case CONNECTION_OK:
pge = 0;
break;
};
};
return pr;
};
----- Original Message -----
From: "Tom Lane" <tgl@sss.pgh.pa.us>
To: "madhtr" <madhtr@schif.org>
Cc: <pgsql-general@postgresql.org>
Sent: Tuesday, August 14, 2007 14:36
Subject: Re: [GENERAL] pqlib in c++: PQconnectStart PQconnectPoll
"madhtr" <madhtr@schif.org> writes:
when i intentioally try to connect asynchronously to a database that does
not exist, i get"server closed the connection unexpectedly"
There's something wrong with your code then.
regards, tom lane
---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings
----- Original Message -----
From: "Tom Lane" <tgl@sss.pgh.pa.us>
To: "madhtr" <madhtr@schif.org>
Cc: <pgsql-general@postgresql.org>
Sent: Tuesday, August 14, 2007 14:36
Subject: Re: [GENERAL] pqlib in c++: PQconnectStart PQconnectPoll
Show quoted text
"madhtr" <madhtr@schif.org> writes:
when i intentioally try to connect asynchronously to a database that does
not exist, i get"server closed the connection unexpectedly"
There's something wrong with your code then.
regards, tom lane
---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings
Import Notes
Reference msg id not found: e373d31e0708140718l1869e794ie884e8830758a6eb@mail.gmail.comReference msg id not found: 874pj26um5.fsf@oxford.xeocode.comReference msg id not found: e373d31e0708140809h177d374dg6720317a5512d171@mail.gmail.comReference msg id not found: 46C1D069.5000801@magproductions.nlReference msg id not found: e373d31e0708141027v32249939u6df47d70e447494a@mail.gmail.com
I did make an error on the zero assumption, ty :)
However, the reason PGRES_POLLING_FAILED and PGRES_POLLING_OK both break the
loop is because of this:
"If this call returns PGRES_POLLING_FAILED, the connection procedure has
failed. If this call returns PGRES_POLLING_OK, the connection has been
successfully made."
source: http://www.postgresql.org/docs/7.3/static/libpq-connect.html
I was also under the assumption that I would not need to perform my own
selects on the underlying socket, and that whatever I got back would be
either a null pointer, a successful connection pointer, or a broken
connection pointer with an error indication.
cleary I am going to have to study this documentation more carefully ... So
... for simplicity's sake, If I just do the following, how do I get back
"database does not exist" ?
////////////////////
#pragma once
#include <stdlib.h>
#include <libpq-fe.h>
#include <windows.h>
int main(int na,char** sa){
char* host = "localhost";
unsigned short port = 5432;
char* dbname = "nonexistantdb";
char* user = "user";
char* password = "pass";
int e = 0;
PGconn* lpcn = 0;
bool keepon = true;
char cs[1024];
sprintf(
cs,
"host=%s port=%u dbname=%s user=%s password=%s",
host,port,dbname,user,password
);
if (lpcn = PQconnectStart(cs)){
while (keepon){
switch(e = PQconnectPoll(lpcn)){
case PGRES_POLLING_FAILED:
case PGRES_POLLING_OK:
keepon = false;
break;
};
Sleep(1);
};
printf(
"PQerrorMessage(lpcn) returns:\n\n%s\n\nPQstatus(lpcn)
returns %d\n",
PQerrorMessage(lpcn),PQstatus(lpcn)
);
PQfinish(lpcn);
} else
printf("I am assuming we are out of memory ...\n");
return e;
};
/////////////
----- Original Message -----
From: "Tom Lane" <tgl@sss.pgh.pa.us>
To: "madhtr" <madhtr@schif.org>
Cc: <pgsql-general@postgresql.org>
Sent: Tuesday, August 14, 2007 15:53
Subject: Re: [GENERAL] pqlib in c++: PQconnectStart PQconnectPoll
Show quoted text
"madhtr" <madhtr@schif.org> writes:
... here's the source ... can u tell me whats wrong?
Well, your usage of "pge" seems fairly broken, in particular the random
(and wrong) assumptions about which values are or are not zero. AFAICT
this code doesn't really distinguish between PGRES_POLLING_FAILED and
PGRES_POLLING_OK. And if it does return failure, there's no way for the
caller to know which enum type the failure code belongs to.You didn't show us the code that is actually reporting the error, but I
wonder whether it isn't equally confused about how to determine what the
error is.regards, tom lane
Import Notes
Reference msg id not found: e373d31e0708140718l1869e794ie884e8830758a6eb@mail.gmail.comReference msg id not found: 874pj26um5.fsf@oxford.xeocode.comReference msg id not found: e373d31e0708140809h177d374dg6720317a5512d171@mail.gmail.comReference msg id not found: 46C1D069.5000801@magproductions.nlReference msg id not found: e373d31e0708141027v32249939u6df47d70e447494a@mail.gmail.com
----- Original Message -----
From: "Tom Lane" <tgl@sss.pgh.pa.us>
To: "madhtr" <madhtr@schif.org>
Cc: <pgsql-general@postgresql.org>
Sent: Tuesday, August 14, 2007 18:50
Subject: Re: [GENERAL] pqlib in c++: PQconnectStart PQconnectPoll
"madhtr" <madhtr@schif.org> writes:
cleary I am going to have to study this documentation more carefully ...
So
... for simplicity's sake, If I just do the following, how do I get back
"database does not exist" ?[ shrug... ] Your program works perfectly for me:
$ ./testit
PQerrorMessage(lpcn) returns:FATAL: database "nonexistantdb" does not exist
PQstatus(lpcn)
returns 1
$... although it takes a good long while (several seconds) because of the
"sleep(1)" in the interaction with the postmaster.
hmm ... TY, must be my version or something like you state further down.
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().
Maybe your problem is not with the program, but with the postmaster
you're trying to connect to? Does psql work?
yep, good thought. psql command line works fine, its sycnhronous tho.
source: http://www.postgresql.org/docs/7.3/static/libpq-connect.html
Another line of thought, given the reading-between-the-lines conclusion
that you are trying to use PG 7.3 libraries on Windows, is that there
was something broken in the async-connect code back then on that
platform. If you really are trying to do that, do yourself a favor and
move to 8.0 or later. Nobody's going to be very interested in fixing
7.3. (I did try your program with 7.3 on Unix, though, and it seemed
fine except the error message was spelled a bit differently.)
Ty, I'll check that ... :)
I was also under the assumption that I would not need to perform my own
selects on the underlying socket, and that whatever I got back would be
either a null pointer, a successful connection pointer, or a broken
connection pointer with an error indication.You don't *have* to perform selects on the underlying socket, but if you
are not multiplexing this activity with some other I/O, I have to wonder
why you are bothering with an asynchronous connect at all. What you've
got at the moment is a poorly-implemented equivalent of PQconnectdb().
yep, the little simplified program is fairly pointless... but in RL, i will
pass a pointer to a cancel flag ...
void connect(bool* lpcancel){
while (!*lpcancel && trying2connect){
// yadda yadda yadda
};
};
so that the user can click the "connect" button, start a thread, and then
click the "cancel" button instead giving my app the three finger salute if
they grow impatient, heh;)
In any case, I very much appreciate your help and time, I'll let you know
what I figure out. I bet you're right about the version. Hopefully I can
contribute something back to the list at some point. Again, sry for the
sloppy code at the beginning :)
madhtr
Import Notes
Reference msg id not found: e373d31e0708140718l1869e794ie884e8830758a6eb@mail.gmail.comReference msg id not found: 874pj26um5.fsf@oxford.xeocode.comReference msg id not found: e373d31e0708140809h177d374dg6720317a5512d171@mail.gmail.comReference msg id not found: 46C1D069.5000801@magproductions.nlReference msg id not found: e373d31e0708141027v32249939u6df47d70e447494a@mail.gmail.com
"madhtr" <madhtr@schif.org> writes:
when i intentioally try to connect asynchronously to a database that does
not exist, i get
"server closed the connection unexpectedly"
There's something wrong with your code then.
regards, tom lane
"madhtr" <madhtr@schif.org> writes:
... here's the source ... can u tell me whats wrong?
Well, your usage of "pge" seems fairly broken, in particular the random
(and wrong) assumptions about which values are or are not zero. AFAICT
this code doesn't really distinguish between PGRES_POLLING_FAILED and
PGRES_POLLING_OK. And if it does return failure, there's no way for the
caller to know which enum type the failure code belongs to.
You didn't show us the code that is actually reporting the error, but I
wonder whether it isn't equally confused about how to determine what the
error is.
regards, tom lane
"madhtr" <madhtr@schif.org> writes:
cleary I am going to have to study this documentation more carefully ... So
... for simplicity's sake, If I just do the following, how do I get back
"database does not exist" ?
[ shrug... ] Your program works perfectly for me:
$ ./testit
PQerrorMessage(lpcn) returns:
FATAL: database "nonexistantdb" does not exist
PQstatus(lpcn)
returns 1
$
... although it takes a good long while (several seconds) because of the
"sleep(1)" in the interaction with the postmaster.
Maybe your problem is not with the program, but with the postmaster
you're trying to connect to? Does psql work?
source: http://www.postgresql.org/docs/7.3/static/libpq-connect.html
Another line of thought, given the reading-between-the-lines conclusion
that you are trying to use PG 7.3 libraries on Windows, is that there
was something broken in the async-connect code back then on that
platform. If you really are trying to do that, do yourself a favor and
move to 8.0 or later. Nobody's going to be very interested in fixing
7.3. (I did try your program with 7.3 on Unix, though, and it seemed
fine except the error message was spelled a bit differently.)
I was also under the assumption that I would not need to perform my own
selects on the underlying socket, and that whatever I got back would be
either a null pointer, a successful connection pointer, or a broken
connection pointer with an error indication.
You don't *have* to perform selects on the underlying socket, but if you
are not multiplexing this activity with some other I/O, I have to wonder
why you are bothering with an asynchronous connect at all. What you've
got at the moment is a poorly-implemented equivalent of PQconnectdb().
regards, tom lane
"madhtr" <madhtr@schif.org> writes:
From: "Tom Lane" <tgl@sss.pgh.pa.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