libpqxx  v4.0-1
C++ library for PostgreSQL
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pqxx::robusttransaction< ISOLATIONLEVEL > Class Template Reference

Slightly slower, better-fortified version of transaction. More...

#include <robusttransaction.hxx>

Inheritance diagram for pqxx::robusttransaction< ISOLATIONLEVEL >:
Collaboration diagram for pqxx::robusttransaction< ISOLATIONLEVEL >:

Public Types

typedef isolation_traits
< ISOLATIONLEVEL > 
isolation_tag
 

Public Member Functions

 robusttransaction (connection_base &C, const PGSTD::string &Name=PGSTD::string())
 Constructor. More...
 
virtual ~robusttransaction () throw ()
 
void abort ()
 Abort the transaction. More...
 
const PGSTD::string & classname () const throw ()
 
void commit ()
 Commit the transaction. More...
 
connection_baseconn () const
 Connection this transaction is running in. More...
 
PGSTD::string description () const
 
PGSTD::string esc (const char str[]) const
 Escape string for use as SQL string literal in this transaction. More...
 
PGSTD::string esc (const char str[], size_t maxlen) const
 Escape string for use as SQL string literal in this transaction. More...
 
PGSTD::string esc (const PGSTD::string &str) const
 Escape string for use as SQL string literal in this transaction. More...
 
PGSTD::string esc_raw (const unsigned char str[], size_t len) const
 Escape binary data for use as SQL string literal in this transaction. More...
 
PGSTD::string esc_raw (const PGSTD::string &) const
 Escape binary data for use as SQL string literal in this transaction. More...
 
result exec (const PGSTD::string &Query, const PGSTD::string &Desc=PGSTD::string())
 Execute query. More...
 
result exec (const PGSTD::stringstream &Query, const PGSTD::string &Desc=PGSTD::string())
 
PGSTD::string get_variable (const PGSTD::string &)
 Get currently applicable value of variable. More...
 
const PGSTD::string & name () const throw ()
 
internal::parameterized_invocation parameterized (const PGSTD::string &query)
 Parameterize a statement. More...
 
template<typename T >
PGSTD::string quote (const T &t) const
 Represent object as SQL string, including quoting & escaping. More...
 
PGSTD::string quote_name (const PGSTD::string &identifier) const
 Escape an SQL identifier for use in a query. More...
 
PGSTD::string quote_raw (const unsigned char str[], size_t len) const
 Binary-escape and quote a binarystring for use as an SQL constant. More...
 
PGSTD::string quote_raw (const PGSTD::string &str) const
 
void set_variable (const PGSTD::string &Var, const PGSTD::string &Val)
 Set session variable in this connection. More...
 
Prepared statements
prepare::invocation prepared (const PGSTD::string &statement=PGSTD::string())
 Execute prepared statement. More...
 
Error/warning output
void process_notice (const char Msg[]) const
 Have connection process warning message. More...
 
void process_notice (const PGSTD::string &Msg) const
 Have connection process warning message. More...
 

Protected Member Functions

void Begin ()
 Begin transaction (to be called by implementing class) More...
 
result DirectExec (const char C[], int Retries=0)
 Execute query on connection directly. More...
 
virtual result do_exec (const char Query[])
 Sensible default implemented here: perform query. More...
 
void End () throw ()
 End transaction. To be called by implementing class' destructor. More...
 
void reactivation_avoidance_clear () throw ()
 Forget about any reactivation-blocking resources we tried to allocate. More...
 
void start_backend_transaction ()
 Start a transaction on the backend and set desired isolation level. More...
 

Static Protected Member Functions

static PGSTD::string fullname (const PGSTD::string &ttype, const PGSTD::string &isolation)
 

Protected Attributes

internal::reactivation_avoidance_counter m_reactivation_avoidance
 Resources allocated in this transaction that make reactivation impossible. More...
 

Detailed Description

template<isolation_level ISOLATIONLEVEL = read_committed>
class pqxx::robusttransaction< ISOLATIONLEVEL >

Slightly slower, better-fortified version of transaction.

robusttransaction is similar to transaction, but spends more effort (and performance!) to deal with the hopefully rare case that the connection to the backend is lost just as the current transaction is being committed. In this case, there is no way to determine whether the backend managed to commit the transaction before noticing the loss of connection.

In such cases, this class tries to reconnect to the database and figure out what happened. It will need to store and manage some information (pretty much a user-level transaction log) in the back-end for each and every transaction just on the off chance that this problem might occur. This service level was made optional since you may not want to pay this overhead where it is not necessary. Certainly the use of this class makes no sense for local connections, or for transactions that read the database but never modify it, or for noncritical database manipulations.

Besides being slower, it's theoretically possible that robusttransaction actually fails more instead of less often than a normal transaction. This is due to the added work and complexity. What robusttransaction tries to achieve is to be more deterministic, not more successful per se.

When a user first uses a robusttransaction in a database, the class will attempt to create a log table there to keep vital transaction-related state information in. This table, located in that same database, will be called pqxxlog_*user*, where user is the PostgreSQL username for that user. If the log table can not be created, the transaction fails immediately.

If the user does not have permission to create the log table, the database administrator may create one for him beforehand, and give ownership (or at least full insert/update rights) to the user. The table must contain two non-unique fields (which will never be null): "name" (of text type, varchar(256) by default) and "date" (of timestamp type). Older versions of robusttransaction also added a unique "id" field; this field is now obsolete and the log table's implicit oids are used instead. The log tables' names may be made configurable in a future version of libpqxx.

The transaction log table contains records describing unfinished transactions, i.e. ones that have been started but not, as far as the client knows, committed or aborted. This can mean any of the following:

  1. The transaction is in progress. Since backend transactions can't run for extended periods of time, this can only be the case if the log record's timestamp (compared to the server's clock) is not very old, provided of course that the server's system clock hasn't just made a radical jump.
  2. The client's connection to the server was lost, just when the client was committing the transaction, and the client so far has not been able to re-establish the connection to verify whether the transaction was actually completed or rolled back by the server. This is a serious (and luckily a rare) condition and requires manual inspection of the database to determine what happened. The robusttransaction will emit clear and specific warnings to this effect, and will identify the log record describing the transaction in question.
  3. The transaction was completed (either by commit or by rollback), but the client's connection was durably lost just as it tried to clean up the log record. Again, robusttransaction will emit a clear and specific warning to tell you about this and request that the record be deleted as soon as possible.
  4. The client has gone offline at any time while in one of the preceding states. This also requires manual intervention, but the client obviously is not able to issue a warning.

It is safe to drop a log table when it is not in use (ie., it is empty or all records in it represent states 2-4 above). Each robusttransaction will attempt to recreate the table at its next time of use.

Member Typedef Documentation

template<isolation_level ISOLATIONLEVEL = read_committed>
typedef isolation_traits<ISOLATIONLEVEL> pqxx::robusttransaction< ISOLATIONLEVEL >::isolation_tag

Constructor & Destructor Documentation

template<isolation_level ISOLATIONLEVEL = read_committed>
pqxx::robusttransaction< ISOLATIONLEVEL >::robusttransaction ( connection_base C,
const PGSTD::string &  Name = PGSTD::string() 
)
inlineexplicit

Constructor.

Creates robusttransaction of given name

Parameters
CConnection that this robusttransaction should live inside.
Nameoptional human-readable name for this transaction
template<isolation_level ISOLATIONLEVEL = read_committed>
virtual pqxx::robusttransaction< ISOLATIONLEVEL >::~robusttransaction ( ) throw ()
inlinevirtual

Member Function Documentation

void pqxx::transaction_base::abort ( )
inherited

Abort the transaction.

No special effort is required to call this function; it will be called implicitly when the transaction is destructed.

void pqxx::transaction_base::Begin ( )
protectedinherited

Begin transaction (to be called by implementing class)

Will typically be called from implementing class' constructor.

Referenced by pqxx::transaction< ISOLATIONLEVEL, READWRITE >::transaction().

Here is the caller graph for this function:

const PGSTD::string& pqxx::internal::namedclass::classname ( ) const throw ()
inlineinherited
void pqxx::transaction_base::commit ( )
inherited

Commit the transaction.

Unless this function is called explicitly, the transaction will not be committed (actually the nontransaction implementation breaks this rule, hence the name).

Once this function returns, the whole transaction will typically be irrevocably completed in the database. There is also, however, a minute risk that the connection to the database may be lost at just the wrong moment. In that case, libpqxx may be unable to determine whether the transaction was completed or aborted and an in_doubt_error will be thrown to make this fact known to the caller. The robusttransaction implementation takes some special precautions to reduce this risk.

connection_base& pqxx::transaction_base::conn ( ) const
inlineinherited

Connection this transaction is running in.

Referenced by pqxx::transaction< ISOLATIONLEVEL, READWRITE >::~transaction().

Here is the caller graph for this function:

PGSTD::string pqxx::internal::namedclass::description ( ) const
inherited
result pqxx::transaction_base::DirectExec ( const char  C[],
int  Retries = 0 
)
protectedinherited

Execute query on connection directly.

Parameters
CQuery or command to execute
RetriesNumber of times to retry the query if it fails. Be extremely careful with this option; if you retry in the middle of a transaction, you may be setting up a new connection transparently and executing the latter part of the transaction without a backend transaction being active (and with the former part aborted).
virtual result pqxx::dbtransaction::do_exec ( const char  Query[])
protectedvirtualinherited

Sensible default implemented here: perform query.

Implements pqxx::transaction_base.

void pqxx::transaction_base::End ( ) throw ()
protectedinherited

End transaction. To be called by implementing class' destructor.

Referenced by pqxx::transaction< ISOLATIONLEVEL, READWRITE >::~transaction().

Here is the caller graph for this function:

result pqxx::transaction_base::exec ( const PGSTD::string &  Query,
const PGSTD::string &  Desc = PGSTD::string() 
)
inherited

Execute query.

Perform a query in this transaction.

This is one of the most important functions in libpqxx.

Most libpqxx exceptions can be thrown from here, including sql_error, broken_connection, and many sql_error subtypes such as feature_not_supported or insufficient_privilege. But any exception thrown by the C++ standard library may also occur here. All exceptions will be derived from std::exception, however, and all libpqxx-specific exception types are derived from pqxx::pqxx_exception.

Parameters
QueryQuery or command to execute
DescOptional identifier for query, to help pinpoint SQL errors
Returns
A result set describing the query's or command's result
result pqxx::transaction_base::exec ( const PGSTD::stringstream &  Query,
const PGSTD::string &  Desc = PGSTD::string() 
)
inlineinherited

References pqxx::transaction_base::exec().

Referenced by pqxx::transaction_base::exec().

Here is the call graph for this function:

Here is the caller graph for this function:

static PGSTD::string pqxx::dbtransaction::fullname ( const PGSTD::string &  ttype,
const PGSTD::string &  isolation 
)
staticprotectedinherited
PGSTD::string pqxx::transaction_base::get_variable ( const PGSTD::string &  )
inherited

Get currently applicable value of variable.

First consults an internal cache of variables that have been set (whether in the ongoing transaction or in the connection) using the set_variable functions. If it is not found there, the database is queried.

Warning
Do not mix the set_variable with raw "SET" queries, and do not try to set or get variables while a pipeline or table stream is active.
This function used to be declared as const but isn't anymore.
const PGSTD::string& pqxx::internal::namedclass::name ( ) const throw ()
inlineinherited
internal::parameterized_invocation pqxx::transaction_base::parameterized ( const PGSTD::string &  query)
inherited

Parameterize a statement.

prepare::invocation pqxx::transaction_base::prepared ( const PGSTD::string &  statement = PGSTD::string())
inherited

Execute prepared statement.

Prepared statements are defined using the connection classes' prepare() function, and continue to live on in the ongoing session regardless of the context they were defined in (unless explicitly dropped using the connection's unprepare() function). Their execution however, like other forms of query execution, requires a transaction object.

Just like param_declaration is a helper class that lets you tag parameter declarations onto the statement declaration, the invocation class returned here lets you tag parameter values onto the call:

result run_mystatement(transaction_base &T)
{
return T.prepared("mystatement")("param1")(2)()(4).exec();
}

Here, parameter 1 (written as "<tt>$1</tt>" in the statement's body) is a string that receives the value "param1"; the second parameter is an integer with the value 2; the third receives a null, making its type irrelevant; and number 4 again is an integer. The ultimate invocation of exec() is essential; if you forget this, nothing happens.

To see whether any prepared statement has been defined under a given name, use:

T.prepared("mystatement").exists()
Warning
Do not try to execute a prepared statement manually through direct SQL statements. This is likely not to work, and even if it does, is likely to be slower than using the proper libpqxx functions. Also, libpqxx knows how to emulate prepared statements if some part of the infrastructure does not support them.
Actual definition of the prepared statement on the backend may be deferred until its first use, which means that any errors in the prepared statement may not show up until it is executed–and perhaps abort the ongoing transaction in the process.

If you leave out the statement name, the call refers to the nameless statement instead.

void pqxx::transaction_base::process_notice ( const char  Msg[]) const
inlineinherited

Have connection process warning message.

void pqxx::transaction_base::process_notice ( const PGSTD::string &  Msg) const
inlineinherited

Have connection process warning message.

void pqxx::transaction_base::reactivation_avoidance_clear ( ) throw ()
inlineprotectedinherited

Forget about any reactivation-blocking resources we tried to allocate.

References pqxx::result::clear().

Here is the call graph for this function:

void pqxx::transaction_base::set_variable ( const PGSTD::string &  Var,
const PGSTD::string &  Val 
)
inherited

Set session variable in this connection.

The new value is typically forgotten if the transaction aborts. Known exceptions to this rule are nontransaction, and PostgreSQL versions prior to 7.3. In the case of nontransaction, the set value will be kept regardless; but in that case, if the connection ever needs to be recovered, the set value will not be restored.

Parameters
VarThe variable to set
ValThe new value to store in the variable
void pqxx::dbtransaction::start_backend_transaction ( )
protectedinherited

Start a transaction on the backend and set desired isolation level.

Member Data Documentation

internal::reactivation_avoidance_counter pqxx::transaction_base::m_reactivation_avoidance
protectedinherited

Resources allocated in this transaction that make reactivation impossible.

This number may be negative!


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