librpg - A other portgres-c++ - Interface
Class PgTransaction
Description
The PgTransaction is for the technique "Resource aquisition is initializion",
descriped in Stroustrup : The C++ - Programminglanguage (14.4.1).
Like in libpq++, a PgTransaction is a subtype of PgConnection. Thus the
SQL-Commands can submitted via this Interface.
But this interface shoudn't be used alone, the PgTransaction should be initialized via the Constructor from a existing PgConnection, which keeps alive over the livetime of a PgTransaction.
Interface
- PgTransaction(PgConnection &conn)
- Begins a Transaction from a existing connection
- ~PgTransaction();
- If a Transaction is not commited at this time, the transactions will be rolled back
- void commit()
-
- void rollback()
-
- Status getstatus()
-
Example
#include<iostream.h>
#include"rpglib.h"
main(int argc,char **argv)
{
PgConnection *pgc = new PgConnection("dbname=postgres");
try
{
PgResultHandle res;
PgTransaction trans(*pgc); // Constructor calls BEGIN
res=trans.ExecRetTuples("SELECT uid FROM passwd WHERE login='%s'","rw");
for(int t=0;t<PQntuples(res.ptr());t++)
{
cout << PQgetvalue(res.ptr(),t,0) << endl;
}
trans.commit();
// in case of an Error, the Destructor is called with ROLLBACK
}
catch(PgException& ex)
{
cerr << "Ausnahme: " << ex.what() << endl;
}
delete pgc;
}
History
I donīt know. I have seen it in iX and remembered the Designpattern "Resource aquisition is initializion" and tried it.
Computerscience and Networkassocation Ravensburg e.V Rudolf Weber