Skip to content

Commit 0e67ac9

Browse files
authored
Merge pull request #3744 from magento-thunder/MAGETWO-97326
Fixed issues: - MAGETWO-97326: [Backport 2.1.x] Mysql reconnect does not work
2 parents 6f81a1b + 04b0410 commit 0e67ac9

File tree

3 files changed

+250
-77
lines changed

3 files changed

+250
-77
lines changed

dev/tests/integration/testsuite/Magento/Framework/DB/Adapter/Pdo/MysqlTest.php

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,26 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
7-
// @codingStandardsIgnoreFile
8-
9-
/**
10-
* Test for an PDO MySQL adapter
11-
*/
126
namespace Magento\Framework\DB\Adapter\Pdo;
137

148
use Magento\Framework\App\ResourceConnection;
15-
use Zend_Db_Statement_Exception;
9+
use Magento\TestFramework\Helper\Bootstrap;
1610

1711
class MysqlTest extends \PHPUnit_Framework_TestCase
1812
{
1913
/**
20-
* Database adapter instance
21-
*
22-
* @var \Magento\Framework\DB\Adapter\Pdo\Mysql
14+
* @var ResourceConnection
2315
*/
24-
protected $_connection = null;
16+
private $resourceConnection;
2517

26-
public function setUp()
18+
protected function setUp()
2719
{
2820
set_error_handler(null);
21+
$this->resourceConnection = Bootstrap::getObjectManager()
22+
->get(ResourceConnection::class);
2923
}
3024

31-
public function tearDown()
25+
protected function tearDown()
3226
{
3327
restore_error_handler();
3428
}
@@ -40,30 +34,20 @@ public function tearDown()
4034
*/
4135
public function testWaitTimeout()
4236
{
43-
if (!$this->_getConnection() instanceof \Magento\Framework\DB\Adapter\Pdo\Mysql) {
37+
if (!$this->getDbAdapter() instanceof \Magento\Framework\DB\Adapter\Pdo\Mysql) {
4438
$this->markTestSkipped('This test is for \Magento\Framework\DB\Adapter\Pdo\Mysql');
4539
}
4640
try {
47-
$defaultWaitTimeout = $this->_getWaitTimeout();
4841
$minWaitTimeout = 1;
49-
$this->_setWaitTimeout($minWaitTimeout);
50-
$this->assertEquals($minWaitTimeout, $this->_getWaitTimeout(), 'Wait timeout was not changed');
42+
$this->setWaitTimeout($minWaitTimeout);
43+
$this->assertEquals($minWaitTimeout, $this->getWaitTimeout(), 'Wait timeout was not changed');
5144

5245
// Sleep for time greater than wait_timeout and try to perform query
5346
sleep($minWaitTimeout + 1);
54-
$result = $this->_executeQuery('SELECT 1');
55-
$this->assertInstanceOf('Magento\Framework\DB\Statement\Pdo\Mysql', $result);
56-
// Restore wait_timeout
57-
$this->_setWaitTimeout($defaultWaitTimeout);
58-
$this->assertEquals(
59-
$defaultWaitTimeout,
60-
$this->_getWaitTimeout(),
61-
'Default wait timeout was not restored'
62-
);
63-
} catch (\Exception $e) {
64-
// Reset connection on failure to restore global variables
65-
$this->_getConnection()->closeConnection();
66-
throw $e;
47+
$result = $this->executeQuery('SELECT 1');
48+
$this->assertInstanceOf(\Magento\Framework\DB\Statement\Pdo\Mysql::class, $result);
49+
} finally {
50+
$this->getDbAdapter()->closeConnection();
6751
}
6852
}
6953

@@ -72,9 +56,9 @@ public function testWaitTimeout()
7256
*
7357
* @return int
7458
*/
75-
protected function _getWaitTimeout()
59+
private function getWaitTimeout()
7660
{
77-
$result = $this->_executeQuery('SELECT @@session.wait_timeout');
61+
$result = $this->executeQuery('SELECT @@session.wait_timeout');
7862
return (int)$result->fetchColumn();
7963
}
8064

@@ -83,54 +67,67 @@ protected function _getWaitTimeout()
8367
*
8468
* @param int $waitTimeout
8569
*/
86-
protected function _setWaitTimeout($waitTimeout)
70+
private function setWaitTimeout($waitTimeout)
8771
{
88-
$this->_executeQuery("SET @@session.wait_timeout = {$waitTimeout}");
72+
$this->executeQuery("SET @@session.wait_timeout = {$waitTimeout}");
8973
}
9074

9175
/**
9276
* Execute SQL query and return result statement instance
9377
*
94-
* @param string $sql
95-
* @return \Zend_Db_Statement_Interface
96-
* @throws \Exception
78+
* @param $sql
79+
* @return void|\Zend_Db_Statement_Pdo
80+
* @throws \Magento\Framework\Exception\LocalizedException
81+
* @throws \Zend_Db_Adapter_Exception
9782
*/
98-
protected function _executeQuery($sql)
83+
private function executeQuery($sql)
9984
{
100-
/**
101-
* Suppress PDO warnings to work around the bug
102-
* @link https://bugs.php.net/bug.php?id=63812
103-
*/
104-
$phpErrorReporting = error_reporting();
105-
/** @var $pdoConnection \PDO */
106-
$pdoConnection = $this->_getConnection()->getConnection();
107-
$pdoWarningsEnabled = $pdoConnection->getAttribute(\PDO::ATTR_ERRMODE) & \PDO::ERRMODE_WARNING;
108-
if (!$pdoWarningsEnabled) {
109-
error_reporting($phpErrorReporting & ~E_WARNING);
110-
}
111-
try {
112-
$result = $this->_getConnection()->query($sql);
113-
error_reporting($phpErrorReporting);
114-
} catch (\Exception $e) {
115-
error_reporting($phpErrorReporting);
116-
throw $e;
117-
}
118-
return $result;
85+
return $this->getDbAdapter()->query($sql);
11986
}
12087

12188
/**
12289
* Retrieve database adapter instance
12390
*
12491
* @return \Magento\Framework\DB\Adapter\Pdo\Mysql
12592
*/
126-
protected function _getConnection()
93+
private function getDbAdapter()
12794
{
128-
if (is_null($this->_connection)) {
129-
/** @var $coreResource \Magento\Framework\App\ResourceConnection */
130-
$coreResource = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
131-
->get('Magento\Framework\App\ResourceConnection');
132-
$this->_connection = $coreResource->getConnection();
133-
}
134-
return $this->_connection;
95+
return $this->resourceConnection->getConnection();
96+
}
97+
98+
public function testGetCreateTable()
99+
{
100+
$tableName = $this->resourceConnection->getTableName('core_config_data');
101+
$this->assertEquals(
102+
$this->getDbAdapter()->getCreateTable($tableName),
103+
$this->getDbAdapter()->getCreateTable($tableName)
104+
);
105+
}
106+
107+
public function testGetForeignKeys()
108+
{
109+
$tableName = $this->resourceConnection->getTableName('core_config_data');
110+
$this->assertEquals(
111+
$this->getDbAdapter()->getForeignKeys($tableName),
112+
$this->getDbAdapter()->getForeignKeys($tableName)
113+
);
114+
}
115+
116+
public function testGetIndexList()
117+
{
118+
$tableName = $this->resourceConnection->getTableName('core_config_data');
119+
$this->assertEquals(
120+
$this->getDbAdapter()->getIndexList($tableName),
121+
$this->getDbAdapter()->getIndexList($tableName)
122+
);
123+
}
124+
125+
public function testDescribeTable()
126+
{
127+
$tableName = $this->resourceConnection->getTableName('core_config_data');
128+
$this->assertEquals(
129+
$this->getDbAdapter()->describeTable($tableName),
130+
$this->getDbAdapter()->describeTable($tableName)
131+
);
135132
}
136133
}

lib/internal/Magento/Framework/DB/Statement/Pdo/Mysql.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,20 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
namespace Magento\Framework\DB\Statement\Pdo;
67

7-
// @codingStandardsIgnoreFile
8+
use Magento\Framework\DB\Statement\Parameter;
89

910
/**
1011
* Mysql DB Statement
1112
*
1213
* @author Magento Core Team <core@magentocommerce.com>
1314
*/
14-
namespace Magento\Framework\DB\Statement\Pdo;
15-
16-
use Magento\Framework\DB\Statement\Parameter;
17-
1815
class Mysql extends \Zend_Db_Statement_Pdo
1916
{
17+
2018
/**
21-
* Executes statement with binding values to it.
22-
* Allows transferring specific options to DB driver.
19+
* Executes statement with binding values to it. Allows transferring specific options to DB driver.
2320
*
2421
* @param array $params Array of values to bind to parameter placeholders.
2522
* @return bool
@@ -63,11 +60,9 @@ public function _executeWithBinding(array $params)
6360
$statement->bindParam($paramName, $bindValues[$name], $dataType, $length, $driverOptions);
6461
}
6562

66-
try {
63+
return $this->tryExecute(function () use ($statement) {
6764
return $statement->execute();
68-
} catch (\PDOException $e) {
69-
throw new \Zend_Db_Statement_Exception($e->getMessage(), (int)$e->getCode(), $e);
70-
}
65+
});
7166
}
7267

7368
/**
@@ -92,7 +87,29 @@ public function _execute(array $params = null)
9287
if ($specialExecute) {
9388
return $this->_executeWithBinding($params);
9489
} else {
95-
return parent::_execute($params);
90+
return $this->tryExecute(function () use ($params) {
91+
return $params !== null ? $this->_stmt->execute($params) : $this->_stmt->execute();
92+
});
93+
}
94+
}
95+
96+
/**
97+
* Executes query and avoid warnings.
98+
*
99+
* @param callable $callback
100+
* @return bool
101+
* @throws \Zend_Db_Statement_Exception
102+
*/
103+
private function tryExecute($callback)
104+
{
105+
$previousLevel = error_reporting(\E_ERROR); // disable warnings for PDO bugs #63812, #74401
106+
try {
107+
return $callback();
108+
} catch (\PDOException $e) {
109+
$message = sprintf('%s, query was: %s', $e->getMessage(), $this->_stmt->queryString);
110+
throw new \Zend_Db_Statement_Exception($message, (int)$e->getCode(), $e);
111+
} finally {
112+
error_reporting($previousLevel);
96113
}
97114
}
98115
}

0 commit comments

Comments
 (0)