Skip to content

Commit c9fc067

Browse files
committed
added rollback and commit flags
1 parent 865e2f8 commit c9fc067

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

src/Bolt/BoltUnmanagedTransaction.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
use Bolt\Bolt;
1717
use Bolt\error\ConnectionTimeoutException;
18-
use Bolt\error\IgnoredException;
1918
use Bolt\error\MessageException;
2019
use Bolt\protocol\V3;
20+
use Laudis\Neo4j\Common\TransactionHelper;
2121
use Laudis\Neo4j\Contracts\ConnectionInterface;
2222
use Laudis\Neo4j\Contracts\FormatterInterface;
2323
use Laudis\Neo4j\Contracts\UnmanagedTransactionInterface;
@@ -54,6 +54,9 @@ final class BoltUnmanagedTransaction implements UnmanagedTransactionInterface
5454
/** @psalm-readonly */
5555
private string $database;
5656

57+
private bool $isRolledBack = false;
58+
private bool $isCommitted = false;
59+
5760
/**
5861
* @param FormatterInterface<T> $formatter
5962
* @param ConnectionInterface<V3> $connection
@@ -73,11 +76,11 @@ public function commit(iterable $statements = []): CypherList
7376

7477
try {
7578
$this->getBolt()->commit();
79+
$this->isCommitted = true;
7680
} catch (MessageException $e) {
77-
throw Neo4jException::fromMessageException($e);
81+
$this->handleMessageException($e);
7882
} catch (ConnectionTimeoutException $e) {
79-
$this->connection->reset();
80-
throw $e;
83+
$this->handleConnectionTimeoutException($e);
8184
}
8285

8386
return $tbr;
@@ -87,11 +90,11 @@ public function rollback(): void
8790
{
8891
try {
8992
$this->connection->getImplementation()->rollback();
93+
$this->isRolledBack = true;
9094
} catch (MessageException $e) {
91-
throw Neo4jException::fromMessageException($e);
95+
$this->handleMessageException($e);
9296
} catch (ConnectionTimeoutException $e) {
93-
$this->connection->reset();
94-
throw $e;
97+
$this->handleConnectionTimeoutException($e);
9598
}
9699
}
97100

@@ -130,10 +133,9 @@ public function runStatements(iterable $statements): CypherList
130133
/** @var array<array> $results */
131134
$results = $this->getBolt()->pullAll();
132135
} catch (MessageException $e) {
133-
throw Neo4jException::fromMessageException($e);
136+
$this->handleMessageException($e);
134137
} catch (ConnectionTimeoutException $e) {
135-
$this->connection->reset();
136-
throw $e;
138+
$this->handleConnectionTimeoutException($e);
137139
}
138140

139141
$end = microtime(true);
@@ -162,4 +164,29 @@ public function __destruct()
162164
{
163165
$this->connection->close();
164166
}
167+
168+
/**
169+
* @throws Neo4jException
170+
*
171+
* @return never
172+
*/
173+
private function handleMessageException(MessageException $e): void
174+
{
175+
$exception = Neo4jException::fromMessageException($e);
176+
if (in_array($exception->getClassification(), TransactionHelper::ROLLBACK_CLASSIFICATIONS)) {
177+
$this->isRolledBack = true;
178+
}
179+
throw $exception;
180+
}
181+
182+
/**
183+
* @return never
184+
* @throws ConnectionTimeoutException
185+
*/
186+
private function handleConnectionTimeoutException(ConnectionTimeoutException $e): void
187+
{
188+
$this->connection->reset();
189+
190+
throw $e;
191+
}
165192
}

src/Common/TransactionHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
final class TransactionHelper
2121
{
22-
private const ROLLBACK_CLASSIFICATIONS = ['ClientError', 'TransientError', 'DatabaseError'];
22+
public const ROLLBACK_CLASSIFICATIONS = ['ClientError', 'TransientError', 'DatabaseError'];
2323

2424
/**
2525
* @template U

0 commit comments

Comments
 (0)