libpqxx  7.0.2
pqxx::subtransaction Class Reference

"Transaction" nested within another transaction More...

#include <subtransaction.hxx>

Inheritance diagram for pqxx::subtransaction:

Public Member Functions

 subtransaction (dbtransaction &t, std::string const &name=std::string{})
 Nest a subtransaction nested in another transaction. More...
 
 subtransaction (subtransaction &t, std::string const &name=std::string{})
 Nest a subtransaction in another subtransaction. More...
 
virtual ~subtransaction () noexcept override
 
- Public Member Functions inherited from pqxx::internal::transactionfocus
 transactionfocus (transaction_base &t)
 
 transactionfocus ()=delete
 
 transactionfocus (transactionfocus const &)=delete
 
transactionfocusoperator= (transactionfocus const &)=delete
 
- Public Member Functions inherited from pqxx::internal::namedclass
 namedclass (std::string_view classname)
 
 namedclass (std::string_view classname, std::string_view name)
 
 namedclass (std::string_view classname, char const name[])
 
 namedclass (std::string_view classname, std::string &&name)
 
std::string const & name () const noexcept
 Object name, or the empty string if no name was given. More...
 
std::string const & classname () const noexcept
 Class name. More...
 
std::string description () const
 Combination of class name and object name; or just class name. More...
 

Additional Inherited Members

- Protected Member Functions inherited from pqxx::internal::transactionfocus
void register_me ()
 
void unregister_me () noexcept
 
void reg_pending_error (std::string const &) noexcept
 
bool registered () const noexcept
 
- Protected Member Functions inherited from pqxx::dbtransaction
 dbtransaction (connection &c)
 
- Protected Attributes inherited from pqxx::internal::transactionfocus
transaction_base & m_trans
 

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 &C)
{
string const temptable = "fleetingtable";
work W(C, "do_job");
do_firstpart(W);
// Attempt to delete our temporary table if it already existed.
try
{
subtransaction S(W, "droptemp");
S.exec0("DROP TABLE " + temptable);
S.commit();
}
catch (undefined_table const &)
{
// 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.exec0("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

◆ subtransaction() [1/2]

pqxx::subtransaction::subtransaction ( dbtransaction t,
std::string const &  Name = std::string{} 
)
explicit

Nest a subtransaction nested in another transaction.

Implementation of the pqxx::subtransaction class.

pqxx::transaction is a nested transaction, i.e. one within a transaction

Copyright (c) 2000-2020, Jeroen T. Vermeulen.

See COPYING for copyright license. If you did not receive a file called COPYING with this source code, please notify the distributor of this mistake, or contact the author.

◆ subtransaction() [2/2]

pqxx::subtransaction::subtransaction ( subtransaction t,
std::string const &  name = std::string{} 
)
explicit

Nest a subtransaction in another subtransaction.

◆ ~subtransaction()

virtual pqxx::subtransaction::~subtransaction ( )
overridevirtualnoexcept

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