Skip to content

Connection pooling

sparqlite automatically reuses HTTP connections across multiple requests to the same endpoint, reducing latency and improving performance.

When you create a SPARQLClient, it initializes a persistent connection that is reused for all subsequent requests. This avoids the overhead of establishing a new TCP connection and TLS handshake for each query.

from sparqlite import SPARQLClient
# Connection is established on first request
client = SPARQLClient("https://dbpedia.org/sparql")
# First query - establishes connection
result1 = client.query("SELECT ?s WHERE { ?s ?p ?o } LIMIT 1")
# Subsequent queries - reuse existing connection (faster)
result2 = client.query("SELECT ?p WHERE { ?s ?p ?o } LIMIT 1")
result3 = client.query("SELECT ?o WHERE { ?s ?p ?o } LIMIT 1")
# Clean up when done
client.close()

The recommended way to use sparqlite is with a context manager, which automatically closes the connection when done:

from sparqlite import SPARQLClient
with SPARQLClient("https://dbpedia.org/sparql") as client:
result1 = client.query("SELECT ?s WHERE { ?s ?p ?o } LIMIT 1")
result2 = client.query("SELECT ?p WHERE { ?s ?p ?o } LIMIT 1")
# Connection is automatically closed when exiting the with block

If you are not using a context manager, call close() when you are done with the client:

client = SPARQLClient("https://dbpedia.org/sparql")
try:
result = client.query("SELECT * WHERE { ?s ?p ?o } LIMIT 5")
finally:
client.close()

If you forget to call close(), the connection will be closed when the client object is garbage collected. However, it is best practice to explicitly close connections to release resources promptly.

Each SPARQLClient instance should be used by a single thread. If you need to make concurrent requests, create separate client instances for each thread.