如何避免neo4j-javascript-driver Node.js模块因超时退出?
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

