dbtransaction.hxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /** Definition of the pqxx::dbtransaction abstract base class.
  2. *
  3. * pqxx::dbransaction defines a real transaction on the database.
  4. *
  5. * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/dbtransaction instead.
  6. *
  7. * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
  8. *
  9. * See COPYING for copyright license. If you did not receive a file called
  10. * COPYING with this source code, please notify the distributor of this mistake,
  11. * or contact the author.
  12. */
  13. #ifndef PQXX_H_DBTRANSACTION
  14. #define PQXX_H_DBTRANSACTION
  15. #include "pqxx/compiler-public.hxx"
  16. #include "pqxx/compiler-internal-pre.hxx"
  17. #include "pqxx/transaction_base.hxx"
  18. namespace pqxx
  19. {
  20. enum readwrite_policy
  21. {
  22. read_only,
  23. read_write
  24. };
  25. /// Abstract base class responsible for bracketing a backend transaction.
  26. /**
  27. * @ingroup transaction
  28. *
  29. * Use a dbtransaction-derived object such as "work" (transaction<>) to enclose
  30. * operations on a database in a single "unit of work." This ensures that the
  31. * whole series of operations either succeeds as a whole or fails completely.
  32. * In no case will it leave half-finished work behind in the database.
  33. *
  34. * Once processing on a transaction has succeeded and any changes should be
  35. * allowed to become permanent in the database, call commit(). If something
  36. * has gone wrong and the changes should be forgotten, call abort() instead.
  37. * If you do neither, an implicit abort() is executed at destruction time.
  38. *
  39. * It is an error to abort a transaction that has already been committed, or to
  40. * commit a transaction that has already been aborted. Aborting an already
  41. * aborted transaction or committing an already committed one has been allowed
  42. * to make errors easier to deal with. Repeated aborts or commits have no
  43. * effect after the first one.
  44. *
  45. * Database transactions are not suitable for guarding long-running processes.
  46. * If your transaction code becomes too long or too complex, please consider
  47. * ways to break it up into smaller ones. There's no easy, general way to do
  48. * this since application-specific considerations become important at this
  49. * point.
  50. *
  51. * The actual operations for beginning and committing/aborting the backend
  52. * transaction are implemented by a derived class. The implementing concrete
  53. * class must also call Begin() and End() from its constructors and destructors,
  54. * respectively, and implement do_exec().
  55. */
  56. class PQXX_LIBEXPORT PQXX_NOVTABLE dbtransaction : public transaction_base
  57. {
  58. public:
  59. virtual ~dbtransaction();
  60. protected:
  61. dbtransaction(
  62. connection_base &,
  63. const std::string &IsolationString,
  64. readwrite_policy rw=read_write);
  65. explicit dbtransaction(
  66. connection_base &,
  67. bool direct=true,
  68. readwrite_policy rw=read_write);
  69. /// Start a transaction on the backend and set desired isolation level
  70. void start_backend_transaction();
  71. /// Sensible default implemented here: begin backend transaction
  72. virtual void do_begin() override; //[t01]
  73. /// Sensible default implemented here: perform query
  74. virtual result do_exec(const char Query[]) override;
  75. /// To be implemented by derived class: commit backend transaction
  76. virtual void do_commit() override =0;
  77. /// Sensible default implemented here: abort backend transaction
  78. /** Default implementation does two things:
  79. * <ol>
  80. * <li>Clears the "connection reactivation avoidance counter"</li>
  81. * <li>Executes a ROLLBACK statement</li>
  82. * </ol>
  83. */
  84. virtual void do_abort() override; //[t13]
  85. static std::string fullname(const std::string &ttype,
  86. const std::string &isolation);
  87. private:
  88. /// Precomputed SQL command to run at start of this transaction
  89. std::string m_start_cmd;
  90. };
  91. } // namespace pqxx
  92. #include "pqxx/compiler-internal-post.hxx"
  93. #endif