You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何避免neo4j-javascript-driver Node.js模块因超时退出?

Fixing the "Neo4jError: Operation timed out in 60000 ms" in neo4j-javascript-driver

Got it, let's tackle that annoying timeout issue head-on. That 60-second limit can kill long-running queries or transactions, but we can tweak the driver settings to disable or extend it. Here's how to do it properly:

1. Adjust Driver-Level Connection Timeouts

First, when initializing your Neo4j driver, you can override the default connection timeout. The exact parameter name depends on which driver version you're using:

For Neo4j Driver v5+

Use connectionTimeout to disable or extend the timeout (set to 0 for no limit, or a higher millisecond value if you want a safety net):

import neo4j from 'neo4j-driver';

const driver = neo4j.driver(
  'bolt://localhost:7687',
  neo4j.auth.basic('your-username', 'your-password'),
  {
    connectionTimeout: 0, // Disable connection timeout entirely
    maxTransactionRetryTime: 0, // Optional: Disable retry timeout for transactions
  }
);

For Neo4j Driver v4.x

The parameter name here is connectionAcquisitionTimeout—same logic applies:

const driver = neo4j.driver(
  'bolt://localhost:7687',
  neo4j.auth.basic('your-username', 'your-password'),
  {
    connectionAcquisitionTimeout: 0, // Disable connection acquisition timeout
    maxTransactionRetryTime: 0, // Optional: Turn off transaction retry timeouts
  }
);

2. Disable Per-Query/Transaction Timeouts

Even with driver-level settings, individual queries or transactions might still hit timeouts. Add a timeout parameter set to 0 when running your query or transaction to bypass this:

Example: Direct Long Query

async function runExtendedQuery() {
  const session = driver.session();
  try {
    const result = await session.run(
      // Your long-running Cypher query here
      'MATCH (n:LargeDataset) RETURN collect(n)',
      {},
      { timeout: 0 } // Disable query-specific timeout
    );
    console.log('Query completed:', result.records.length);
  } finally {
    await session.close();
  }
}

Example: Long Transaction

async function runExtendedTransaction() {
  const session = driver.session();
  try {
    const result = await session.executeWrite(async (tx) => {
      return tx.run(
        'MATCH (n:LargeDataset) SET n.processed = true RETURN count(n)',
        {},
        { timeout: 0 } // Disable transaction-level timeout
      );
    });
    console.log('Transaction completed:', result.records[0].get('count(n)'));
  } finally {
    await session.close();
  }
}

Quick Heads-Up

Disabling timeouts entirely can be risky if you have runaway queries—consider using a very large millisecond value (like 300000 for 5 minutes) instead of 0 if you want a safety net. Also, if you're working with a Neo4j cluster, double-check that your cluster-level settings don't override these driver configurations.

内容的提问来源于stack exchange,提问作者Aerodynamika

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:54:23