当前位置:网站首页>Notes on MySQL transaction not automatically submitting

Notes on MySQL transaction not automatically submitting

2022-06-13 08:18:00 Parthenocissus still works

Use the company framework to write a php Script , Perform some data insertion updates, etc , The code uses mysql Business , But it's strange that the last data update will not take effect every time I cycle , And this one sql Execution is outside the transaction , And all the transactions were executed successfully , The data also returns success , If the transaction is closed , The last sentence sql It will also be executed successfully . The analytical question should be mysql Business related issues , At first, I thought it was my own code , But check , After testing for a long time, I didn't find , So consider whether it is mysql Environment setting problem , contrast , Modifying the transaction level has not been solved . Finally, the suspicion is that the original framework mysql There's something wrong with the drive . So copy the original driver (mysqli) Code for , Reuse pdo Expand and write a , When the modified procedure is written to the transaction related code , Suddenly thought of , Is it a setting autocommit =  0; As a result of . Because opening a transaction usually puts autocommit Set to 0. This is common sense , Combine the previous knowledge , every last sql In fact, it is a simple matter , So we started the test . The test code is as follows :

<?php
class testcommit extends testAffair
{
	private $chance = 5;

	public function totest()
	{
		// self::$db->beginTransaction();
		self::$db->setAutocommit(0);// Execute the replacement code for the second time 
		$data = array(
			'type' => 'test',
			'email' => '[email protected]',
			'code' => rand(0, 255),
			'times' => $this->chance,
			'ip' => '127.0.0.1',
			'createtime' => time()
		);
		$row = self::$db->insert('testms', $data);
		if(!$row){
			// echo 'rollbacked' . PHP_EOL;
			// self::$db->rollback();
			return false;
		}
		$insertid = self::$db->getInsertid();
		var_dump($row, $insertid);

		// self::$db->commit();
		return $insertid;
	}

	public function exctest()
	{
		$result = $this->totest();
		if($result){
			$res = self::$db->update('goods', array('status' => '0'), array('id' => '123'));
			var_dump($res);
			echo 'over';
		}else{
			$res = self::$db->update('goods', array('deleted' => '1'), array('id' => '123'));
			echo 'fail';
		}
	}
}

$test = new testcommit();
$test->exctest();

The annotation code is the code tested for the first time , among beginTransaction Is automatically set in autocommit by 0. and rollback and commit Is not set in . The first test was really testms The data is inserted , But no updates goods surface . The second test commented out the transaction , Only add settings autocommit=0, As expected ,testms Table and goods There are no inserts or updates . Third setting autocommit=1. The results are all successfully executed and the data is successfully submitted and effective .

To solve the last problem , After the transaction is completed, the autocommit Set to 1 that will do .

原网站

版权声明
本文为[Parthenocissus still works]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270544564146.html