Any timeout feature(in libPQ) suitable for my case?

From: "Frankie Lam" <frankie(at)ucr(dot)com(dot)hk>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: Any timeout feature(in libPQ) suitable for my case?
Date: 2003-02-11 03:53:39
Message-ID: b29s2v$2nlu$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

I'm sorry if I go the wrong place.

I wonder if there is a timeout feature for limiting the lifetime of a
connection in libPQ.

Please have a look at the simple demo program of my problem below.

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

void printTuples(PGresult *result)
{
int r, n;
int nrows = PQntuples(result);
int nfields = PQnfields(result);
printf("number of rows returned = %d\n", nrows);
printf("number of fields returned = %d\n", nfields);
for(r = 0; r < nrows; r++) {
for(n = 0; n < nfields; n++)
printf(" %s = %s(%d),",
PQfname(result, n),
PQgetvalue(result, r, n),
PQgetlength(result, r, n));
printf("\n");
}
}

void doSQL(PGconn *conn, char *command)
{
PGresult *result;

printf("%s\n", command);

result = PQexec(conn, command);
printf("status is %s\n", PQresStatus(PQresultStatus(result)));
printf("#rows affected %s\n", PQcmdTuples(result));
printf("result message: %s\n", PQresultErrorMessage(result));

switch(PQresultStatus(result)) {
case PGRES_TUPLES_OK:
printTuples(result);
break;
}
PQclear(result);
}

int main()
{
PGresult *result;
PGconn *conn;
int a=-1;

conn = PQconnectdb("hostaddr=192.168.1.50 dbname=twins
connect_timeout=1");

if(PQstatus(conn) == CONNECTION_OK && PQsetnonblocking(conn,1) == 0)
{
printf("connection made\n");

sleep(10); //<<<== sleep for 10 seconds so I have enough time to
unplug the cable.
//GO AND UNPLUG THE NETWORK CABLE to the 192.168.1.50 before it
wakes up.

//After unplugging the cable, it gets stuck here unless I plug the
cable back again,
//connect_timeout is no use :-(

while(result = PQgetResult(conn)) {
printTuples(result);
PQclear(result);
}
}
else
printf("connection failed\n");

PQfinish(conn);
return EXIT_SUCCESS;
}

Any info given is very much appreciated

Frankie

Responses

Browse pgsql-interfaces by date

  From Date Subject
Next Message Tom Lane 2003-02-11 04:35:43 Re: Any timeout feature(in libPQ) suitable for my case?
Previous Message Klimt,Bryan 2003-02-10 20:09:23 PL/Python problem