0Pricing
Neo4j Graph Database Fundamentals · Lezione

Pooling delle connessioni e gestione degli errori

Imparate a gestire in modo efficiente i pool di connessioni del driver Neo4j e a gestire errori e retry quando integrate Neo4j nelle applicazioni.

Pooling delle connessioni e gestione degli errori è una lezione Neo4j Graph Database Fundamentals gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Neo4j Graph Database Fundamentals, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Neo4j Graph Database Fundamentals include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

The Driver Is Long-Lived

A common mistake is creating a new driver per request. The Neo4j driver manages a connection pool and should be created once and reused for your app's lifetime.

from neo4j import GraphDatabase

driver = GraphDatabase.driver('neo4j://localhost:7687', auth=('neo4j', 'password'))
# reuse driver everywhere; close at shutdown

What a Connection Pool Does

The driver keeps a pool of open connections and hands them out to sessions on demand. This avoids the cost of opening a TCP connection per query.

Tuning Pool Size

You can configure the maximum pool size and connection lifetime to match your workload.

driver = GraphDatabase.driver(
    uri,
    auth=auth,
    max_connection_pool_size=50,
    max_connection_lifetime=3600
)

Sessions Are Cheap

Unlike drivers, sessions are lightweight. Open a session per unit of work and close it promptly, ideally with a context manager.

with driver.session() as session:
    result = session.run('MATCH (n) RETURN count(n) AS c')
    print(result.single()['c'])

Transient vs Permanent Errors

Neo4j errors are classified. Transient errors (like a leader switch in a cluster) can be safely retried. Permanent errors (like syntax errors) cannot.

Automatic Retries

Managed transaction functions automatically retry on transient errors, which is why they are the recommended way to run writes.

def add_person(tx, name):
    tx.run('CREATE (:Person {name: $name})', name=name)

with driver.session() as session:
    session.execute_write(add_person, 'Alice')

Catching Driver Exceptions

Wrap calls to handle failures gracefully and surface useful messages to your app.

from neo4j.exceptions import ServiceUnavailable, CypherSyntaxError

try:
    with driver.session() as s:
        s.run('MATCH (n) RETURN n')
except ServiceUnavailable:
    print('Database unreachable')
except CypherSyntaxError as e:
    print('Bad query:', e)

Timeouts

Set transaction timeouts so a slow query does not hold a connection forever, freeing the pool for other work.

with driver.session() as s:
    s.run('MATCH (n) RETURN n', timeout=5)

Closing Resources

Always close the driver on application shutdown to release pooled connections cleanly.

import atexit
atexit.register(driver.close)

Pool Exhaustion

If all connections are busy, new requests wait. Symptoms include rising latency. Fixes: close sessions promptly, raise pool size, or shorten slow queries.

Production Checklist

For robust integration:

  • One shared driver instance
  • Short-lived sessions
  • Managed transactions for retries
  • Timeouts and exception handling
  • Close driver on shutdown

Quick Check

Test your connection management knowledge.

Recap

You learned robust driver usage:

  • Reuse a single long-lived driver
  • Sessions are cheap; open and close per task
  • Managed transactions retry transient errors
  • Handle exceptions and set timeouts
  • Avoid pool exhaustion with prompt cleanup

Domande Frequenti

La lezione «Pooling delle connessioni e gestione degli errori» è gratuita?

Sì — il testo completo di «Pooling delle connessioni e gestione degli errori» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Neo4j Graph Database Fundamentals, passa a CoddyKit PRO. Il corso Neo4j Graph Database Fundamentals include 4 lezioni in totale.

Cosa imparerò in «Pooling delle connessioni e gestione degli errori»?

Imparate a gestire in modo efficiente i pool di connessioni del driver Neo4j e a gestire errori e retry quando integrate Neo4j nelle applicazioni. Eserciti Neo4j Graph Database Fundamentals con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Neo4j Graph Database Fundamentals?

Non è richiesta alcuna esperienza precedente. Neo4j Graph Database Fundamentals su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Pooling delle connessioni e gestione degli errori»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Neo4j Graph Database Fundamentals?

Sì. Ogni lezione Neo4j Graph Database Fundamentals include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Connessione con il driver Python
  2. Esecuzione programmatica di operazioni CRUD
  3. Gestione di transazioni e sessioni
  4. Pooling delle connessioni e gestione degli errori
← Torna a Neo4j Graph Database Fundamentals