public function testTransactions() { $this->assertEquals(0, $this->db->Execute('CREATE TABLE IF NOT EXISTS t_test (id INT NOT NULL PRIMARY KEY) ENGINE=InnoDB')); $this->assertEquals(0, $this->db->Execute('TRUNCATE TABLE t_test')); $this->assertEquals(0, $this->db->StartTrans()); $this->assertTrue($this->db->Execute('INSERT INTO t_test VALUES(1)')); $this->assertEquals(0, $this->db->StartTrans()); $this->assertTrue($this->db->Execute('INSERT INTO t_test VALUES(2)')); $this->assertEquals(array(array('id' => 1), array('id' => 2)), $this->db->FetchMode('ASSOC')->GetAll('SELECT * FROM t_test')); $this->assertEquals(0, $this->db->StartTrans()); $this->assertTrue($this->db->Execute('INSERT INTO t_test VALUES(3)')); $this->assertEquals(array(array('id' => 1), array('id' => 2), array('id' => 3)), $this->db->FetchMode('ASSOC')->GetAll('SELECT * FROM t_test')); $this->assertEquals(0, $this->db->StartTrans()); $this->assertTrue($this->db->Execute('INSERT INTO t_test VALUES(7)')); $this->assertEquals(array(array('id' => 1), array('id' => 2), array('id' => 3), array('id' => 7)), $this->db->FetchMode('ASSOC')->GetAll('SELECT * FROM t_test')); $this->assertEquals(0, $this->db->RollBackTrans()); $this->assertTrue($this->db->CompleteTrans()); $this->assertTrue($this->db->CompleteTrans()); $this->assertTrue($this->db->CompleteTrans()); $this->assertEquals(array(array('id' => 1), array('id' => 2), array('id' => 3)), $this->db->FetchMode('ASSOC')->GetAll('SELECT * FROM t_test')); }
echo "In process: 2", PHP_EOL; $db = new DALMP\Database("utf8://{$user}:{$password}@{$host}:{$port}/dalmp"); $db->StartTrans(); $credit = $db->PGetOne('SELECT credit FROM t_test2 WHERE id=? FOR UPDATE', 1); if ($credit > 0) { $db->PExecute('UPDATE t_test2 SET credit=credit - ? WHERE id = ?', 100, 1); } echo "process {$i} credit: ", $db->PGetOne('SELECT credit FROM t_test2'), PHP_EOL; $rs = $db->CompleteTrans(); echo 'Transaction returned: ', (bool) $rs, PHP_EOL; exit($i); break; case 3: echo "In process: 3", PHP_EOL; $db = new DALMP\Database("utf8://{$user}:{$password}@{$host}:{$port}/dalmp"); $db->StartTrans(); $credit = $db->PGetOne('SELECT credit FROM t_test2 WHERE id=? FOR UPDATE', 1); if ($credit > 0) { $db->PExecute('UPDATE t_test2 SET credit=credit - ? WHERE id = ?', 100, 1); } echo "process {$i} credit: ", $db->PGetOne('SELECT credit FROM t_test2'), PHP_EOL; $rs = $db->CompleteTrans(); echo 'Transaction returned: ', (bool) $rs, PHP_EOL; exit($i); } } } while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child {$status} completed", PHP_EOL; }