Connection API Reference¶
This module provides connection management for SurrealDB with pooling and registry support.
Connection Classes¶
- class surrealengine.connection.SurrealEngineAsyncConnection(url=None, namespace=None, database=None, username=None, password=None, name=None, make_default=False, use_pool=False, pool_size=10, max_idle_time=60, connect_timeout=30, operation_timeout=30, retry_limit=3, retry_delay=1.0, retry_backoff=2.0, validate_on_borrow=True, health_check_interval=30)[source]¶
Bases:
objectAsynchronous connection manager for SurrealDB.
This class manages the asynchronous connection to a SurrealDB database, providing methods for connecting, disconnecting, and executing transactions. It also provides access to the database through the db property.
- url¶
The URL of the SurrealDB server
- namespace¶
The namespace to use
- database¶
The database to use
- username¶
The username for authentication
- password¶
The password for authentication
- client¶
The SurrealDB async client instance or ConnectionPoolClient
- use_pool¶
Whether to use a connection pool
- pool¶
The connection pool if use_pool is True
- pool_size¶
The size of the connection pool
- max_idle_time¶
Maximum time in seconds a connection can be idle before being closed
- __init__(url=None, namespace=None, database=None, username=None, password=None, name=None, make_default=False, use_pool=False, pool_size=10, max_idle_time=60, connect_timeout=30, operation_timeout=30, retry_limit=3, retry_delay=1.0, retry_backoff=2.0, validate_on_borrow=True, health_check_interval=30)[source]¶
Initialize a new SurrealEngineAsyncConnection.
- Parameters:
url (str | None) – The URL of the SurrealDB server
namespace (str | None) – The namespace to use
database (str | None) – The database to use
username (str | None) – The username for authentication
password (str | None) – The password for authentication
name (str | None) – The name to register this connection under in the registry
make_default (bool) – Whether to set this connection as the default
use_pool (bool) – Whether to use a connection pool
pool_size (int) – The size of the connection pool
max_idle_time (int) – Maximum time in seconds a connection can be idle before being closed
connect_timeout (int) – Timeout in seconds for establishing a connection
operation_timeout (int) – Timeout in seconds for operations
retry_limit (int) – Maximum number of retries for failed operations
retry_delay (float) – Initial delay in seconds between retries
retry_backoff (float) – Backoff multiplier for retry delay
validate_on_borrow (bool) – Whether to validate connections when borrowing from the pool
- async __aenter__()[source]¶
Enter the async context manager.
- Returns:
The connection instance
- Return type:
- async __aexit__(exc_type, exc_val, exc_tb)[source]¶
Exit the async context manager.
- Parameters:
exc_type (Type[BaseException] | None) – The exception type, if an exception was raised
exc_val (BaseException | None) – The exception value, if an exception was raised
exc_tb (Any | None) – The exception traceback, if an exception was raised
- property db: SurrealEngine¶
Get dynamic table accessor.
- Returns:
A SurrealEngine instance for accessing tables dynamically
- async connect()[source]¶
Connect to the database.
This method creates a new client if one doesn’t exist. If use_pool is True, it creates a connection pool and a ConnectionPoolClient. Otherwise, it creates a direct connection to the database.
- Returns:
The SurrealDB client instance or ConnectionPoolClient
- Return type:
- async disconnect()[source]¶
Disconnect from the database.
This method closes the client connection if one exists. If use_pool is True, it closes the connection pool.
- class surrealengine.connection.SurrealEngineSyncConnection(url=None, namespace=None, database=None, username=None, password=None, name=None, make_default=False)[source]¶
Bases:
objectSynchronous connection manager for SurrealDB.
This class manages the synchronous connection to a SurrealDB database, providing methods for connecting, disconnecting, and executing transactions. It also provides access to the database through the db property.
- url¶
The URL of the SurrealDB server
- namespace¶
The namespace to use
- database¶
The database to use
- username¶
The username for authentication
- password¶
The password for authentication
- client¶
The SurrealDB sync client instance
- __init__(url=None, namespace=None, database=None, username=None, password=None, name=None, make_default=False)[source]¶
Initialize a new SurrealEngineSyncConnection.
- Parameters:
url (str | None) – The URL of the SurrealDB server
namespace (str | None) – The namespace to use
database (str | None) – The database to use
username (str | None) – The username for authentication
password (str | None) – The password for authentication
name (str | None) – The name to register this connection under in the registry
make_default (bool) – Whether to set this connection as the default
- __exit__(exc_type, exc_val, exc_tb)[source]¶
Exit the sync context manager.
- Parameters:
exc_type (Type[BaseException] | None) – The exception type, if an exception was raised
exc_val (BaseException | None) – The exception value, if an exception was raised
exc_tb (Any | None) – The exception traceback, if an exception was raised
- property db: SurrealEngine¶
Get dynamic table accessor.
- Returns:
A SurrealEngine instance for accessing tables dynamically
- connect()[source]¶
Connect to the database.
This method creates a new client if one doesn’t exist, signs in if credentials are provided, and sets the namespace and database.
- Returns:
The SurrealDB client instance
- Return type:
- disconnect()[source]¶
Disconnect from the database.
This method closes the client connection if one exists.
- class surrealengine.connection.BaseSurrealEngineConnection(*args, **kwargs)[source]¶
Bases:
ProtocolProtocol defining the interface for SurrealDB connections.
This protocol defines the common interface that both synchronous and asynchronous connections must implement.
- property db: SurrealEngine¶
Get dynamic table accessor.
- __init__(*args, **kwargs)¶
Connection Pooling¶
- class surrealengine.connection.ConnectionPoolBase(url, namespace=None, database=None, username=None, password=None, pool_size=10, max_idle_time=60, connect_timeout=30, operation_timeout=30, retry_limit=3, retry_delay=1.0, retry_backoff=2.0, validate_on_borrow=True, health_check_interval=30)[source]¶
Bases:
ABCBase class for connection pools.
This abstract class defines the common interface and functionality for both synchronous and asynchronous connection pools.
- url¶
The URL of the SurrealDB server
- namespace¶
The namespace to use
- database¶
The database to use
- username¶
The username for authentication
- password¶
The password for authentication
- pool_size¶
Maximum number of connections in the pool
- max_idle_time¶
Maximum time in seconds a connection can be idle before being closed
- connect_timeout¶
Timeout in seconds for establishing a connection
- operation_timeout¶
Timeout in seconds for operations
- retry_limit¶
Maximum number of retries for failed operations
- retry_delay¶
Initial delay in seconds between retries
- retry_backoff¶
Backoff multiplier for retry delay
- validate_on_borrow¶
Whether to validate connections when borrowing from the pool
- __init__(url, namespace=None, database=None, username=None, password=None, pool_size=10, max_idle_time=60, connect_timeout=30, operation_timeout=30, retry_limit=3, retry_delay=1.0, retry_backoff=2.0, validate_on_borrow=True, health_check_interval=30)[source]¶
Initialize a new ConnectionPoolBase.
- Parameters:
url (str) – The URL of the SurrealDB server
namespace (str | None) – The namespace to use
database (str | None) – The database to use
username (str | None) – The username for authentication
password (str | None) – The password for authentication
pool_size (int) – Maximum number of connections in the pool
max_idle_time (int) – Maximum time in seconds a connection can be idle before being closed
connect_timeout (int) – Timeout in seconds for establishing a connection
operation_timeout (int) – Timeout in seconds for operations
retry_limit (int) – Maximum number of retries for failed operations
retry_delay (float) – Initial delay in seconds between retries
retry_backoff (float) – Backoff multiplier for retry delay
validate_on_borrow (bool) – Whether to validate connections when borrowing from the pool
- abstractmethod create_connection()[source]¶
Create a new connection.
- Returns:
A new connection
- Return type:
- abstractmethod close_connection(connection)[source]¶
Close a connection.
- Parameters:
connection (Any) – The connection to close
- abstractmethod get_connection()[source]¶
Get a connection from the pool.
- Returns:
A connection from the pool
- Return type:
- class surrealengine.connection.AsyncConnectionPool(url, namespace=None, database=None, username=None, password=None, pool_size=10, max_idle_time=60, connect_timeout=30, operation_timeout=30, retry_limit=3, retry_delay=1.0, retry_backoff=2.0, validate_on_borrow=True, health_check_interval=30)[source]¶
Bases:
ConnectionPoolBaseAsynchronous connection pool for SurrealDB.
This class manages a pool of asynchronous connections to a SurrealDB database. It handles connection creation, validation, and reuse, and provides methods for acquiring and releasing connections.
The connections returned by this pool are wrapped in SurrealEngineAsyncConnection objects, which can be used with the Document class and other SurrealEngine functionality that expects a SurrealEngineAsyncConnection.
- pool¶
List of available connections
- in_use¶
Dictionary of connections currently in use and their timestamps
- lock¶
Asyncio lock for thread-safe operations
- closed¶
Whether the pool is closed
- __init__(url, namespace=None, database=None, username=None, password=None, pool_size=10, max_idle_time=60, connect_timeout=30, operation_timeout=30, retry_limit=3, retry_delay=1.0, retry_backoff=2.0, validate_on_borrow=True, health_check_interval=30)[source]¶
Initialize a new AsyncConnectionPool.
- Parameters:
url (str) – The URL of the SurrealDB server
namespace (str | None) – The namespace to use
database (str | None) – The database to use
username (str | None) – The username for authentication
password (str | None) – The password for authentication
pool_size (int) – Maximum number of connections in the pool
max_idle_time (int) – Maximum time in seconds a connection can be idle before being closed
connect_timeout (int) – Timeout in seconds for establishing a connection
operation_timeout (int) – Timeout in seconds for operations
retry_limit (int) – Maximum number of retries for failed operations
retry_delay (float) – Initial delay in seconds between retries
retry_backoff (float) – Backoff multiplier for retry delay
validate_on_borrow (bool) – Whether to validate connections when borrowing from the pool
- async create_connection()[source]¶
Create a new connection.
- Returns:
A new connection wrapped in SurrealEngineAsyncConnection
- Raises:
Exception – If the connection cannot be created
- Return type:
- async validate_connection(connection)[source]¶
Validate a connection.
- Parameters:
connection (SurrealEngineAsyncConnection) – The connection to validate (SurrealEngineAsyncConnection)
- Returns:
True if the connection is valid, False otherwise
- Return type:
- async close_connection(connection)[source]¶
Close a connection.
- Parameters:
connection (SurrealEngineAsyncConnection) – The connection to close (SurrealEngineAsyncConnection)
- async get_connection()[source]¶
Get a connection from the pool.
- Returns:
A SurrealEngineAsyncConnection from the pool
- Raises:
RuntimeError – If the pool is closed
Exception – If a connection cannot be obtained
- Return type:
- async return_connection(connection)[source]¶
Return a connection to the pool.
- Parameters:
connection (SurrealEngineAsyncConnection) – The connection to return (SurrealEngineAsyncConnection)
- class surrealengine.connection.SyncConnectionPool(url, namespace=None, database=None, username=None, password=None, pool_size=10, max_idle_time=60, connect_timeout=30, operation_timeout=30, retry_limit=3, retry_delay=1.0, retry_backoff=2.0, validate_on_borrow=True, health_check_interval=30)[source]¶
Bases:
ConnectionPoolBaseSynchronous connection pool for SurrealDB.
This class manages a pool of synchronous connections to a SurrealDB database. It handles connection creation, validation, and reuse, and provides methods for acquiring and releasing connections.
The connections returned by this pool are wrapped in SurrealEngineSyncConnection objects, which can be used with the Document class and other SurrealEngine functionality that expects a SurrealEngineSyncConnection.
- pool¶
Queue of available connections
- in_use¶
Set of connections currently in use
- lock¶
Lock for thread-safe operations
- closed¶
Whether the pool is closed
- __init__(url, namespace=None, database=None, username=None, password=None, pool_size=10, max_idle_time=60, connect_timeout=30, operation_timeout=30, retry_limit=3, retry_delay=1.0, retry_backoff=2.0, validate_on_borrow=True, health_check_interval=30)[source]¶
Initialize a new SyncConnectionPool.
- Parameters:
url (str) – The URL of the SurrealDB server
namespace (str | None) – The namespace to use
database (str | None) – The database to use
username (str | None) – The username for authentication
password (str | None) – The password for authentication
pool_size (int) – Maximum number of connections in the pool
max_idle_time (int) – Maximum time in seconds a connection can be idle before being closed
connect_timeout (int) – Timeout in seconds for establishing a connection
operation_timeout (int) – Timeout in seconds for operations
retry_limit (int) – Maximum number of retries for failed operations
retry_delay (float) – Initial delay in seconds between retries
retry_backoff (float) – Backoff multiplier for retry delay
validate_on_borrow (bool) – Whether to validate connections when borrowing from the pool
- create_connection()[source]¶
Create a new connection.
- Returns:
A new connection wrapped in SurrealEngineSyncConnection
- Raises:
Exception – If the connection cannot be created
- Return type:
- validate_connection(connection)[source]¶
Validate a connection.
- Parameters:
connection (SurrealEngineSyncConnection) – The connection to validate (SurrealEngineSyncConnection)
- Returns:
True if the connection is valid, False otherwise
- Return type:
- close_connection(connection)[source]¶
Close a connection.
- Parameters:
connection (SurrealEngineSyncConnection) – The connection to close (SurrealEngineSyncConnection)
- get_connection()[source]¶
Get a connection from the pool.
- Returns:
A SurrealEngineSyncConnection from the pool
- Raises:
RuntimeError – If the pool is closed
Exception – If a connection cannot be obtained
- Return type:
- return_connection(connection)[source]¶
Return a connection to the pool.
- Parameters:
connection (SurrealEngineSyncConnection) – The connection to return (SurrealEngineSyncConnection)
Connection Registry¶
- class surrealengine.connection.ConnectionRegistry[source]¶
Bases:
objectGlobal connection registry for SurrealDB.
This class provides a centralized registry for managing database connections. It allows setting a default connection and registering named connections that can be retrieved throughout the application.
- _default_async_connection¶
The default async connection to use when none is specified
- _default_sync_connection¶
The default sync connection to use when none is specified
- Type:
- _async_connections¶
Dictionary of named async connections
- Type:
Dict[str, surrealengine.connection.SurrealEngineAsyncConnection]
- _sync_connections¶
Dictionary of named sync connections
- Type:
Dict[str, surrealengine.connection.SurrealEngineSyncConnection]
- classmethod set_default_async_connection(connection)[source]¶
Set the default async connection.
- Parameters:
connection (SurrealEngineAsyncConnection) – The async connection to set as default
- classmethod set_default_sync_connection(connection)[source]¶
Set the default sync connection.
- Parameters:
connection (SurrealEngineSyncConnection) – The sync connection to set as default
- classmethod set_default_connection(connection)[source]¶
Set the default connection based on its type.
- Parameters:
connection (SurrealEngineAsyncConnection | SurrealEngineSyncConnection) – The connection to set as default
- classmethod get_default_async_connection()[source]¶
Get the default async connection.
- Returns:
The default async connection
- Raises:
RuntimeError – If no default async connection has been set
- Return type:
- classmethod get_default_sync_connection()[source]¶
Get the default sync connection.
- Returns:
The default sync connection
- Raises:
RuntimeError – If no default sync connection has been set
- Return type:
- classmethod get_default_connection(async_mode=True)[source]¶
Get the default connection based on the mode.
- Parameters:
async_mode (bool) – Whether to get the async or sync connection
- Returns:
The default connection of the requested type
- Raises:
RuntimeError – If no default connection of the requested type has been set
- Return type:
- classmethod add_async_connection(name, connection)[source]¶
Add a named async connection to the registry.
- Parameters:
name (str) – The name to register the connection under
connection (SurrealEngineAsyncConnection) – The async connection to register
- classmethod add_sync_connection(name, connection)[source]¶
Add a named sync connection to the registry.
- Parameters:
name (str) – The name to register the connection under
connection (SurrealEngineSyncConnection) – The sync connection to register
- classmethod add_connection(name, connection)[source]¶
Add a named connection to the registry based on its type.
- Parameters:
name (str) – The name to register the connection under
connection (SurrealEngineAsyncConnection | SurrealEngineSyncConnection) – The connection to register
- classmethod get_async_connection(name)[source]¶
Get a named async connection from the registry.
- classmethod get_sync_connection(name)[source]¶
Get a named sync connection from the registry.
Utility Functions¶
- surrealengine.connection.create_connection(url=None, namespace=None, database=None, username=None, password=None, name=None, make_default=False, async_mode=True, use_pool=False, pool_size=10, max_idle_time=60, connect_timeout=30, operation_timeout=30, retry_limit=3, retry_delay=1.0, retry_backoff=2.0, validate_on_borrow=True, auto_connect=False, health_check_interval=30)[source]¶
Factory function to create a connection of the appropriate type.
- Parameters:
url (str | None) – The URL of the SurrealDB server
namespace (str | None) – The namespace to use
database (str | None) – The database to use
username (str | None) – The username for authentication
password (str | None) – The password for authentication
name (str | None) – The name to register this connection under in the registry
make_default (bool) – Whether to set this connection as the default
async_mode (bool) – Whether to create an async or sync connection
use_pool (bool) – Whether to use a connection pool (async_mode only)
pool_size (int) – The size of the connection pool
max_idle_time (int) – Maximum time in seconds a connection can be idle before being closed
connect_timeout (int) – Timeout in seconds for establishing a connection
operation_timeout (int) – Timeout in seconds for operations
retry_limit (int) – Maximum number of retries for failed operations
retry_delay (float) – Initial delay in seconds between retries
retry_backoff (float) – Backoff multiplier for retry delay
validate_on_borrow (bool) – Whether to validate connections when borrowing from the pool
auto_connect (bool) – Whether to automatically connect the connection
health_check_interval (int) – Background health check interval (seconds) for pools
- Returns:
A connection of the requested type
- Return type:
Examples
Basic async connection:
>>> connection = create_connection( ... url="ws://localhost:8001/rpc", ... namespace="test_ns", ... database="test_db", ... username="root", ... password="root", ... make_default=True ... ) >>> # await connection.connect() # in async context
Sync connection:
>>> connection = create_connection( ... url="ws://localhost:8001/rpc", ... namespace="test_ns", ... database="test_db", ... username="root", ... password="root", ... async_mode=False, ... make_default=True ... ) >>> connection.connect()
Connection with pooling:
>>> connection = create_connection( ... url="ws://localhost:8000/rpc", ... namespace="production", ... database="app_db", ... username="app_user", ... password="secure_password", ... use_pool=True, ... pool_size=20, ... make_default=True ... )
Named connection with context manager:
>>> connection = create_connection( ... url="ws://db:8000/rpc", ... namespace="test_ns", ... database="test_db", ... username="root", ... password="root", ... name="test_connection" ... ) >>> # async with connection: # in async context ... # # Use connection ... # pass
- surrealengine.connection.parse_connection_string(connection_string)[source]¶
Parse a connection string into a dictionary of connection parameters.
Supports the following formats: - surrealdb://user:pass@host:port/namespace/database?param1=value1¶m2=value2 (maps to ws://) - wss://user:pass@host:port/namespace/database?param1=value1¶m2=value2 - ws://user:pass@host:port/namespace/database?param1=value1¶m2=value2 - http://user:pass@host:port/namespace/database?param1=value1¶m2=value2 - https://user:pass@host:port/namespace/database?param1=value1¶m2=value2
Connection string parameters: - pool_size: Maximum number of connections in the pool (default: 10) - max_idle_time: Maximum time in seconds a connection can be idle before being closed (default: 60) - connect_timeout: Timeout in seconds for establishing a connection (default: 30) - operation_timeout: Timeout in seconds for operations (default: 30) - retry_limit: Maximum number of retries for failed operations (default: 3) - retry_delay: Initial delay in seconds between retries (default: 1) - retry_backoff: Backoff multiplier for retry delay (default: 2) - validate_on_borrow: Whether to validate connections when borrowing from the pool (default: true)
- Parameters:
connection_string (str) – The connection string to parse
- Returns:
A dictionary containing the parsed connection parameters
- Raises:
ValueError – If the connection string is invalid
- Return type:
Event Handling¶
- class surrealengine.connection.ConnectionEvent[source]¶
Bases:
objectEvent types for connection events.
This class defines the event types that can be emitted by connections.
- CONNECTED = 'connected'¶
- DISCONNECTED = 'disconnected'¶
- RECONNECTING = 'reconnecting'¶
- RECONNECTED = 'reconnected'¶
- CONNECTION_FAILED = 'connection_failed'¶
- RECONNECTION_FAILED = 'reconnection_failed'¶
- CONNECTION_CLOSED = 'connection_closed'¶
- class surrealengine.connection.ConnectionEventListener[source]¶
Bases:
objectListener for connection events.
This class defines the interface for connection event listeners.
- class surrealengine.connection.ConnectionEventEmitter[source]¶
Bases:
objectEmitter for connection events.
This class provides methods for registering listeners and emitting events.
- listeners¶
List of registered event listeners
- add_listener(listener)[source]¶
Add a listener for connection events.
- Parameters:
listener (ConnectionEventListener) – The listener to add
- remove_listener(listener)[source]¶
Remove a listener for connection events.
- Parameters:
listener (ConnectionEventListener) – The listener to remove
Operation Queue¶
- class surrealengine.connection.OperationQueue(maxsize=0, drop_policy='block')[source]¶
Bases:
objectQueue for operations during reconnection with backpressure and metrics.
This class manages a queue of operations that are waiting for a connection to be reestablished. Once the connection is restored, the operations are executed in the order they were queued.
- sync_operations¶
Queue of synchronous operations
- async_operations¶
Queue of asynchronous operations
- is_reconnecting¶
Whether the connection is currently reconnecting
- reconnection_event¶
Event that is set when reconnection is complete
- async_reconnection_event¶
Asyncio event that is set when reconnection is complete
- __init__(maxsize=0, drop_policy='block')[source]¶
Initialize a new OperationQueue. :param maxsize: Maximum queued operations per list (0 = unbounded) :param drop_policy: One of ‘block’ | ‘drop_oldest’ | ‘error’
- start_reconnection()[source]¶
Start the reconnection process.
This method marks the connection as reconnecting and clears the reconnection events.
- end_reconnection()[source]¶
End the reconnection process.
This method marks the connection as no longer reconnecting and sets the reconnection events.
Reconnection Strategy¶
- class surrealengine.connection.ReconnectionStrategy(max_attempts=10, initial_delay=1.0, max_delay=60.0, backoff_factor=2.0)[source]¶
Bases:
objectStrategy for reconnecting to the database.
This class provides methods for reconnecting to the database with configurable retry limits and backoff strategies.
- max_attempts¶
Maximum number of reconnection attempts
- initial_delay¶
Initial delay in seconds between reconnection attempts
- max_delay¶
Maximum delay in seconds between reconnection attempts
- backoff_factor¶
Backoff multiplier for reconnection delay
Retry Strategy¶
- class surrealengine.connection.RetryStrategy(retry_limit=3, retry_delay=1.0, retry_backoff=2.0)[source]¶
Bases:
objectStrategy for retrying operations with exponential backoff.
This class provides methods for retrying operations with configurable retry limits and backoff strategies.
- retry_limit¶
Maximum number of retries
- retry_delay¶
Initial delay in seconds between retries
- retry_backoff¶
Backoff multiplier for retry delay
- __init__(retry_limit=3, retry_delay=1.0, retry_backoff=2.0)[source]¶
Initialize a new RetryStrategy.