2121use Laudis \Neo4j \Databags \SummarizedResult ;
2222use Laudis \Neo4j \Databags \TransactionConfiguration ;
2323use Laudis \Neo4j \Enum \TransactionState ;
24- use Laudis \Neo4j \Exception \ClientException ;
24+ use Laudis \Neo4j \Exception \TransactionException ;
2525use Laudis \Neo4j \Formatter \SummarizedResultFormatter ;
2626use Laudis \Neo4j \ParameterHelper ;
2727use Laudis \Neo4j \Types \CypherList ;
@@ -58,23 +58,23 @@ public function __construct(
5858 /**
5959 * @param iterable<Statement> $statements
6060 *
61- * @throws ClientException |Throwable
61+ * @throws TransactionException |Throwable
6262 *
6363 * @return CypherList<SummarizedResult>
6464 */
6565 public function commit (iterable $ statements = []): CypherList
6666 {
6767 if ($ this ->isFinished ()) {
6868 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 " );
7070 }
7171
7272 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 " );
7474 }
7575
7676 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 " );
7878 }
7979 }
8080
@@ -93,16 +93,20 @@ public function commit(iterable $statements = []): CypherList
9393 public function rollback (): void
9494 {
9595 if ($ this ->isFinished ()) {
96- if ($ this ->state === TransactionState::TERMINATED ) {
97- throw new ClientException ("Can't rollback, transaction has been terminated " );
98- }
99-
10096 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 " );
10298 }
10399
104100 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 ;
106110 }
107111 }
108112
@@ -115,6 +119,20 @@ public function rollback(): void
115119 */
116120 public function run (string $ statement , iterable $ parameters = []): SummarizedResult
117121 {
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+
118136 return $ this ->runStatement (new Statement ($ statement , $ parameters ));
119137 }
120138
0 commit comments