Dear Gurus,
VERSION:
* libpqxx-2.5.5 (from source at sarge)
* postgresql-dev 7.4.7-6sarge1
* postgresql server 7.4.6 (from source at woody)
I don't know if it's normal. I try to connect to a server which has
connection disabled in pg_hba.conf (otherwise, I connect with `ident
sameuser', but sometimes I need to hangup connections and for this purpose
we modify pg_hba.conf, HUP all backends, and kill unwanted ones. Now I want
to write robust code for this disabled case).
pqxx::connection's documentation says:
The advantage of having an "immediate" connection (represented by this
class) is that errors in setting up the connection will probably occur
during construction of the connection object, rather than at some later
point further down your program.
This is what seems to be hurt here.
As you can see below, I have a Connection class which wraps around libpqxx
(silly, I know). It has reconnect(), begin() and exec(), basically included
below. Other methods, such as commit(), rollback() etc are not included.
* reconnect() creates a pqxx::connection C and receives no errors on connect.
* begin() creates a pqxx::transaction<> W.
* Connection::exec() tries to begin() if needed and W->exec(), but receives
"Broken connection" in begin(). Code snippet and log file below.
Another question: I'd like to use a unique "NORESULT" of type pqxx::result
to be returned from exec() on error. Unfortunately, `r = pqxx::result()'
wouldn't work since INSERTs return the same kind of result. What do you
think is the clearest method?
a) adding a special tuple to the result? smells like hack. how do I do that?
b) changing return type to a descendant of pqxx::result, giving it an error
field?
c) returning a pointer (NULL on error) is not an option. (I can't clearly
remember but I fully discarded that idea.)
TIA,
--
G.
//---------------------------------------------------------------------------
void Connection::reconnect()
{
static int openFailed = 0;
if (C)
delete C;
open = false;
try {
debug("Connect: with string: ``" + connString + "''...", 2+openFailed);
C = new connection(connString);
if (openFailed) {
debug("Connect: success after " + IToS(openFailed) + " failures", 2);
openFailed = 0;
}
debug(" ok.", 7+openFailed);
open = true;
if (afterConnect) {
debug("Connect: afterConnect...", 7+openFailed);
afterConnect(this);
debug(" ok.", 7+openFailed) ;
}
} catch (sql_error &e) {
debug(string("Connection::reconnect: SQL Error. Server said: ") +
e.what() + "QUERY: " + e.query(), 0+openFailed);
} catch (exception &e) {
debug(string("Connection::reconnect: Unknown Error. Someone said: ") +
e.what(), 0+openFailed);
}
if (!open)
openFailed++;
}
//---------------------------------------------------------------------------
void Connection::begin()
{
if (ts==tsSingle) {
if (W)
debug("Connection::begin: already in transaction", 0);
else {
W = new MyWork(*C);
ts = tsBegin;
}
} else
debug("Connection::begin: ts is " + IToS(ts) + ", not " +
IToS(tsSingle), 0);
debug("Connection::begin, W=" + IToS((int)W), 7);
};
//---------------------------------------------------------------------------
const result Connection::exec(string query)
{
debug("Connection::exec: state=" + IToS(ts) + ", query=" + query, 8);
// ...
try {
if (!open) {
reconnect();
}
if (open) {
// ...
const result R = W->exec(query);
// ...
return R;
}
} catch (broken_connection &e) {
debug(string("Connection::exec: Broken Connection. Someone said: ") +
e.what(), 0);
fail = 2;
} // ...
debug("fail: "+IToS(fail),0);
// ...
return NORESULT;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
LOG snippet:
DEBUG2: Connect: with string: ``host=tir port=5432 dbname=tir user=gyujt''...
DEBUG7: ok.
DEBUG7: Connect: afterConnect...
DEBUG8: Connection::exec: state=0, query=LISTEN muvelet_vonalkod
DEBUG0: Connection::exec: Broken Connection. Someone said: fe_sendauth: no
password supplied
DEBUG0: fail: 2
_______________________________________________
Libpqxx-general mailing list
[email protected]
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general