Skip to content

Commit 6791ece

Browse files
committed
Release if exception is thrown
1 parent 95b3896 commit 6791ece

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/ConnectionTransaction.php

+19-5
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,17 @@ public function query(string $sql): Promise
116116

117117
return call(function () use ($sql) {
118118
++$this->refCount;
119-
$result = yield $this->handle->query($sql);
119+
try {
120+
$result = yield $this->handle->query($sql);
121+
} finally {
122+
($this->release)();
123+
}
120124

121125
if ($result instanceof ResultSet) {
126+
++$this->refCount;
122127
return new PooledResultSet($result, $this->release);
123128
}
124129

125-
($this->release)();
126130
return $result;
127131
});
128132
}
@@ -140,7 +144,13 @@ public function prepare(string $sql): Promise
140144

141145
return call(function () use ($sql) {
142146
++$this->refCount;
143-
$statement = yield $this->handle->prepare($sql);
147+
try {
148+
$statement = yield $this->handle->prepare($sql);
149+
} catch (\Throwable $exception) {
150+
($this->release)();
151+
throw $exception;
152+
}
153+
144154
return new PooledStatement($statement, $this->release);
145155
});
146156
}
@@ -158,13 +168,17 @@ public function execute(string $sql, array $params = []): Promise
158168

159169
return call(function () use ($sql, $params) {
160170
++$this->refCount;
161-
$result = yield $this->handle->execute($sql, $params);
171+
try {
172+
$result = yield $this->handle->execute($sql, $params);
173+
} finally {
174+
($this->release)();
175+
}
162176

163177
if ($result instanceof ResultSet) {
178+
++$this->refCount;
164179
return new PooledResultSet($result, $this->release);
165180
}
166181

167-
($this->release)();
168182
return $result;
169183
});
170184
}

0 commit comments

Comments
 (0)