21
21
use Laudis \Neo4j \Databags \SummarizedResult ;
22
22
use Laudis \Neo4j \Databags \TransactionConfiguration ;
23
23
use Laudis \Neo4j \Enum \TransactionState ;
24
- use Laudis \Neo4j \Exception \ClientException ;
24
+ use Laudis \Neo4j \Exception \TransactionException ;
25
25
use Laudis \Neo4j \Formatter \SummarizedResultFormatter ;
26
26
use Laudis \Neo4j \ParameterHelper ;
27
27
use Laudis \Neo4j \Types \CypherList ;
@@ -58,23 +58,23 @@ public function __construct(
58
58
/**
59
59
* @param iterable<Statement> $statements
60
60
*
61
- * @throws ClientException |Throwable
61
+ * @throws TransactionException |Throwable
62
62
*
63
63
* @return CypherList<SummarizedResult>
64
64
*/
65
65
public function commit (iterable $ statements = []): CypherList
66
66
{
67
67
if ($ this ->isFinished ()) {
68
68
if ($ this ->state === TransactionState::TERMINATED ) {
69
- throw new ClientException ("Can't commit, transaction has been terminated " );
69
+ throw new TransactionException ("Can't commit, transaction has been terminated " );
70
70
}
71
71
72
72
if ($ this ->state === TransactionState::COMMITTED ) {
73
- throw new ClientException ("Can't commit, transaction has already been committed " );
73
+ throw new TransactionException ("Can't commit, transaction has already been committed " );
74
74
}
75
75
76
76
if ($ this ->state === TransactionState::ROLLED_BACK ) {
77
- throw new ClientException ("Can't commit, transaction has already been rolled back " );
77
+ throw new TransactionException ("Can't commit, transaction has already been rolled back " );
78
78
}
79
79
}
80
80
@@ -93,16 +93,20 @@ public function commit(iterable $statements = []): CypherList
93
93
public function rollback (): void
94
94
{
95
95
if ($ this ->isFinished ()) {
96
- if ($ this ->state === TransactionState::TERMINATED ) {
97
- throw new ClientException ("Can't rollback, transaction has been terminated " );
98
- }
99
-
100
96
if ($ this ->state === TransactionState::COMMITTED ) {
101
- throw new ClientException ("Can't rollback, transaction has already been committed " );
97
+ throw new TransactionException ("Can't rollback, transaction has already been committed " );
102
98
}
103
99
104
100
if ($ this ->state === TransactionState::ROLLED_BACK ) {
105
- throw new ClientException ("Can't rollback, transaction has already been rolled back " );
101
+ // Already rolled back, throw a TransactionException to be wrapped by DriverErrorResponse
102
+ throw new TransactionException ('Transaction has already been rolled back ' );
103
+ }
104
+
105
+ if ($ this ->state === TransactionState::TERMINATED ) {
106
+ // Transaction failed, allow rollback as a no-op
107
+ $ this ->state = TransactionState::ROLLED_BACK ;
108
+
109
+ return ;
106
110
}
107
111
}
108
112
@@ -115,6 +119,20 @@ public function rollback(): void
115
119
*/
116
120
public function run (string $ statement , iterable $ parameters = []): SummarizedResult
117
121
{
122
+ if ($ this ->isFinished ()) {
123
+ if ($ this ->state === TransactionState::TERMINATED ) {
124
+ throw new TransactionException ("Can't rollback, transaction has been terminated " );
125
+ }
126
+
127
+ if ($ this ->state === TransactionState::COMMITTED ) {
128
+ throw new TransactionException ("Can't rollback, transaction has already been committed " );
129
+ }
130
+
131
+ if ($ this ->state === TransactionState::ROLLED_BACK ) {
132
+ throw new TransactionException ("Can't rollback, transaction has already been rolled back " );
133
+ }
134
+ }
135
+
118
136
return $ this ->runStatement (new Statement ($ statement , $ parameters ));
119
137
}
120
138
0 commit comments