Skip to content

Commit 3b16990

Browse files
committed
removed recreation of bolt connection
1 parent bca96ed commit 3b16990

File tree

6 files changed

+17
-36
lines changed

6 files changed

+17
-36
lines changed

src/Bolt/BoltConnection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Bolt\error\MessageException;
1818
use Bolt\protocol\V3;
1919
use Bolt\protocol\V4;
20+
use function in_array;
2021
use Laudis\Neo4j\Common\ConnectionConfiguration;
2122
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2223
use Laudis\Neo4j\Contracts\ConnectionInterface;
@@ -244,7 +245,7 @@ public function discard(?int $qid): void
244245
*/
245246
public function run(string $text, array $parameters, ?string $database, ?float $timeout, BookmarkHolder $holder): array
246247
{
247-
if (!str_starts_with($this->serverState, 'TX_') || str_starts_with($this->getServerVersion(), '3')) {
248+
if (in_array($this->serverState, ['STREAMING', 'TX_STREAMING'])) {
248249
$this->consumeResults();
249250
}
250251

src/Bolt/BoltResult.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
namespace Laudis\Neo4j\Bolt;
1515

16-
use Laudis\Neo4j\Contracts\FormatterInterface;
1716
use function array_splice;
1817
use function count;
1918
use Generator;
2019
use Iterator;
20+
use Laudis\Neo4j\Contracts\FormatterInterface;
2121

2222
/**
2323
* @psalm-import-type BoltCypherStats from FormatterInterface

src/Bolt/ConnectionPool.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ public function release(ConnectionInterface $connection): void
109109
}
110110
}
111111

112-
public function __destruct()
113-
{
114-
echo 'Destructing connection pool';
115-
}
116-
117112
/**
118113
* @return BoltConnection|null
119114
*/
@@ -122,12 +117,12 @@ private function returnAnyAvailableConnection(SessionConfiguration $config): ?Co
122117
$streamingConnection = null;
123118
$requiresReconnectConnection = null;
124119
// Ensure random connection reuse before picking one.
125-
// shuffle($this->activeConnections);
120+
shuffle($this->activeConnections);
126121

127122
foreach ($this->activeConnections as $activeConnection) {
128123
// We prefer a connection that is just ready
129124
if ($activeConnection->getServerState() === 'READY') {
130-
if ($this->factory->canReuseConnection($activeConnection, $this->data)) {
125+
if ($this->factory->canReuseConnection($activeConnection, $this->data, $config)) {
131126
return $this->factory->reuseConnection($activeConnection, $config);
132127
} else {
133128
$requiresReconnectConnection = $activeConnection;
@@ -142,7 +137,7 @@ private function returnAnyAvailableConnection(SessionConfiguration $config): ?Co
142137
// https://github.com/neo4j-php/neo4j-php-client/issues/146
143138
// NOTE: we cannot work with TX_STREAMING as we cannot force the transaction to implicitly close.
144139
if ($streamingConnection === null && $activeConnection->getServerState() === 'STREAMING') {
145-
if ($this->factory->canReuseConnection($activeConnection, $this->data)) {
140+
if ($this->factory->canReuseConnection($activeConnection, $this->data, $config)) {
146141
$streamingConnection = $activeConnection;
147142
if (method_exists($streamingConnection, 'consumeResults')) {
148143
$streamingConnection->consumeResults(); // State should now be ready

src/Bolt/Session.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,4 @@ public function getLastBookmark(): Bookmark
194194
{
195195
return $this->bookmarkHolder->getBookmark();
196196
}
197-
198-
public function __destruct()
199-
{
200-
echo 'destroyed session';
201-
}
202197
}

src/BoltFactory.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,35 +81,23 @@ public function createConnection(ConnectionRequestData $data, SessionConfigurati
8181
return new BoltConnection($protocol, $connection, $data->getAuth(), $data->getUserAgent(), $config);
8282
}
8383

84-
public function canReuseConnection(ConnectionInterface $connection, ConnectionRequestData $data): bool
84+
public function canReuseConnection(ConnectionInterface $connection, ConnectionRequestData $data, SessionConfiguration $config): bool
8585
{
86+
$databaseInfo = $connection->getDatabaseInfo();
87+
$database = $databaseInfo === null ? null : $databaseInfo->getName();
88+
8689
return $connection->getServerAddress()->getHost() === $data->getUri()->getHost() &&
8790
$connection->getServerAddress()->getPort() === $data->getUri()->getPort() &&
8891
$connection->getAuthentication()->toString($data->getUri()) === $data->getAuth()->toString($data->getUri()) &&
8992
$connection->getEncryptionLevel() === $this->sslConfigurationFactory->create($data->getUri(), $data->getSslConfig())[0] &&
90-
$connection->getUserAgent() === $data->getUserAgent();
93+
$connection->getUserAgent() === $data->getUserAgent() &&
94+
$connection->getAccessMode() === $config->getAccessMode() &&
95+
$database === $config->getDatabase();
9196
}
9297

9398
public function reuseConnection(BoltConnection $connection, SessionConfiguration $sessionConfig): BoltConnection
9499
{
95-
[$protocol, $connectionImpl] = $connection->getImplementation();
96-
97-
$config = new ConnectionConfiguration(
98-
$connection->getServerAgent(),
99-
$connection->getServerAddress(),
100-
$connection->getServerVersion(),
101-
$connection->getProtocol(),
102-
$sessionConfig->getAccessMode(),
103-
$sessionConfig->getDatabase() === null ? null : new DatabaseInfo($sessionConfig->getDatabase()),
104-
$connection->getEncryptionLevel()
105-
);
106-
107-
return new BoltConnection(
108-
$protocol,
109-
$connectionImpl,
110-
$connection->getAuthentication(),
111-
$connection->getUserAgent(),
112-
$config
113-
);
100+
// TODO make sure session config gets merged
101+
return $connection;
114102
}
115103
}

tests/Integration/BasicDriverTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public function testFullWalk(string $connection): void
5757
{
5858
$driver = Driver::create($connection);
5959

60+
global $session;
61+
6062
$session = $driver->createSession();
6163

6264
$session->run('MATCH (x) DETACH DELETE x');

0 commit comments

Comments
 (0)