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

transaction_base.hxx

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/transaction_base.hxx
00005  *
00006  *   DESCRIPTION
00007  *      common code and definitions for the transaction classes.
00008  *   pqxx::transaction_base defines the interface for any abstract class that
00009  *   represents a database transaction
00010  *   DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/transaction_base instead.
00011  *
00012  * Copyright (c) 2001-2004, Jeroen T. Vermeulen <jtv@xs4all.nl>
00013  *
00014  * See COPYING for copyright license.  If you did not receive a file called
00015  * COPYING with this source code, please notify the distributor of this mistake,
00016  * or contact the author.
00017  *
00018  *-------------------------------------------------------------------------
00019  */
00020 
00021 /* End-user programs need not include this file, unless they define their own
00022  * transaction classes.  This is not something the typical program should want
00023  * to do.
00024  *
00025  * However, reading this file is worthwhile because it defines the public
00026  * interface for the available transaction classes such as transaction and 
00027  * nontransaction.
00028  */
00029 
00030 #include "pqxx/connection_base"
00031 #include "pqxx/isolation"
00032 #include "pqxx/result"
00033 
00034 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
00035  */
00036 
00037 
00038 namespace pqxx
00039 {
00040 class connection_base;
00041 class transaction_base;
00042 
00043 
00044 namespace internal
00045 {
00046 class PQXX_LIBEXPORT transactionfocus : public namedclass
00047 {
00048 public:
00049   transactionfocus(transaction_base &t, 
00050       const PGSTD::string &Name,
00051       const PGSTD::string &Classname) :
00052     namedclass(Name, Classname),
00053     m_Trans(t)
00054   {
00055   }
00056 
00057 protected:
00058   void register_me();
00059   void unregister_me() throw ();
00060   void reg_pending_error(const PGSTD::string &) throw ();
00061 
00062   transaction_base &m_Trans;
00063 
00064 private:
00066   transactionfocus();
00068   transactionfocus(const transactionfocus &);
00070   transactionfocus &operator=(const transactionfocus &);
00071 };
00072 } // namespace internal
00073 
00074 
00075 
00077 
00085 class PQXX_LIBEXPORT transaction_base : public internal::namedclass
00086 {
00087   // TODO: Retry non-serializable transaction w/update only on broken_connection
00088 public:
00090   typedef isolation_traits<read_committed> isolation_tag;
00091 
00092   virtual ~transaction_base() =0;                                       //[t1]
00093 
00095 
00107   void commit();                                                        //[t1]
00108 
00110 
00113   void abort();                                                         //[t10]
00114 
00116 
00120   result exec(const char Query[], 
00121               const PGSTD::string &Desc=PGSTD::string());               //[t1]
00122 
00124 
00131   result exec(const PGSTD::string &Query,
00132               const PGSTD::string &Desc=PGSTD::string())                //[t2]
00133         { return exec(Query.c_str(), Desc); }
00134 
00136   void process_notice(const char Msg[]) const                           //[t14]
00137         { m_Conn.process_notice(Msg); }
00139   void process_notice(const PGSTD::string &Msg) const                   //[t14]
00140         { m_Conn.process_notice(Msg); }
00141 
00143   connection_base &conn() const { return m_Conn; }                      //[t4]
00144 
00146 
00154   void set_variable(const PGSTD::string &Var, const PGSTD::string &Val);//[t61]
00155 
00157 
00163   PGSTD::string get_variable(const PGSTD::string &) const;              //[t61]
00164 
00165 #ifdef PQXX_DEPRECATED_HEADERS
00166 
00167   void Commit() { commit(); }
00169   void Abort() { abort(); }
00171   result Exec(const char Q[], const PGSTD::string &D=PGSTD::string())
00172         { return exec(Q,D); }
00174   result Exec(const PGSTD::string &Q, const PGSTD::string &D=PGSTD::string())
00175         { return exec(Q,D); }
00177   void ProcessNotice(const char M[]) const { return process_notice(M); }
00179   void ProcessNotice(const PGSTD::string &M) const { return process_notice(M); }
00181   PGSTD::string Name() const { return name(); }
00183   connection_base &Conn() const { return conn(); }
00185   void SetVariable(const PGSTD::string &Var, const PGSTD::string &Val)
00186         { set_variable(Var,Val); }
00187 #endif
00188 
00189 protected:
00191 
00194   explicit transaction_base(connection_base &, 
00195                           const PGSTD::string &TName,
00196                           const PGSTD::string &CName);
00197 
00199 
00201   void Begin();
00202 
00204   void End() throw ();
00205 
00207   virtual void do_begin() =0;
00209   virtual result do_exec(const char Query[]) =0;
00211   virtual void do_commit() =0;
00213   virtual void do_abort() =0;
00214 
00215   // For use by implementing class:
00216 
00218 
00226   result DirectExec(const char C[], int Retries=0);
00227  
00228 private:
00229   /* A transaction goes through the following stages in its lifecycle:
00230    *  - nascent: the transaction hasn't actually begun yet.  If our connection 
00231    *    fails at this stage, it may recover and the transaction can attempt to
00232    *    establish itself again.
00233    *  - active: the transaction has begun.  Since no commit command has been 
00234    *    issued, abortion is implicit if the connection fails now.
00235    *  - aborted: an abort has been issued; the transaction is terminated and 
00236    *    its changes to the database rolled back.  It will accept no further 
00237    *    commands.
00238    *  - committed: the transaction has completed successfully, meaning that a 
00239    *    commit has been issued.  No further commands are accepted.
00240    *  - in_doubt: the connection was lost at the exact wrong time, and there is
00241    *    no way of telling whether the transaction was committed or aborted.
00242    *
00243    * Checking and maintaining state machine logic is the responsibility of the 
00244    * base class (ie., this one).
00245    */
00246   enum Status 
00247   { 
00248     st_nascent, 
00249     st_active, 
00250     st_aborted, 
00251     st_committed,
00252     st_in_doubt
00253   };
00254 
00255 
00256   void CheckPendingError();
00257 
00258   friend class Cursor;
00259   int GetUniqueCursorNum() { return m_UniqueCursorNum++; }
00260   void MakeEmpty(result &R) const { m_Conn.MakeEmpty(R); }
00261 
00262   friend class internal::transactionfocus;
00263   void RegisterFocus(internal::transactionfocus *);
00264   void UnregisterFocus(internal::transactionfocus *) throw ();
00265   void RegisterPendingError(const PGSTD::string &) throw ();
00266   friend class tablereader;
00267   void BeginCopyRead(const PGSTD::string &Table);
00268   bool ReadCopyLine(PGSTD::string &L) { return m_Conn.ReadCopyLine(L); }
00269   friend class tablewriter;
00270   void BeginCopyWrite(const PGSTD::string &Table);
00271   bool WriteCopyLine(const PGSTD::string &L, bool async=false) 
00272         { return m_Conn.WriteCopyLine(L, async); }
00273   void EndCopyWrite() throw () { m_Conn.EndCopyWrite(); }
00274 
00275   friend class pipeline;
00276   void start_exec(const PGSTD::string &Q) { m_Conn.start_exec(Q); }
00277   PGresult *get_result() { return m_Conn.get_result(); }
00278 
00279   connection_base &m_Conn;
00280 
00281   int m_UniqueCursorNum;
00282   internal::unique<internal::transactionfocus> m_Focus;
00283   Status m_Status;
00284   bool m_Registered;
00285   mutable PGSTD::map<PGSTD::string, PGSTD::string> m_Vars;
00286   PGSTD::string m_PendingError;
00287 
00289   transaction_base();
00291   transaction_base(const transaction_base &);
00293   transaction_base &operator=(const transaction_base &);
00294 };
00295 
00296 } // namespace pqxx
00297 
00298 

Generated on Thu Feb 19 22:04:36 2004 for libpqxx by doxygen 1.3.5