pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Any timeout feature(in libPQ) suitable for my case?
2+ messages / 2 participants
[nested] [flat]

* Any timeout feature(in libPQ) suitable for my case?
@ 2003-02-11 03:53  Frankie Lam <frankie@ucr.com.hk>
  0 siblings, 1 reply; 2+ messages in thread

From: Frankie Lam @ 2003-02-11 03:53 UTC (permalink / raw)
  To: 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





^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Any timeout feature(in libPQ) suitable for my case?
@ 2003-02-11 04:35  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Frankie Lam <frankie@ucr.com.hk>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2003-02-11 04:35 UTC (permalink / raw)
  To: Frankie Lam <frankie@ucr.com.hk>; +Cc: pgsql-interfaces

"Frankie Lam" <frankie@ucr.com.hk> writes:
> I wonder if there is a timeout feature for limiting the lifetime of a
> connection in libPQ.

There is not.  I don't think it's libpq's business to be deciding
whether the connection is lost or not --- that is the province of
the kernel's TCP stack.  IMHO we should not second-guess the transport
layer.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2003-02-11 04:35 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-02-11 03:53 Any timeout feature(in libPQ) suitable for my case? Frankie Lam <frankie@ucr.com.hk>
2003-02-11 04:35 ` Tom Lane <tgl@sss.pgh.pa.us>

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