summaryrefslogtreecommitdiffstats
path: root/library/Reporting/RetryConnection.php
blob: 5f7e125ec650bb227cb8ded7427ff3d5a73290a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php

// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2

namespace Icinga\Module\Reporting;

use ipl\Sql\Connection;

class RetryConnection extends Connection
{
    public function prepexec($stmt, $values = null)
    {
        try {
            $sth = parent::prepexec($stmt, $values);
        } catch (\Exception $e) {
            $lostConnection = Str::contains($e->getMessage(), [
                'server has gone away',
                'no connection to the server',
                'Lost connection',
                'Error while sending',
                'is dead or not enabled',
                'decryption failed or bad record mac',
                'server closed the connection unexpectedly',
                'SSL connection has been closed unexpectedly',
                'Error writing data to the connection',
                'Resource deadlock avoided',
                'Transaction() on null',
                'child connection forced to terminate due to client_idle_limit',
                'query_wait_timeout',
                'reset by peer',
                'Physical connection is not usable',
                'TCP Provider: Error code 0x68',
                'ORA-03114',
                'Packets out of order. Expected',
                'Adaptive Server connection failed',
                'Communication link failure',
            ]);

            if (! $lostConnection) {
                throw $e;
            }

            $this->disconnect();

            try {
                $this->connect();
            } catch (\Exception $e) {
                $noConnection = Str::contains($e->getMessage(), [
                    'No such file or directory',
                    'Connection refused'
                ]);

                if (! $noConnection) {
                    throw $e;
                }

                \sleep(10);

                $this->connect();
            }

            $sth = parent::prepexec($stmt, $values);
        }

        return $sth;
    }
}