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')); }
$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; } # ------------------------------------------------------------------------------ echo PHP_EOL, str_repeat('-', 80), PHP_EOL, 'Time: ', $timer->getPageLoadTime(), ' - Memory: ', $timer->getMemoryUsage(1), PHP_EOL, str_repeat('-', 80), PHP_EOL;
require_once '../../MPLT.php'; $timer = new MPLT(); require_once '../../src/dalmp.php'; # ------------------------------------------------------------------------------ $user = getenv('MYSQL_USER') ?: 'root'; $password = getenv('MYSQL_PASS') ?: ''; $host = getenv('MYSQL_HOST') ?: '127.0.0.1'; $port = getenv('MYSQL_PORT') ?: '3306'; $db = new DALMP\Database("utf8://{$user}:{$password}@{$host}:{$port}/dalmp"); $db->debug(); $db->Execute('CREATE TABLE IF NOT EXISTS t_test (id INT NOT NULL PRIMARY KEY) ENGINE=InnoDB'); $db->Execute('TRUNCATE TABLE t_test'); $db->FetchMode('ASSOC'); $db->StartTrans(); $db->Execute('INSERT INTO t_test VALUES(1)'); $db->StartTrans(); $db->Execute('INSERT INTO t_test VALUES(2)'); print_r($db->GetAll('SELECT * FROM t_test')); $db->StartTrans(); $db->Execute('INSERT INTO t_test VALUES(3)'); print_r($db->GetAll('SELECT * FROM t_test')); $db->StartTrans(); $db->Execute('INSERT INTO t_test VALUES(7)'); print_r($db->GetALL('SELECT * FROM t_test')); $db->RollBackTrans(); print_r($db->GetALL('SELECT * FROM t_test')); $db->CompleteTrans(); $db->CompleteTrans(); $db->CompleteTrans(); # ------------------------------------------------------------------------------ echo PHP_EOL, str_repeat('-', 80), PHP_EOL, 'Time: ', $timer->getPageLoadTime(), ' - Memory: ', $timer->getMemoryUsage(1), PHP_EOL, str_repeat('-', 80), PHP_EOL;