Re: libpq++ : Disconnect a DB - Mailing list pgsql-general

From 100.179370@germanynet.de
Subject Re: libpq++ : Disconnect a DB
Date
Msg-id 3AA7F230.DEC873DE@germanynet.de
Whole thread Raw
In response to libpq++ : Disconnect a DB  (Renaud Tthonnart <thonnart@amwdb.u-strasbg.fr>)
List pgsql-general
Hi Rebaud,

Renaud Tthonnart wrote:
>
> Good afternoon everyone,
>
> By declaring a PgDatabase variable like this, I know that I make a
> connection to the DB 'test'
>
> PgDatabase data("dbname = test");
>
> How to end connection?

Have a look into file

    /usr/local/pgsql/include/libpq++/pgdatabase.h

or whereever you have installed it.

There is destructor declared

      ~PgDatabase() {}       // close connection and clean up


> By making this?
>
> delete data;
> data = NULL;

No, this would be right, if data were a pointer to an PgDatabase
object. Imagine you had written

    {  ...
       // ptr_to_data is a pointer variable of storage class auto.
       // The objects memory is allocated from the heap and the adress
stored
       // in ptr_to_data
       PgDatabase *ptr_to_data = new PgDatabase("dbname = test");

       // error checking, processing, ...
       delete ptr_to_data;  // This calls the destructor and frees
memory.

       // This is not necessary at all. It is just good style to ensure
that the
       // pointer value can not be used again.
       ptr_to_data = NULL;

       // ptr_to_data goes out of scope. It's only a variable
containing an
       // adress of an object. So nothing is done here. It's legal that
       // there are more than one variables holding a reference of the
object.
       // Your objects life time must not end with the your pointer
going out of
       // scope.
    }

So every destruction--regardless of doing it explicit or
implicit--closes the connection. So, if your variable representing
your connection goes out of scopy, your c++ compiler ensures that
the destructor is called automatically.

    { // data is in storage class auto
      PgDatabase data("dbname=test");

      ... // error checking and processing
    } // end of scope of object data, ~PgDatabase is called!

Hope this helps.

> ...

--
Dipl-Ing. Martin Jacobs * Windsbach
Registered Linux User #87175, http://counter.li.org/



pgsql-general by date:

Previous
From: Gilles DAROLD
Date:
Subject: Shell env and PL/SQL
Next
From: 100.179370@germanynet.de
Date:
Subject: LDAP with PostgreSQL as backend?