Skip to content

Commit 5911235

Browse files
Merge pull request #45 from michaelarnauts/patch-4
Example code for closing and opening the database is not performant
2 parents 91bd73b + 96fc22f commit 5911235

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ If you need several consumers you can list respective entries in the configurati
117117

118118
Be sure that all queues and exchanges are defined in corresponding bindings, it's up to you to set up correct message routing.
119119
#### Lifecycle events
120-
There are also some lifecycle events implemented: before_consume, after_consume, before_publish, after_publish. You can use them for any additional work you need to do before or after message been consumed/published. For example, reopen database connection for it not to be closed by a timeout as a consumer is a long-running process:
120+
There are also some lifecycle events implemented: before_consume, after_consume, before_publish, after_publish. You can use them for any additional work you need to do before or after message been consumed/published. For example, make sure that Yii knows the database connection has been closed by a timeout as a consumer is a long-running process:
121121
```php
122122
<?php
123123
// config/main.php
@@ -128,12 +128,12 @@ return [
128128
'rabbitmq' => [
129129
// ...
130130
'on before_consume' => function ($event) {
131-
if (isset(\Yii::$app->db)) {
132-
$db = \Yii::$app->db;
133-
if ($db->getIsActive()) {
134-
$db->close();
131+
if (\Yii::$app->has('db') && \Yii::$app->db->isActive) {
132+
try {
133+
\Yii::$app->db->createCommand('SELECT 1')->query();
134+
} catch (\yii\db\Exception $exception) {
135+
\Yii::$app->db->close();
135136
}
136-
$db->open();
137137
}
138138
},
139139
],

0 commit comments

Comments
 (0)