@@ -32,6 +32,8 @@ class Magento2DbConnection
3232 // 1 MB / about 270 bytes per path
3333 const REQUEST_PATHS_PER_CHUNK = 3500 ;
3434
35+ const MAX_CONNECTION_RETRIES = 10 ;
36+
3537 /** @var ResourceConnection $connection */
3638 protected $ connection ;
3739
@@ -48,8 +50,13 @@ public function __construct(ResourceConnection $connection)
4850 {
4951 $ this ->connection = $ connection ;
5052
53+ $ this ->connect ();
54+ }
55+
56+ protected function connect ()
57+ {
5158 /** @var Mysql $mysql */
52- $ mysql = $ connection ->getConnection ();
59+ $ mysql = $ this -> connection ->getConnection ();
5360
5461 /** @var PDO $pdo */
5562 $ this ->pdo = $ mysql ->getConnection ();
@@ -79,6 +86,55 @@ protected function calculateMaxAllowedPacket()
7986 * @return \PDOStatement
8087 */
8188 public function execute (string $ query , $ values = [])
89+ {
90+ $ connectionErrors = [
91+ 2006 , // SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
92+ 2013 , // SQLSTATE[HY000]: General error: 2013 Lost connection to MySQL server during query
93+ ];
94+ $ triesCount = 0 ;
95+
96+ do {
97+ $ retry = false ;
98+ try {
99+ return $ this ->executeQuery ($ query , $ values );
100+ } catch (\Exception $ e ) {
101+ /** @var $pdoException \PDOException */
102+ $ pdoException = null ;
103+ if ($ e instanceof \PDOException) {
104+ $ pdoException = $ e ;
105+ } elseif (($ e instanceof Zend_Db_Statement_Exception)
106+ && ($ e ->getPrevious () instanceof \PDOException)
107+ ) {
108+ $ pdoException = $ e ->getPrevious ();
109+ }
110+
111+ // Check to reconnect
112+ if ($ pdoException && $ triesCount < self ::MAX_CONNECTION_RETRIES
113+ && in_array ($ pdoException ->errorInfo [1 ], $ connectionErrors )
114+ ) {
115+ $ retry = true ;
116+ $ triesCount ++;
117+
118+ $ this ->connection ->closeConnection ();
119+
120+ $ this ->connect ();
121+ }
122+
123+ if (!$ retry ) {
124+ throw $ e ;
125+ }
126+ }
127+ } while ($ retry );
128+ }
129+
130+ /**
131+ * Prepares and executes an SQL query or statement
132+ *
133+ * @param string $query
134+ * @param array $values
135+ * @return \PDOStatement
136+ */
137+ protected function executeQuery (string $ query , $ values = [])
82138 {
83139#echo $query . "\n";
84140
@@ -115,7 +171,7 @@ public function execute(string $query, $values = [])
115171 public function insertMultiple (string $ table , array $ columns , array $ values , int $ magnitude )
116172 {
117173 $ this ->chunkedGroupExecute ("
118- INSERT INTO ` {$ table }` (` " . implode ('`, ` ' , $ columns ) . "`)
174+ INSERT INTO ` {$ table }` (` " . implode ('`, ` ' , $ columns ) . "`)
119175 VALUES {{marks}} " ,
120176 $ columns , $ values , $ magnitude
121177 );
@@ -132,7 +188,7 @@ public function insertMultiple(string $table, array $columns, array $values, int
132188 public function replaceMultiple (string $ table , array $ columns , array $ values , int $ magnitude )
133189 {
134190 $ this ->chunkedGroupExecute ("
135- REPLACE INTO ` {$ table }` (` " . implode ('`, ` ' , $ columns ) . "`)
191+ REPLACE INTO ` {$ table }` (` " . implode ('`, ` ' , $ columns ) . "`)
136192 VALUES {{marks}} " ,
137193 $ columns , $ values , $ magnitude
138194 );
@@ -151,7 +207,7 @@ public function replaceMultiple(string $table, array $columns, array $values, in
151207 public function insertMultipleWithUpdate (string $ table , array $ columns , array $ values , int $ magnitude , string $ updateClause )
152208 {
153209 $ this ->chunkedGroupExecute ("
154- INSERT INTO ` {$ table }` (` " . implode ('`, ` ' , $ columns ) . "`)
210+ INSERT INTO ` {$ table }` (` " . implode ('`, ` ' , $ columns ) . "`)
155211 VALUES {{marks}}
156212 ON DUPLICATE KEY UPDATE {$ updateClause }" ,
157213 $ columns , $ values , $ magnitude
@@ -170,7 +226,7 @@ public function insertMultipleWithUpdate(string $table, array $columns, array $v
170226 public function insertMultipleWithIgnore (string $ table , array $ columns , array $ values , int $ magnitude )
171227 {
172228 $ this ->chunkedGroupExecute ("
173- INSERT IGNORE INTO ` {$ table }` (` " . implode ('`, ` ' , $ columns ) . "`)
229+ INSERT IGNORE INTO ` {$ table }` (` " . implode ('`, ` ' , $ columns ) . "`)
174230 VALUES {{marks}} " ,
175231 $ columns , $ values , $ magnitude
176232 );
@@ -187,7 +243,7 @@ public function deleteMultiple(string $table, string $keyColumn, array $keys)
187243 {
188244 foreach (array_chunk ($ keys , self ::DELETES_PER_CHUNK ) as $ chunk ) {
189245 $ this ->execute ("
190- DELETE FROM` {$ table }`
246+ DELETE FROM` {$ table }`
191247 WHERE ` {$ keyColumn }` IN ( " . $ this ->getMarks ($ chunk ) . ") " ,
192248 $ chunk );
193249 }
@@ -366,4 +422,4 @@ public function getFullTableName(string $table)
366422 {
367423 return $ this ->connection ->getTableName ($ table );
368424 }
369- }
425+ }
0 commit comments