Skip to content

Commit 24d4243

Browse files
committed
Reset connection when closing it + cleanup and hardening
1 parent 5b371c4 commit 24d4243

1 file changed

Lines changed: 83 additions & 72 deletions

File tree

src/mako/database/connections/Connection.php

Lines changed: 83 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -145,79 +145,129 @@ public function __clone()
145145
{
146146
$this->name = "{$this->name}_clone";
147147

148-
$this->log = [];
148+
$this->clearLog();
149149

150150
$this->transactionNestingLevel = 0;
151151

152152
$this->pdo = $this->connect();
153153
}
154154

155155
/**
156-
* Closes the database connection.
156+
* Returns the connection name.
157157
*/
158-
public function close(): void
158+
public function getName(): string
159159
{
160-
$this->pdo = null;
160+
return $this->name;
161+
}
161162

162-
$this->transactionNestingLevel = 0;
163+
/**
164+
* Returns the PDO instance or null if the connection has been closed.
165+
*/
166+
public function getPDO(): ?PDO
167+
{
168+
return $this->pdo;
163169
}
164170

165171
/**
166-
* Does the connection support transactional DDL?
172+
* Enables the query log.
167173
*/
168-
public function supportsTransactionalDDL(): bool
174+
public function enableLog(): void
169175
{
170-
return static::SUPPORTS_TRANSACTIONAL_DDL;
176+
$this->enableLog = true;
171177
}
172178

173179
/**
174-
* Returns the connection name.
180+
* Disables the query log.
175181
*/
176-
public function getName(): string
182+
public function disableLog(): void
177183
{
178-
return $this->name;
184+
$this->enableLog = false;
179185
}
180186

181187
/**
182-
* Returns a query builder helper instance.
188+
* Clears the query log.
183189
*/
184-
public function getQueryBuilderHelper(): HelperInterface
190+
public function clearLog(): void
185191
{
186-
static $queryBuilderHelper = [];
192+
$this->log = [];
193+
}
187194

188-
return $queryBuilderHelper[static::class] ?? ($queryBuilderHelper[static::class] = new $this->queryBuilderHelper);
195+
/**
196+
* Returns the query log.
197+
*/
198+
public function getLog(): array
199+
{
200+
return $this->log;
189201
}
190202

191203
/**
192-
* Returns a query compiler instance.
204+
* Resets the connection.
193205
*/
194-
public function getQueryCompiler(Query $query): Compiler
206+
public function reset(): void
195207
{
196-
return new ($this->queryCompiler)($query);
208+
if ($this->pdo === null) {
209+
return;
210+
}
211+
212+
try {
213+
// Rollback any open transactions
214+
215+
while ($this->transactionNestingLevel > 0) {
216+
$this->rollBackTransaction();
217+
}
218+
219+
// Rollback potential transaction created directly on the PDO instance
220+
221+
if ($this->pdo->inTransaction()) {
222+
$this->pdo->rollBack();
223+
}
224+
}
225+
finally {
226+
$this->clearLog();
227+
}
197228
}
198229

199230
/**
200-
* Returns the PDO instance or null if the connection has been closed.
231+
* Closes the database connection.
201232
*/
202-
public function getPDO(): ?PDO
233+
public function close(): void
203234
{
204-
return $this->pdo;
235+
try {
236+
$this->reset();
237+
}
238+
catch (Throwable) {
239+
// The connection is being closed so we don't care if the reset fails
240+
}
241+
242+
$this->pdo = null;
243+
244+
$this->transactionNestingLevel = 0;
205245
}
206246

207247
/**
208-
* Enables the query log.
248+
* Does the connection support transactional DDL?
209249
*/
210-
public function enableLog(): void
250+
public function supportsTransactionalDDL(): bool
211251
{
212-
$this->enableLog = true;
252+
return static::SUPPORTS_TRANSACTIONAL_DDL;
213253
}
214254

215255
/**
216-
* Disables the query log.
256+
* Returns a query builder helper instance.
217257
*/
218-
public function disableLog(): void
258+
public function getQueryBuilderHelper(): HelperInterface
219259
{
220-
$this->enableLog = false;
260+
static $queryBuilderHelper = [];
261+
262+
return $queryBuilderHelper[static::class] ?? ($queryBuilderHelper[static::class] = new $this->queryBuilderHelper);
263+
}
264+
265+
/**
266+
* Returns a query compiler instance.
267+
*/
268+
public function getQueryCompiler(Query $query): Compiler
269+
{
270+
return new ($this->queryCompiler)($query);
221271
}
222272

223273
/**
@@ -272,10 +322,14 @@ public function reconnect(): void
272322
*/
273323
public function isAlive(): bool
274324
{
325+
if ($this->pdo === null) {
326+
return false;
327+
}
328+
275329
try {
276330
$this->pdo->query('SELECT 1');
277331
}
278-
catch (PDOException $e) {
332+
catch (PDOException) {
279333
return false;
280334
}
281335

@@ -320,22 +374,6 @@ protected function log(string $query, array $params, float $start): void
320374
$this->log[] = ['query' => $query, 'time' => $time];
321375
}
322376

323-
/**
324-
* Clears the query log.
325-
*/
326-
public function clearLog(): void
327-
{
328-
$this->log = [];
329-
}
330-
331-
/**
332-
* Returns the query log for the connection.
333-
*/
334-
public function getLog(): array
335-
{
336-
return $this->log;
337-
}
338-
339377
/**
340378
* Prepare query and params.
341379
*/
@@ -667,7 +705,7 @@ public function getTransactionNestingLevel(): int
667705
*/
668706
public function inTransaction(): bool
669707
{
670-
return $this->pdo->inTransaction();
708+
return $this->pdo?->inTransaction() ?? false;
671709
}
672710

673711
/**
@@ -692,31 +730,4 @@ public function transaction(Closure $queries): mixed
692730

693731
return $returnValue;
694732
}
695-
696-
/**
697-
* Resets the connection.
698-
*/
699-
public function reset(): void
700-
{
701-
if ($this->pdo === null) {
702-
return;
703-
}
704-
705-
try {
706-
// Rollback any open transactions
707-
708-
while ($this->transactionNestingLevel > 0) {
709-
$this->rollBackTransaction();
710-
}
711-
712-
// Rollback potential transaction created directly on the PDO instance
713-
714-
if ($this->pdo->inTransaction()) {
715-
$this->pdo->rollBack();
716-
}
717-
}
718-
finally {
719-
$this->clearLog();
720-
}
721-
}
722733
}

0 commit comments

Comments
 (0)