libpqxx  4.0.1
pqxx::subtransaction Class Reference

"Transaction" nested within another transaction More...

#include <subtransaction.hxx>

Inheritance diagram for pqxx::subtransaction:
pqxx::internal::transactionfocus pqxx::dbtransaction pqxx::internal::namedclass pqxx::transaction_base pqxx::internal::namedclass

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.
- Public Member Functions inherited from pqxx::internal::transactionfocus
 transactionfocus (transaction_base &t)
- Public Member Functions inherited from pqxx::internal::namedclass
 namedclass (const std::string &Classname, const std::string &Name="")
const std::string & name () const throw ()
const std::string & classname () const throw ()
std::string description () const
- Public Member Functions inherited from pqxx::dbtransaction
virtual ~dbtransaction ()
- Public Member Functions inherited from pqxx::transaction_base
virtual ~transaction_base ()=0
void commit ()
 Commit the transaction.
void abort ()
 Abort the transaction.
std::string esc (const char str[]) const
 Escape string for use as SQL string literal in this transaction.
std::string esc (const char str[], size_t maxlen) const
 Escape string for use as SQL string literal in this transaction.
std::string esc (const std::string &str) const
 Escape string for use as SQL string literal in this transaction.
std::string esc_raw (const unsigned char str[], size_t len) const
 Escape binary data for use as SQL string literal in this transaction.
std::string esc_raw (const std::string &) const
 Escape binary data for use as SQL string literal in this transaction.
template<typename T >
std::string quote (const T &t) const
 Represent object as SQL string, including quoting & escaping.
std::string quote_raw (const unsigned char str[], size_t len) const
 Binary-escape and quote a binarystring for use as an SQL constant.
std::string quote_raw (const std::string &str) const
std::string quote_name (const std::string &identifier) const
 Escape an SQL identifier for use in a query.
result exec (const std::string &Query, const std::string &Desc=std::string())
 Execute query.
result exec (const std::stringstream &Query, const std::string &Desc=std::string())
internal::parameterized_invocation parameterized (const std::string &query)
 Parameterize a statement.
connection_baseconn () const
 Connection this transaction is running in.
void set_variable (const std::string &Var, const std::string &Val)
 Set session variable in this connection.
std::string get_variable (const std::string &)
 Get currently applicable value of variable.
prepare::invocation prepared (const std::string &statement=std::string())
 Execute prepared statement.
void process_notice (const char Msg[]) const
 Have connection process warning message.
void process_notice (const std::string &Msg) const
 Have connection process warning message.

Additional Inherited Members

- Public Types inherited from pqxx::transaction_base
typedef isolation_traits
< read_committed
isolation_tag
 If nothing else is known, our isolation level is at least read_committed.
- Protected Member Functions inherited from pqxx::internal::transactionfocus
void register_me ()
void unregister_me () throw ()
void reg_pending_error (const std::string &) throw ()
bool registered () const throw ()
- Protected Member Functions inherited from pqxx::dbtransaction
 dbtransaction (connection_base &, const std::string &IsolationString, readwrite_policy rw=read_write)
 dbtransaction (connection_base &, bool direct=true, readwrite_policy rw=read_write)
void start_backend_transaction ()
 Start a transaction on the backend and set desired isolation level.
virtual void do_begin ()
 Sensible default implemented here: begin backend transaction.
virtual result do_exec (const char Query[])
 Sensible default implemented here: perform query.
virtual void do_commit ()=0
 To be implemented by derived class: commit backend transaction.
virtual void do_abort ()
 Sensible default implemented here: abort backend transaction.
- Static Protected Member Functions inherited from pqxx::dbtransaction
static std::string fullname (const std::string &ttype, const std::string &isolation)
- Protected Attributes inherited from pqxx::internal::transactionfocus
transaction_basem_Trans
- Protected Attributes inherited from pqxx::transaction_base
internal::reactivation_avoidance_counter m_reactivation_avoidance
 Resources allocated in this transaction that make reactivation impossible.

Detailed Description

"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.

Constructor & Destructor Documentation

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.


The documentation for this class was generated from the following files: