Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

transactor.hxx

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/transactor.hxx
00005  *
00006  *   DESCRIPTION
00007  *      definition of the pqxx::transactor class.
00008  *   pqxx::transactor is a framework-style wrapper for safe transactions
00009  *   DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/transactor instead.
00010  *
00011  * Copyright (c) 2001-2005, Jeroen T. Vermeulen <jtv@xs4all.nl>
00012  *
00013  * See COPYING for copyright license.  If you did not receive a file called
00014  * COPYING with this source code, please notify the distributor of this mistake,
00015  * or contact the author.
00016  *
00017  *-------------------------------------------------------------------------
00018  */
00019 #include "pqxx/libcompiler.h"
00020 
00021 #include "pqxx/connection_base"
00022 #include "pqxx/transaction"
00023 
00024 
00025 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
00026  */
00027 
00029 #define PQXX_DEPRECATED_TRANSACTION_CALLBACKS
00030 
00031 namespace pqxx
00032 {
00033 
00035 
00061 template<typename TRANSACTION=transaction<read_committed> >
00062   class transactor :
00063     public PGSTD::unary_function<TRANSACTION, void>
00064 {
00065 public:
00066   explicit transactor(const PGSTD::string &TName="transactor") :        //[t4]
00067     m_Name(TName) { }
00068 
00070 
00081   void operator()(TRANSACTION &T);                                      //[t4]
00082 
00083   // Overridable member functions, called by connection_base::perform() if an
00084   // attempt to run transaction fails/succeeds, respectively, or if the
00085   // connection is lost at just the wrong moment, goes into an indeterminate
00086   // state.  Use these to patch up runtime state to match events, if needed, or
00087   // to report failure conditions.
00088 
00090 
00098   void on_abort(const char[]) throw () {}                               //[t13]
00099 
00101 
00105   void on_commit() {}                                                   //[t6]
00106 
00108 
00119   void on_doubt() throw () {}                                           //[t13]
00120 
00121 #ifdef PQXX_DEPRECATED_TRANSACTION_CALLBACKS
00122 
00123 
00124   void OnCommit() {}
00126 
00127   void OnAbort(const char[]) throw () {}
00129 
00130   void OnDoubt() throw () {}
00131 #endif
00132 
00133   // TODO: Rename Name()--is there a compatible way?
00135   PGSTD::string Name() const { return m_Name; }                         //[t13]
00136 
00137 private:
00138   PGSTD::string m_Name;
00139 };
00140 
00141 
00142 }
00143 
00144 
00155 template<typename TRANSACTOR>
00156 inline void pqxx::connection_base::perform(const TRANSACTOR &T,
00157                                            int Attempts)
00158 {
00159   if (Attempts <= 0) return;
00160 
00161   bool Done = false;
00162 
00163   // Make attempts to perform T
00164   // TODO: Differentiate between db-related exceptions and other exceptions?
00165   do
00166   {
00167     --Attempts;
00168 
00169     // Work on a copy of T2 so we can restore the starting situation if need be
00170     TRANSACTOR T2(T);
00171     try
00172     {
00173       typename TRANSACTOR::argument_type X(*this, T2.Name());
00174       T2(X);
00175       X.commit();
00176       Done = true;
00177     }
00178     catch (const in_doubt_error &)
00179     {
00180       // Not sure whether transaction went through or not.  The last thing in
00181       // the world that we should do now is retry.
00182       T2.OnDoubt();
00183       throw;
00184     }
00185     catch (const PGSTD::exception &e)
00186     {
00187       // Could be any kind of error.
00188 #ifdef PQXX_DEPRECATED_TRANSACTION_CALLBACKS
00189       T2.OnAbort(e.what());
00190 #endif
00191       T2.on_abort(e.what());
00192       if (Attempts <= 0) throw;
00193       continue;
00194     }
00195     catch (...)
00196     {
00197       // Don't try to forge ahead if we don't even know what happened
00198       T2.OnAbort("Unknown exception");
00199       throw;
00200     }
00201 
00202 #ifdef PQXX_DEPRECATED_TRANSACTION_CALLBACKS
00203     T2.OnCommit();
00204 #endif
00205     T2.on_commit();
00206   } while (!Done);
00207 }
00208 
00209 

Generated on Sat Mar 19 18:42:43 2005 for libpqxx by  doxygen 1.4.1