neo4j-php-client icon indicating copy to clipboard operation
neo4j-php-client copied to clipboard

Connection failure when one of multiple hosts down

Open zorac opened this issue 3 months ago • 0 comments

Describe the bug I'm connecting to a Neo4j cluster with three nodes by creating a client with three drivers, one for each node (same alias, different priorities.) If the highest priority node is down, but the other two are up, the connection fails with the following exception:

RuntimeException: Cannot connect to host: "<hostname>". Hosts tried: "<IP>", "<hostname>", "<IP>"
  in Laudis\Neo4j\Neo4j\Neo4jConnectionPool::acquire (Neo4jConnectionPool.php:168)
  in Laudis\Neo4j\Neo4j\Neo4jDriver::verifyConnectivity (Neo4jDriver.php:92)
  in Laudis\Neo4j\Common\DriverSetupManager::getDriver (DriverSetupManager.php:129)
  in Laudis\Neo4j\Client::getDriver (Client.php:123)
  in Laudis\Neo4j\Client::startSession (Client.php:128)
  in Laudis\Neo4j\Client::getSession (Client.php:100)
  in Laudis\Neo4j\Client::writeTransaction (Client.php:134)

To Reproduce

  1. Create a client using the below code; $config is a simple PHP object, the value of $config->hostname is equivalent to neoj1.example.org,neoj2.example.org,neoj3.example.org.
  2. Attempt to run a statement/transaction while neoj1.example.org is down, but the other two nodes are up.
  3. Exception thrown as above.
            $auth = Authenticate::basic(
                $this->config->username,
                $this->config->password,
            );

            $builder = ClientBuilder::create()
                ->withDefaultSessionConfiguration(
                    SessionConfiguration::default()
                        ->withDatabase($this->config->database)
                );

            $hosts = explode(',', $this->config->hostname);
            $priority = count($hosts);
            $proto = ($priority > 1) ? 'neo4j' : 'bolt';

            foreach ($hosts as $host) {
                $builder = $builder->withDriver(
                    'default',
                    "$proto://$host",
                    $auth,
                    $priority--,
                );
            }

            $this->client = $builder->build();

Expected behavior If the highest-priority node is down, connections should be attempted to lower-priority nodes.

Desktop (please complete the following information):

  • Library version: 3.4.0
  • Neo4j Version: 5.25.1 Enterprise
  • PHP version: 8.4.12
  • OS: Alpine LInux 3.21.4

zorac avatar Dec 09 '25 12:12 zorac