pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Frankie Lam <frankie@ucr.com.hk>
To: pgsql-interfaces@postgresql.org
Subject: Any timeout feature(in libPQ) suitable for my case?
Date: Tue, 11 Feb 2003 11:53:39 +0800
Message-ID: <b29s2v$2nlu$1@news.hub.org> (raw)

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





view thread (2+ messages)  latest in thread

Message-ID: <b29s2v$2nlu$1@news.hub.org>
Permalink:  ../b29s2v$2nlu$1@news.hub.org/
Also on:    postgresql.org/message-id/b29s2v$2nlu$1@news.hub.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: frankie@ucr.com.hk
  Subject: Re: Any timeout feature(in libPQ) suitable for my case?
  In-Reply-To: <b29s2v$2nlu$1@news.hub.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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