Skip to content

Commit 0bf1199

Browse files
committed
exposed version to neo4j connection pool and routed the correct calls
1 parent 69f3eb4 commit 0bf1199

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/Neo4j/Neo4jConnectionPool.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Laudis\Neo4j\Enum\RoutingRoles;
2424
use Psr\Http\Message\UriInterface;
2525
use function random_int;
26+
use function str_starts_with;
2627
use function time;
2728

2829
/**
@@ -35,13 +36,15 @@ final class Neo4jConnectionPool implements ConnectionPoolInterface
3536
private ?RoutingTable $table = null;
3637
/** @var ConnectionPoolInterface<StreamSocket> */
3738
private ConnectionPoolInterface $pool;
39+
private string $version;
3840

3941
/**
4042
* @param ConnectionPoolInterface<StreamSocket> $pool
4143
*/
42-
public function __construct(ConnectionPoolInterface $pool)
44+
public function __construct(ConnectionPoolInterface $pool, string $version)
4345
{
4446
$this->pool = $pool;
47+
$this->version = $version;
4548
}
4649

4750
/**
@@ -78,7 +81,12 @@ private function getNextServer(RoutingTable $table, AccessMode $mode): string
7881
private function routingTable(DriverInterface $driver): RoutingTable
7982
{
8083
if ($this->table === null || $this->table->getTtl() < time()) {
81-
$response = $driver->createSession()->run('CALL dbms.routing.getRoutingTable({context: []})')->first();
84+
$session = $driver->createSession();
85+
if (str_starts_with($this->version, '3')) {
86+
$response = $session->run('CALL dbms.cluster.overview()')->first();
87+
} else {
88+
$response = $session->run('CALL dbms.routing.getRoutingTable({context: []})')->first();
89+
}
8290
/** @var iterable<array{addresses: list<string>, role:string}> $values */
8391
$values = $response->get('servers');
8492
/** @var int $ttl */

src/Neo4j/Neo4jDriver.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
use Bolt\connection\StreamSocket;
1717
use Exception;
18+
use Laudis\Neo4j\Bolt\BoltDriver;
19+
use RuntimeException;
1820
use function is_string;
1921
use Laudis\Neo4j\Authentication\Authenticate;
2022
use Laudis\Neo4j\Bolt\BoltConnectionPool;
@@ -73,17 +75,25 @@ public static function create($uri, ?DriverConfiguration $configuration = null,
7375

7476
/**
7577
* @param string|UriInterface $uri
78+
* @throws Exception
7679
*/
7780
public static function createWithFormatter($uri, FormatterInterface $formatter, ?DriverConfiguration $configuration = null, ?AuthenticateInterface $authenticate = null): self
7881
{
7982
if (is_string($uri)) {
8083
$uri = Uri::create($uri);
8184
}
8285

86+
$session = BoltDriver::create($uri)->createSession();
87+
$row = $session->run(
88+
'CALL dbms.components() yield versions UNWIND versions as version RETURN version'
89+
)->first();
90+
$version = $row->get('version');
91+
92+
/** @psalm-suppress all */
8393
return new self(
8494
$uri,
8595
$authenticate ?? Authenticate::fromUrl(),
86-
new Neo4jConnectionPool(new BoltConnectionPool()),
96+
new Neo4jConnectionPool(new BoltConnectionPool(), $version),
8797
$configuration ?? DriverConfiguration::default(),
8898
$formatter
8999
);

tests/Integration/HttpConsistencyTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ protected function setUp(): void
2828
$this->client = ClientBuilder::create()
2929
->withDriver('http', 'http://neo4j:test@neo4j')
3030
->withDriver('bolt', 'bolt://neo4j:test@neo4j')
31-
->withDriver('neo4j', 'neo4j://neo4j:test@neo4j')
31+
->withDriver('neo4j', 'neo4j://neo4j:test@core1')
3232
->build();
3333

34-
$this->client->run('MATCH (x) DETACH DELETE x');
34+
$this->client->run('MATCH (x) DETACH DELETE x', [], 'http');
35+
$this->client->run('MATCH (x) DETACH DELETE x', [], 'bolt');
36+
$this->client->run('MATCH (x) DETACH DELETE x', [], 'neo4j');
3537
}
3638

3739
/**

0 commit comments

Comments
 (0)