Skip to content

Commit a519327

Browse files
omiroshnichenkolsrocha
authored andcommitted
MAGETWO-75452: [Backport] - Fix Issue: “Asymmetric transaction rollback error” if commit callbacks throw exceptions magento#9955 - for 2.2
1 parent 2298b93 commit a519327

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

lib/internal/Magento/Framework/Model/ResourceModel/AbstractResource.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@
1414
abstract class AbstractResource
1515
{
1616
/**
17-
* Main constructor
17+
* @var Json
18+
* @since 100.2.0
19+
*/
20+
protected $serializer;
21+
22+
/**
23+
* @var \Psr\Log\LoggerInterface
24+
*/
25+
protected $_logger;
26+
27+
/**
28+
* Constructor
1829
*/
1930
public function __construct()
2031
{
@@ -82,7 +93,7 @@ public function commit()
8293
call_user_func($callback);
8394
}
8495
} catch (\Exception $e) {
85-
throw $e;
96+
$this->getLogger()->critical($e);
8697
}
8798
}
8899
return $this;
@@ -228,4 +239,34 @@ protected function _getColumnsForEntityLoad(\Magento\Framework\Model\AbstractMod
228239
}
229240
return $columns;
230241
}
242+
243+
/**
244+
* Get serializer
245+
*
246+
* @return Json
247+
* @deprecated 100.2.0
248+
* @since 100.2.0
249+
*/
250+
protected function getSerializer()
251+
{
252+
if (null === $this->serializer) {
253+
$this->serializer = ObjectManager::getInstance()->get(Json::class);
254+
}
255+
return $this->serializer;
256+
}
257+
258+
/**
259+
* Get logger
260+
*
261+
* @return \Psr\Log\LoggerInterface
262+
* @deprecated 100.2.0
263+
* @since 100.2.0
264+
*/
265+
private function getLogger()
266+
{
267+
if (null === $this->_logger) {
268+
$this->_logger = ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
269+
}
270+
return $this->_logger;
271+
}
231272
}

lib/internal/Magento/Framework/Model/Test/Unit/ResourceModel/AbstractResourceTest.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@
1010

1111
class AbstractResourceTest extends \PHPUnit_Framework_TestCase
1212
{
13+
/**
14+
* @var AbstractResourceStub
15+
*/
16+
private $abstractResource;
17+
18+
/**
19+
* @var Json|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
private $serializerMock;
22+
23+
/**
24+
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $loggerMock;
27+
28+
protected function setUp()
29+
{
30+
$objectManager = new ObjectManager($this);
31+
$this->serializerMock = $this->createMock(Json::class);
32+
$this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
33+
$this->abstractResource = $objectManager->getObject(AbstractResourceStub::class);
34+
$objectManager->setBackwardCompatibleProperty($this->abstractResource,'serializer',$this->serializerMock);
35+
$objectManager->setBackwardCompatibleProperty($this->abstractResource,'_logger',$this->loggerMock);
36+
}
37+
1338
/**
1439
* @param array $arguments
1540
* @param string $expectation
@@ -144,9 +169,6 @@ function () use ($closureExpectation) {
144169
$abstractResource->commit();
145170
}
146171

147-
/**
148-
* @expectedException \Exception
149-
*/
150172
public function testCommitZeroLevelCallbackException()
151173
{
152174
/** @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject $connection */
@@ -165,6 +187,8 @@ function () {
165187
$connection->expects(static::once())
166188
->method('getTransactionLevel')
167189
->willReturn(0);
190+
$this->loggerMock->expects($this->once())
191+
->method('critical');
168192

169193
$abstractResource->commit();
170194
}

0 commit comments

Comments
 (0)