Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function waitUser() {
- echo "Sleep. Press enter...\n";
- $line = trim(fgets(STDIN));
- fscanf(STDIN, "\n");
- echo "Done. Commiting.\n";
- }
- class MysqlConnect
- {
- protected $link = null;
- protected $host = null;
- public function __construct($host, $database, $username, $password)
- {
- $this->host = $host;
- $this->link = mysql_connect($host, $username, $password, true);
- mysql_select_db($database, $this->link);
- }
- private function log($message) {
- echo "[{$this->host}] {$message}\n";
- }
- public function query($query)
- {
- $this->log($query);
- $res = mysql_query($query, $this->link) or $this->log("ERROR: " . mysql_error());
- return $res;
- }
- public function fetchAll($query)
- {
- $res = $this->query($query);
- $result = array();
- while ($row = mysql_fetch_assoc($res)) {
- $result[] = $row;
- }
- return $res;
- }
- public function insert($table, $data)
- {
- if (!count($data))
- return false;
- list($row) = $data;
- $cols = array();
- foreach ($row as $key => $value) {
- $cols[] = $key;
- }
- $cols = implode(", ", $cols);
- $values = array();
- foreach ($data as $row) {
- $values[] = "'" . implode("', '", $row) . "'";
- }
- $values = "(" . implode("), (", $values) . ")";
- $query = "INSERT INTO `{$table}` ($cols) VALUES {$values}";
- $this->query($query);
- }
- }
- class ConnectManager
- {
- protected $connections = array();
- protected $firstConnection = null;
- protected $lastTxId = null;
- public function __construct($connections)
- {
- $this->connections = $connections;
- $this->firstConnection = array_shift($connections);
- }
- public function query($query)
- {
- foreach ($this->connections as $connection) {
- $connection->query($query);
- }
- }
- public function fetchAll($query)
- {
- foreach ($this->connections as $connection) {
- $connection->fetchAll($query);
- }
- }
- public function insert($table, $data)
- {
- foreach ($this->connections as $connection) {
- $connection->insert($table, $data);
- }
- }
- protected function getTxIdent()
- {
- return uniqid("tx-", true);
- }
- public function txBegin()
- {
- $tid = $this->getTxIdent();
- $this->lastTxId = $tid;
- foreach ($this->connections as $connection) {
- $connection->query("XA START '{$tid}'");
- }
- }
- public function txCommit()
- {
- $tid = $this->lastTxId;
- foreach ($this->connections as $connection) {
- // $connection = $this->firstConnection;
- $connection->query("XA END '{$tid}'");
- $connection->query("XA PREPARE '{$tid}'");
- }
- waitUser();
- foreach ($this->connections as $connection) {
- $connection->query("XA COMMIT '{$tid}'");
- }
- }
- public function txRollback()
- {
- $tid = $this->lastTxId;
- foreach ($this->connections as $connection) {
- $connection->query("XA END '{$tid}'");
- $connection->query("XA RALLBACK '{$tid}'");
- }
- }
- }
- $int = rand(0, 999);
- echo "LAST INT: {$int}\n";
- $c1 = new MysqlConnect("localhost", "xa_test", "user", "password");
- $c2 = new MysqlConnect("192.168.56.10", "xa_test", "user", "password");
- $man = new ConnectManager(array($c1, $c2));
- $man->txBegin();
- $i = $int;
- $c1->insert("some_table", array(array("field" => "field {$i}", "value" => rand(0, 999))));
- $c2->insert("some_table", array(array("field" => "field {$i}", "value" => rand(0, 999))));
- $man->txCommit();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement