Home php教程 PHP源码 PHP简单的事务测试用例

PHP简单的事务测试用例

May 26, 2016 am 08:18 AM


 show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
// schemal
create table transaction_tbl (
	name char(10) not null
) engine=innodb;

*/

class Transaction 
{
	// 判断插入查询是否成功
	private $error = 0;
	
	// 连接资源
	private $conn_id = null;
	
	public function __construct()
	{
		$this->connect();
	}
	
	public function test()
	{
		echo "BEGIN TEST\n";

		//开始事务
		$this->beginTransaction();
		// 插入测试数据
		// name 的大小限制为10
		$str1 = str_repeat("a", mt_rand(1, 13));
		$str2 = str_repeat("b", mt_rand(1, 13));

		$this->query("INSERT INTO `transaction_tbl` (`name`) VALUES ('$str1')");
		$this->query("INSERT INTO `transaction_tbl` (`name`) VALUES ('$str2')");			
		
		//提交查询
		if ($this->error == 0)
		{
			$this->commit();
		}
		else
		{
			$this->rollback();
		}
		
		echo "END TEST\n";
	}
	
	private function query($sql)
	{
		echo $sql."\n";
		(mysql_query($sql, $this->conn_id) == FALSE) && ++$this->error;
	}
	
	// 开始事务
	private function beginTransaction()
	{
		mysql_query("BEGIN", $this->conn_id);
	}
	//回滚
	private function rollback()
	{
		echo "ROLLBACK\n";
		mysql_query("ROLLBACK", $this->conn_id);	
	}
	//提交
	private function commit()
	{
		echo "save done\n";
		mysql_query("COMMIT", $this->conn_id);
	}
	
	// 连接数据库
	private function connect()
	{
		$this->conn_id = @mysql_connect("127.0.0.1", "root", "") or die("can\'t connect: ". mysql_error());
		mysql_select_db("test", $this->conn_id) or die("can\'t use test: ". mysql_error());
	}
	// 关闭数据
	private function close()
	{
		mysql_close($this->conn_id);
	}	
	
	public function __destruct()
	{
		$this->close();
	}		
}

$trans = new Transaction();
$trans->test();
Copy after login

                   

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)