Re: libpq++ : Disconnect a DB

From: 100(dot)179370(at)germanynet(dot)de
To: pgsql-general(at)postgresql(dot)org
Subject: Re: libpq++ : Disconnect a DB
Date: 2001-03-08 20:57:20
Message-ID: 3AA7F230.DEC873DE@germanynet.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message 100.179370 2001-03-08 21:20:23 LDAP with PostgreSQL as backend?
Previous Message Gilles DAROLD 2001-03-08 20:54:19 Shell env and PL/SQL