1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) 2013 Janos Szurovecz |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
7
|
|
|
* the Software without restriction, including without limitation the rights to |
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
10
|
|
|
* so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
13
|
|
|
* copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21
|
|
|
* SOFTWARE. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace predaddy\messagehandling\interceptors; |
25
|
|
|
|
26
|
|
|
use Exception; |
27
|
|
|
use lf4php\MDC; |
28
|
|
|
use precore\lang\Object; |
29
|
|
|
use precore\util\UUID; |
30
|
|
|
use predaddy\messagehandling\DispatchInterceptor; |
31
|
|
|
use predaddy\messagehandling\InterceptorChain; |
32
|
|
|
use predaddy\messagehandling\SubscriberExceptionContext; |
33
|
|
|
use predaddy\messagehandling\SubscriberExceptionHandler; |
34
|
|
|
use trf4php\TransactionManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Wraps the command dispatch process in transaction. It handles nested levels, which means it does not |
38
|
|
|
* start a new transaction if there is an open one. |
39
|
|
|
* |
40
|
|
|
* <p> It puts a transaction ID to MDC with 'TR' key. |
41
|
|
|
* |
42
|
|
|
* @author Janos Szurovecz <[email protected]> |
43
|
|
|
*/ |
44
|
|
|
final class WrapInTransactionInterceptor extends Object implements DispatchInterceptor, SubscriberExceptionHandler |
45
|
|
|
{ |
46
|
|
|
const MDC_KEY = 'TR'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var TransactionManager |
50
|
|
|
*/ |
51
|
|
|
private $transactionManager; |
52
|
|
|
|
53
|
|
|
private $transactionLevel = 0; |
54
|
|
|
|
55
|
|
|
private $rollbackRequired = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param TransactionManager $transactionManager |
59
|
|
|
*/ |
60
|
|
|
public function __construct(TransactionManager $transactionManager) |
61
|
|
|
{ |
62
|
|
|
$this->transactionManager = $transactionManager; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function invoke($message, InterceptorChain $chain) |
66
|
|
|
{ |
67
|
|
|
try { |
68
|
|
|
$this->transactionLevel++; |
69
|
|
|
if ($this->transactionLevel == 1) { |
70
|
|
|
$this->transactionManager->beginTransaction(); |
71
|
|
|
MDC::put(self::MDC_KEY, UUID::randomUUID()->toString()); |
72
|
|
|
self::getLogger()->debug('Transaction has been started'); |
73
|
|
|
$chain->proceed(); |
74
|
|
|
if ($this->rollbackRequired) { |
75
|
|
|
$this->transactionManager->rollback(); |
76
|
|
|
$this->rollbackRequired = false; |
77
|
|
|
self::getLogger()->debug('Transaction has been rolled back'); |
78
|
|
|
} else { |
79
|
|
|
$this->transactionManager->commit(); |
80
|
|
|
self::getLogger()->debug('Transaction has been committed'); |
81
|
|
|
} |
82
|
|
|
MDC::remove(self::MDC_KEY); |
83
|
|
|
} else { |
84
|
|
|
self::getLogger()->debug('Transaction has already started, using the existing one'); |
85
|
|
|
$chain->proceed(); |
86
|
|
|
} |
87
|
|
|
$this->transactionLevel--; |
88
|
|
|
} catch (Exception $e) { |
89
|
|
|
MDC::remove(self::MDC_KEY); |
90
|
|
|
$this->transactionLevel--; |
91
|
|
|
throw $e; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function handleException(Exception $exception, SubscriberExceptionContext $context) |
96
|
|
|
{ |
97
|
|
|
$this->rollbackRequired = true; |
98
|
|
|
self::getLogger()->debug("Transaction rollback required, context '{}'!", [$context], $exception); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return int |
103
|
|
|
*/ |
104
|
|
|
public function getTransactionLevel() |
105
|
|
|
{ |
106
|
|
|
return $this->transactionLevel; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|