00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef PQXX_TRANSACTOR_H
00019 #define PQXX_TRANSACTOR_H
00020
00021 #include <string>
00022
00023 #include "pqxx/compiler.h"
00024 #include "pqxx/connection_base.h"
00025 #include "pqxx/transaction.h"
00026
00027
00028
00029
00030
00031
00032 namespace pqxx
00033 {
00034
00036
00061 template<typename TRANSACTION=transaction<read_committed> >
00062 class PQXX_LIBEXPORT transactor :
00063 public PGSTD::unary_function<TRANSACTION, void>
00064 {
00065 public:
00066 explicit transactor(const PGSTD::string &TName="transactor") :
00067 m_Name(TName) { }
00068
00070
00076 void operator()(TRANSACTION &T);
00077
00078
00079
00080
00081
00082
00083
00085
00091 void OnAbort(const char []) throw () {}
00092
00094
00097 void OnCommit() {}
00098
00101
00110 void OnDoubt() throw () {}
00111
00113 PGSTD::string Name() const { return m_Name; }
00114
00115 private:
00116 PGSTD::string m_Name;
00117 };
00118
00119
00121 typedef transactor<transaction<read_committed> > Transactor;
00122
00123 }
00124
00125
00136 template<typename TRANSACTOR>
00137 inline void pqxx::connection_base::Perform(const TRANSACTOR &T,
00138 int Attempts)
00139 {
00140 if (Attempts <= 0) return;
00141
00142 bool Done = false;
00143
00144
00145
00146 do
00147 {
00148 --Attempts;
00149
00150
00151 TRANSACTOR T2(T);
00152 try
00153 {
00154 typename TRANSACTOR::argument_type X(*this, T2.Name());
00155 T2(X);
00156 X.Commit();
00157 Done = true;
00158 }
00159 catch (const in_doubt_error &)
00160 {
00161
00162
00163 T2.OnDoubt();
00164 throw;
00165 }
00166 catch (const PGSTD::exception &e)
00167 {
00168
00169 T2.OnAbort(e.what());
00170 if (Attempts <= 0) throw;
00171 continue;
00172 }
00173 catch (...)
00174 {
00175
00176 T2.OnAbort("Unknown exception");
00177 throw;
00178 }
00179
00180 T2.OnCommit();
00181 } while (!Done);
00182 }
00183
00184
00185
00186 #endif
00187