libpq++ : Disconnect a DB

Started by Renaud Tthonnartabout 25 years ago2 messagesgeneral
Jump to latest
#1Renaud Tthonnart
thonnart@amwdb.u-strasbg.fr

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?
By making this?

delete data;
data = NULL;

regards, Renaud THONNART

#2Noname
100.179370@germanynet.de
In reply to: Renaud Tthonnart (#1)
Re: libpq++ : Disconnect a DB

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/