21,886
社区成员
发帖
与我相关
我的任务
分享
<?php
class db
{
var $RID;
function db()
{
$this->RID = mysql_connect("", "root", "");
mysql_select_db("dbname");
}
function query($q)
{
mysql_query($q, $this->RID);
}
}
?>
<?php
require_once "db.php";
class a extends db
{
function a()
{
parent::db();
}
function i()
{
$this->query("begin");
$this->query("insert into a set ID=1");
require_once "b.php";
$b = new b();
if(!$b->i())
{
$this->query("rollback");
return false;
}
$this->query("commit");
}
}
?>
<?php
require_once "db.php";
class b extends db
{
function b()
{
parent::db();
}
function i()
{
$this->query("begin");
$this->query("rollback");
return false;
}
}
?>
<?php
require "a.php";
$a = new a();
var_dump($a->i());
?>
<?php
abstract class db
{
public $RID;
public function db()
{
$this->RID = mysql_connect("localhost", "root", "123456");
mysql_select_db("test");
}
public function query($q)
{
return mysql_query($q, $this->RID);
}
}
class b extends db
{
public function __construct()
{
parent::db();
}
public function myRollback()
{
return $this->query("rollback");
}
public function myBegin()
{
return $this->query("begin");
}
public function myCommit()
{
return $this->query("commit");
}
public function i()
{
$this->myBegin();
$isSuc = $this->query("insert into a (id,body)values(null,'33')");
if(!$isSuc) #可以在这里改变条件来测试rollback是否正常运行;
{
$this->myRollback();
return false;
}
$this->myCommit();# 正常情况下去掉此行则数据插入无效.证明事务正常启动.
return true;
}
}
class a extends db
{
public function __construct()
{
parent::db();
}
public function i()
{
$bDb = new b();
$bDb->myBegin();
$isSuc = $this->query("insert into a (id,body)values(null,'111111')");
if(!$isSuc) #可以在这里改变条件来测试rollback是否正常运行;
{
$bDb->myRollback();
return false;
}
$bDb->myCommit();# 正常情况下去掉此行则数据插入无效.证明事务正常启动.
return true;
}
}
$dba = new a();
$dbb = new b();
//echo $dba->i();
echo $dbb->i();
?>
<?php
abstract class db
{
public $RID;
public function db()
{
$this->RID = mysql_connect("localhost", "root", "a205a205");
mysql_select_db("test");
}
public function query($q)
{
return mysql_query($q, $this->RID);
}
}
class b extends db
{
public function myRollback()
{
return $this->query("rollback");
}
public function myBegin()
{
return $this->query("begin");
}
public function myCommit()
{
return $this->query("commit");
}
}
class a extends db
{
public function __construct()
{
parent::db();
}
public function i()
{
$bDb = new b();
$bDb->myBegin();
$isSuc = $this->query("insert into a (id,body)values(null,'111111')");
if(!$isSuc) #可以在这里改变条件来测试rollback是否正常运行;
{
$bDb->myRollback();
return false;
}
$bDb->myCommit();# 正常情况下去掉此行则数据插入无效.证明事务正常启动.
return true;
}
}
$db = new a();
echo $db->i();
?>
# 类 b
$this->query("begin");
不应再次启动.
直接回滚即可.
<?php
abstract class db
{
public $RID;
public function db()
{
$this->RID = mysql_connect("localhost", "root", "yourpsw");
mysql_select_db("test");
}
public function query($q)
{
return mysql_query($q, $this->RID);
}
}
class a extends db
{
public function __construct()
{
parent::db();
}
public function i()
{
$this->myBegin();
$isSuc = $this->query("insert into a (id,body)values(null,'heihei')");
if(!$isSuc) #可以在这里改变条件来测试rollback是否正常运行;
{
$this->myRollback();
return false;
}
$this->myCommit();
return true;
}
public function myRollback()
{
return $this->query("rollback");
}
public function myBegin()
{
return $this->query("begin");
}
public function myCommit()
{
return $this->query("commit");
}
}
$db = new a();
echo $db->i();
?>