Any timeout feature(in libPQ) suitable for my case? - Mailing list pgsql-interfaces

From Frankie Lam
Subject Any timeout feature(in libPQ) suitable for my case?
Date
Msg-id b29s2v$2nlu$1@news.hub.org
Whole thread Raw
Responses Re: Any timeout feature(in libPQ) suitable for my case?  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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




pgsql-interfaces by date:

Previous
From: "Jeroen T. Vermeulen"
Date:
Subject: libpqxx presentation
Next
From: Tom Lane
Date:
Subject: Re: Any timeout feature(in libPQ) suitable for my case?