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-2004, 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/connection_base"
00020 #include "pqxx/transaction"
00021 
00022 
00023 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
00024  */
00025 
00027 #define PQXX_DEPRECATED_TRANSACTION_CALLBACKS
00028 
00029 namespace pqxx
00030 {
00031 
00033 
00059 template<typename TRANSACTION=transaction<read_committed> > 
00060   class transactor : 
00061     public PGSTD::unary_function<TRANSACTION, void>
00062 {
00063 public:
00064   explicit transactor(const PGSTD::string &TName="transactor") :        //[t4]
00065     m_Name(TName) { }
00066 
00068 
00077   void operator()(TRANSACTION &T);                                      //[t4]
00078 
00079   // Overridable member functions, called by connection_base::perform() if an
00080   // attempt to run transaction fails/succeeds, respectively, or if the 
00081   // connection is lost at just the wrong moment, goes into an indeterminate 
00082   // state.  Use these to patch up runtime state to match events, if needed, or
00083   // to report failure conditions.
00084 
00086 
00094   void on_abort(const char[]) throw () {}                               //[t13]
00095 
00097 
00101   void on_commit() {}                                                   //[t6]
00102 
00104 
00115   void on_doubt() throw () {}                                           //[t13]
00116 
00117 #ifdef PQXX_DEPRECATED_TRANSACTION_CALLBACKS
00118 
00119 
00120   void OnCommit() {}
00122 
00123   void OnAbort(const char[]) throw () {}
00125 
00126   void OnDoubt() throw () {}
00127 #endif
00128 
00129   // TODO: Rename Name()--is there a compatible way?
00131   PGSTD::string Name() const { return m_Name; }                         //[t13]
00132 
00133 private:
00134   PGSTD::string m_Name;
00135 };
00136 
00137 
00138 }
00139 
00140 
00151 template<typename TRANSACTOR> 
00152 inline void pqxx::connection_base::perform(const TRANSACTOR &T,
00153                                            int Attempts)
00154 {
00155   if (Attempts <= 0) return;
00156 
00157   bool Done = false;
00158 
00159   // Make attempts to perform T
00160   // TODO: Differentiate between db-related exceptions and other exceptions?
00161   do
00162   {
00163     --Attempts;
00164 
00165     // Work on a copy of T2 so we can restore the starting situation if need be
00166     TRANSACTOR T2(T);
00167     try
00168     {
00169       typename TRANSACTOR::argument_type X(*this, T2.Name());
00170       T2(X);
00171       X.commit();
00172       Done = true;
00173     }
00174     catch (const in_doubt_error &)
00175     {
00176       // Not sure whether transaction went through or not.  The last thing in
00177       // the world that we should do now is retry.
00178       T2.OnDoubt();
00179       throw;
00180     }
00181     catch (const PGSTD::exception &e)
00182     {
00183       // Could be any kind of error.  
00184 #ifdef PQXX_DEPRECATED_TRANSACTION_CALLBACKS
00185       T2.OnAbort(e.what());
00186 #endif
00187       T2.on_abort(e.what());
00188       if (Attempts <= 0) throw;
00189       continue;
00190     }
00191     catch (...)
00192     {
00193       // Don't try to forge ahead if we don't even know what happened
00194       T2.OnAbort("Unknown exception");
00195       throw;
00196     }
00197 
00198 #ifdef PQXX_DEPRECATED_TRANSACTION_CALLBACKS
00199     T2.OnCommit();
00200 #endif
00201     T2.on_commit();
00202   } while (!Done);
00203 }
00204 
00205 

Generated on Mon Nov 15 11:28:01 2004 for libpqxx by  doxygen 1.3.9.1