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