Skip to content

Commit 00eee11

Browse files
limingxinleohuangzhhui
authored andcommitted
修改DB事务回滚BUG (swoft-cloud/swoft-component#173)
1 parent c51130a commit 00eee11

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Driver/Mysql/MysqlConnection.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,13 @@ public function execute(array $params = [])
106106
public function receive()
107107
{
108108
$result = $this->connection->recv();
109+
$this->recv = true;
110+
109111
if ($result === false) {
110112
throw new MysqlException('Mysql recv error,connectError=' . $this->connection->connect_error . ' error=' . $this->connection->error);
111113
}
112114
$this->connection->setDefer(false);
113115

114-
$this->recv = true;
115116
$this->result = $result;
116117

117118
return $result;

test/Cases/Mysql/SqlTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,35 @@ public function testUpdateByCo($id)
142142
$this->testUpdate($id);
143143
});
144144
}
145+
146+
public function testErrorSql()
147+
{
148+
Db::beginTransaction();
149+
150+
try {
151+
$user = new User();
152+
$user->setName('limx');
153+
$user->setSex(1);
154+
$user->setAge(27);
155+
$user->setDesc('Swoft');
156+
$id = $user->save()->getResult();
157+
158+
$sql = 'SELECT des FROM `user` WHERE id = ?';
159+
$res = Db::query($sql, [$id])->getResult();
160+
$this->assertTrue(false);
161+
Db::commit();
162+
} catch (\Exception $ex) {
163+
Db::rollback();
164+
165+
$user = User::findById($id)->getResult();
166+
$this->assertNull($user);
167+
}
168+
}
169+
170+
public function testErrorSqlByCo()
171+
{
172+
go(function () {
173+
$this->testErrorSql();
174+
});
175+
}
145176
}

0 commit comments

Comments
 (0)