Advertisement
Guest User

Test

a guest
Mar 16th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. <?php
  2.  
  3. function waitUser() {
  4. echo "Sleep. Press enter...\n";
  5. $line = trim(fgets(STDIN));
  6. fscanf(STDIN, "\n");
  7. echo "Done. Commiting.\n";
  8. }
  9.  
  10. class MysqlConnect
  11. {
  12.  
  13. protected $link = null;
  14. protected $host = null;
  15.  
  16. public function __construct($host, $database, $username, $password)
  17. {
  18. $this->host = $host;
  19. $this->link = mysql_connect($host, $username, $password, true);
  20. mysql_select_db($database, $this->link);
  21. }
  22.  
  23. private function log($message) {
  24. echo "[{$this->host}] {$message}\n";
  25. }
  26.  
  27. public function query($query)
  28. {
  29. $this->log($query);
  30. $res = mysql_query($query, $this->link) or $this->log("ERROR: " . mysql_error());
  31.  
  32. return $res;
  33. }
  34.  
  35. public function fetchAll($query)
  36. {
  37. $res = $this->query($query);
  38.  
  39. $result = array();
  40. while ($row = mysql_fetch_assoc($res)) {
  41. $result[] = $row;
  42. }
  43. return $res;
  44. }
  45.  
  46. public function insert($table, $data)
  47. {
  48. if (!count($data))
  49. return false;
  50.  
  51. list($row) = $data;
  52. $cols = array();
  53. foreach ($row as $key => $value) {
  54. $cols[] = $key;
  55. }
  56. $cols = implode(", ", $cols);
  57.  
  58. $values = array();
  59. foreach ($data as $row) {
  60. $values[] = "'" . implode("', '", $row) . "'";
  61. }
  62.  
  63. $values = "(" . implode("), (", $values) . ")";
  64.  
  65. $query = "INSERT INTO `{$table}` ($cols) VALUES {$values}";
  66.  
  67. $this->query($query);
  68. }
  69. }
  70.  
  71. class ConnectManager
  72. {
  73. protected $connections = array();
  74. protected $firstConnection = null;
  75.  
  76. protected $lastTxId = null;
  77.  
  78. public function __construct($connections)
  79. {
  80. $this->connections = $connections;
  81. $this->firstConnection = array_shift($connections);
  82. }
  83.  
  84. public function query($query)
  85. {
  86. foreach ($this->connections as $connection) {
  87. $connection->query($query);
  88. }
  89. }
  90.  
  91. public function fetchAll($query)
  92. {
  93. foreach ($this->connections as $connection) {
  94. $connection->fetchAll($query);
  95. }
  96. }
  97.  
  98. public function insert($table, $data)
  99. {
  100. foreach ($this->connections as $connection) {
  101. $connection->insert($table, $data);
  102. }
  103. }
  104.  
  105. protected function getTxIdent()
  106. {
  107. return uniqid("tx-", true);
  108. }
  109.  
  110. public function txBegin()
  111. {
  112. $tid = $this->getTxIdent();
  113. $this->lastTxId = $tid;
  114. foreach ($this->connections as $connection) {
  115. $connection->query("XA START '{$tid}'");
  116. }
  117. }
  118.  
  119. public function txCommit()
  120. {
  121. $tid = $this->lastTxId;
  122. foreach ($this->connections as $connection) {
  123. // $connection = $this->firstConnection;
  124. $connection->query("XA END '{$tid}'");
  125. $connection->query("XA PREPARE '{$tid}'");
  126. }
  127.  
  128. waitUser();
  129.  
  130. foreach ($this->connections as $connection) {
  131. $connection->query("XA COMMIT '{$tid}'");
  132. }
  133. }
  134.  
  135. public function txRollback()
  136. {
  137. $tid = $this->lastTxId;
  138. foreach ($this->connections as $connection) {
  139. $connection->query("XA END '{$tid}'");
  140. $connection->query("XA RALLBACK '{$tid}'");
  141. }
  142. }
  143.  
  144. }
  145.  
  146. $int = rand(0, 999);
  147. echo "LAST INT: {$int}\n";
  148.  
  149. $c1 = new MysqlConnect("localhost", "xa_test", "user", "password");
  150. $c2 = new MysqlConnect("192.168.56.10", "xa_test", "user", "password");
  151.  
  152. $man = new ConnectManager(array($c1, $c2));
  153. $man->txBegin();
  154.  
  155. $i = $int;
  156. $c1->insert("some_table", array(array("field" => "field {$i}", "value" => rand(0, 999))));
  157. $c2->insert("some_table", array(array("field" => "field {$i}", "value" => rand(0, 999))));
  158.  
  159. $man->txCommit();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement