transaction.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * Overriden to bring some commonsense to transaction management.
  4. *
  5. * The "sane" approach is to require explicit transaction commits!
  6. *
  7. * Drupal uses the other way round, commits are implicit unless you explicitly
  8. * rollback.
  9. */
  10. class DatabaseTransaction_sqlsrv extends DatabaseTransaction {
  11. /**
  12. * A boolean value to indicate whether this transaction has been commited.
  13. *
  14. * @var Boolean
  15. */
  16. protected $commited = FALSE;
  17. /**
  18. * A boolean to indicate if the transaction scope should behave sanely.
  19. *
  20. * @var DatabaseTransactionSettings
  21. */
  22. protected $settings = FALSE;
  23. /**
  24. * Overriden to add settings.
  25. *
  26. * @param DatabaseConnection $connection
  27. * @param mixed $name
  28. * @param mixed $sane
  29. */
  30. public function __construct(DatabaseConnection $connection, $name = NULL, $settings = NULL) {
  31. $this->settings = $settings;
  32. $this->connection = $connection;
  33. // If there is no transaction depth, then no transaction has started. Name
  34. // the transaction 'drupal_transaction'.
  35. if (!$depth = $connection->transactionDepth()) {
  36. $this->name = 'drupal_transaction';
  37. }
  38. // Within transactions, savepoints are used. Each savepoint requires a
  39. // name. So if no name is present we need to create one.
  40. elseif (empty($name)) {
  41. $this->name = 'savepoint_' . $depth;
  42. }
  43. else {
  44. $this->name = $name;
  45. }
  46. $this->connection->pushTransaction($this->name, $settings);
  47. }
  48. /**
  49. * Overriden __desctur to provide some mental health.
  50. */
  51. public function __destruct() {
  52. if (!$this->settings->Get_Sane()) {
  53. // If we rolled back then the transaction would have already been popped.
  54. if (!$this->rolledBack) {
  55. $this->connection->popTransaction($this->name);
  56. }
  57. }
  58. else {
  59. // If we did not commit and did not rollback explicitly, rollback.
  60. // Rollbacks are not usually called explicitly by the user
  61. // but that could happen.
  62. if (!$this->commited && !$this->rolledBack) {
  63. $this->rollback();
  64. }
  65. }
  66. }
  67. /**
  68. * The "sane" behaviour requires explicit commits.
  69. *
  70. * @throws DatabaseTransactionExplicitCommitNotAllowedException
  71. */
  72. public function commit() {
  73. if (!$this->settings->Get_Sane()) {
  74. throw new DatabaseTransactionExplicitCommitNotAllowedException();
  75. }
  76. // Cannot commit a rolledback transaction...
  77. if ($this->rolledBack) {
  78. throw new DatabaseTransactionCannotCommitAfterRollbackException();
  79. }
  80. // Mark as commited, and commit!
  81. $this->commited = TRUE;
  82. $this->connection->popTransaction($this->name);
  83. }
  84. }
  85. /**
  86. * Like db_transaction() but transaction behaviour is more
  87. * sane requiring explicit commits.
  88. *
  89. * @param mixed $name
  90. * @param array $options
  91. * @return DatabaseTransaction
  92. */
  93. function db_transaction_sane(DatabaseTransactionSettings $settings = NULL) {
  94. if ($settings == NULL) {
  95. $settings = DatabaseTransactionSettings::GetBetterDefaults();
  96. }
  97. return Database::getConnection()->startTransaction(NULL, $settings);
  98. }
  99. /**
  100. * Thrown when the user is trying to commit a rollbacked transaction.
  101. */
  102. class DatabaseTransactionCannotCommitAfterRollbackException extends Exception { }
  103. /**
  104. * Available transaction isolation levels.
  105. */
  106. class DatabaseTransactionIsolationLevel extends Enum {
  107. const ReadUncommitted = 'READ UNCOMMITTED';
  108. const ReadCommitted = 'READ COMMITTED';
  109. const RepeatableRead = 'REPEATABLE READ';
  110. const Snapshot = 'SNAPSHOT';
  111. const Serializable = 'SERIALIZABLE';
  112. const Chaos = 'CHAOS';
  113. const Ignore = 'IGNORE';
  114. }
  115. /**
  116. * Summary of DatabaseTransactionScopeOption
  117. */
  118. class DatabaseTransactionScopeOption extends Enum {
  119. const RequiresNew = 'RequiresNew';
  120. const Supress = 'Supress';
  121. const Required = 'Required';
  122. }
  123. /**
  124. * Behaviour settings for a transaction.
  125. */
  126. class DatabaseTransactionSettings {
  127. /**
  128. * Summary of __construct
  129. * @param mixed $Sane
  130. * @param DatabaseTransactionScopeOption $ScopeOption
  131. * @param DatabaseTransactionIsolationLevel $IsolationLevel
  132. */
  133. public function __construct($Sane = FALSE,
  134. DatabaseTransactionScopeOption $ScopeOption = NULL,
  135. DatabaseTransactionIsolationLevel $IsolationLevel = NULL) {
  136. $this->_Sane = $Sane;
  137. if ($ScopeOption == NULL) {
  138. $ScopeOption = DatabaseTransactionScopeOption::RequiresNew();
  139. }
  140. if ($IsolationLevel == NULL) {
  141. $IsolationLevel = DatabaseTransactionIsolationLevel::Unspecified();
  142. }
  143. $this->_IsolationLevel = $IsolationLevel;
  144. $this->_ScopeOption = $ScopeOption;
  145. }
  146. // @var DatabaseTransactionIsolationLevel
  147. private $_IsolationLevel;
  148. // @var DatabaseTransactionScopeOption
  149. private $_ScopeOption;
  150. // @var Boolean
  151. private $_Sane;
  152. /**
  153. * Summary of Get_IsolationLevel
  154. * @return mixed
  155. */
  156. public function Get_IsolationLevel() {
  157. return $this->_IsolationLevel;
  158. }
  159. /**
  160. * Summary of Get_ScopeOption
  161. * @return mixed
  162. */
  163. public function Get_ScopeOption() {
  164. return $this->_ScopeOption;
  165. }
  166. /**
  167. * Summary of Get_Sane
  168. * @return mixed
  169. */
  170. public function Get_Sane() {
  171. return $this->_Sane;
  172. }
  173. /**
  174. * Returns a default setting system-wide.
  175. *
  176. * @return DatabaseTransactionSettings
  177. */
  178. public static function GetDefaults() {
  179. // Use snapshot if available.
  180. $isolation = DatabaseTransactionIsolationLevel::Ignore();
  181. if ($info = \Database::getConnection()->schema()->getDatabaseInfo()) {
  182. if ($info->snapshot_isolation_state == TRUE) {
  183. // Some DDL operations on core will fail with snapshot.
  184. $isolation = DatabaseTransactionIsolationLevel::ReadCommitted();
  185. }
  186. }
  187. // Otherwise use Drupal's default behaviour (except for nesting!)
  188. return new DatabaseTransactionSettings(FALSE,
  189. DatabaseTransactionScopeOption::Required(),
  190. $isolation);
  191. }
  192. /**
  193. * Proposed better defaults.
  194. *
  195. * @return DatabaseTransactionSettings
  196. */
  197. public static function GetBetterDefaults() {
  198. // Use snapshot if available.
  199. $isolation = DatabaseTransactionIsolationLevel::Ignore();
  200. if ($info = \Database::getConnection()->schema()->getDatabaseInfo()) {
  201. if ($info->snapshot_isolation_state == TRUE) {
  202. $isolation = DatabaseTransactionIsolationLevel::Snapshot();
  203. }
  204. }
  205. // Otherwise use Drupal's default behaviour (except for nesting!)
  206. return new DatabaseTransactionSettings(TRUE,
  207. DatabaseTransactionScopeOption::Required(),
  208. $isolation);
  209. }
  210. /**
  211. * Snapshot isolation is not compatible with DDL operations.
  212. *
  213. * @return DatabaseTransactionSettings
  214. */
  215. public static function GetDDLCompatibleDefaults() {
  216. return new DatabaseTransactionSettings(TRUE,
  217. DatabaseTransactionScopeOption::Required(),
  218. DatabaseTransactionIsolationLevel::ReadCommitted());
  219. }
  220. }