Re: C++: PgDatabase or PgConnection and lifetime of the objects? - Mailing list pgsql-interfaces

From jtv
Subject Re: C++: PgDatabase or PgConnection and lifetime of the objects?
Date
Msg-id 20020121131813.C15575@xs4all.nl
Whole thread Raw
In response to C++: PgDatabase or PgConnection and lifetime of the objects?  (Carlos Moreno <moreno@mochima.com>)
List pgsql-interfaces
On Sat, Jan 19, 2002 at 05:53:15PM -0500, Carlos Moreno wrote:
> 
> I'm still not sure I've grasped what are (if any) the
> important differences between the classes PgDatabase and
> PgConnection -- some of the descriptions in the docs are
> identical for both classes, so I'm never sure when to use
> each  (I kind of use PgDatabase by default, but maybe I'm
> missing something?)
The existing libpq++ is somewhat braindead when it comes to
this.  Basically, you use the simplest one that has all the
methods you need--typically PgDatabase.  I honestly have no
idea why the two classes were separated in this way.

What I usually tell people is to take a look at my alternative
C++ frontend, libpqxx:
 http://members.ams.chello.nl/j.vermeulen31/proj-libpqxx.html

And I intend to keep plugging this until it's included as the
standard C++ API.  :-)


> Also, I'm quite unsure about what should be the lifetime
> of the objects:  should I create one database object when
> the application starts, and every time I need to execute
> an SQL statement, I call Exec?  Or should I create a local
> database object whenever I need it?

In libpq++, you create your PgDatabase object and use that to 
do all your work.  Or even better, make it a PgTransaction.
Unfortunately this means you've got to open a new connection
for every transaction.


> What approach could you recommend concerning the two
> details mentioned in the subject?
With libpq++, create one PgDatabase object for the lifetime
of your connection and build your own exception-safe wrapper
to begin/abort/commit transactions.

With libpqxx, create one Pg::Connection object for the
lifetime of your connection and create Transactor classes
for the transactions you need to do.  Then, have your
program invoke them in a robust, restartable manner using 
Connection::Perform(Transactor).

Jeroen



pgsql-interfaces by date:

Previous
From: jtv
Date:
Subject: Re: libpq++
Next
From: Bernhard Herzog
Date:
Subject: non-blocking connections in libpq, fix proposal