libpqxx 4.0
|
"Transaction" nested within another transaction More...
#include <subtransaction.hxx>
Public Member Functions | |
subtransaction (dbtransaction &T, const std::string &Name=std::string()) | |
Nest a subtransaction nested in another transaction. | |
subtransaction (subtransaction &T, const std::string &Name=std::string()) | |
Nest a subtransaction in another subtransaction. |
"Transaction" nested within another transaction
A subtransaction can be executed inside a backend transaction, or inside another subtransaction. This can be useful when, for example, statements in a transaction may harmlessly fail and you don't want them to abort the entire transaction. Here's an example of how a temporary table may be dropped before re-creating it, without failing if the table did not exist:
void do_job(connection_base &C) { const string temptable = "fleetingtable"; // Since we're dealing with a temporary table here, disallow automatic // recovery of the connection in case it breaks. C.inhibit_reactivation(true); work W(C, "do_job"); do_firstpart(W); // Attempt to delete our temporary table if it already existed try { subtransaction S(W, "droptemp"); S.exec("DROP TABLE " + temptable); S.commit(); } catch (const undefined_table &) { // Table did not exist. Which is what we were hoping to achieve anyway. // Carry on without regrets. } // S may have gone into a failed state and been destroyed, but the // upper-level transaction W is still fine. We can continue to use it. W.exec("CREATE TEMP TABLE " + temptable + "(bar integer, splat varchar)"); do_lastpart(W); }
(This is just an example. If you really wanted to do drop a table without an error if it doesn't exist, you'd use DROP TABLE IF EXISTS.)
There are no isolation levels inside a transaction. They are not needed because all actions within the same backend transaction are always performed sequentially anyway.
pqxx::subtransaction::subtransaction | ( | dbtransaction & | T, |
const std::string & | Name = std::string() |
||
) | [explicit] |
Nest a subtransaction nested in another transaction.
pqxx::subtransaction::subtransaction | ( | subtransaction & | T, |
const std::string & | Name = std::string() |
||
) | [explicit] |
Nest a subtransaction in another subtransaction.