Engine Architecture
The Engine is the starting point for any SQLAlchemy Core application. It manages database connectivity, connection pooling, and dialect-specific behavior, serving as a factory for database connections.Overview
The Engine doesn’t establish actual database connections until they’re needed. Instead, it holds configuration and manages a connection pool that creates connections on demand.Creating an Engine
Basic Usage
Usecreate_engine() to create a new Engine instance:
The
create_engine() function does not establish any actual database connections immediately. Connections are created when first requested via Engine.connect() or when executing statements.Function Signature
URL Format
Database URLs follow the RFC-1738 format:Common URL Examples
URL Components
Database backend name:
postgresql, mysql, sqlite, oracle, mssql, etc.DBAPI driver name:
psycopg2, pymysql, cx_oracle, etc. If omitted, the default driver for the dialect is used.Database username. Must be URL-encoded if it contains special characters.
Database password. Must be URL-encoded if it contains special characters.
Hostname or IP address of the database server.
Port number. Each dialect has a default port if omitted.
Database name or path (for SQLite).
Query string parameters for driver-specific or dialect-specific options.
Programmatic URL Construction
For dynamic URL construction or when handling special characters:Engine Configuration Options
Logging and Echo
Enable SQL statement logging. Set to
True for statement logging, or "debug" to also log result sets.Enable connection pool logging. Set to
"debug" for detailed pool checkout/checkin logging.When
True, SQL parameter values are hidden from logs for security.Connection Pool Configuration
See the Connection Management page for detailed pool configuration.Number of connections to maintain in the pool.
Maximum number of connections that can be created beyond
pool_size.Seconds to wait for a connection from the pool before raising an error.
Recycle connections after this many seconds. Useful for databases that close idle connections.
Test connections for liveness before using them.
Use last-in-first-out for pool retrieval, allowing server-side timeouts to close unused connections.
Transaction Isolation
Set the default transaction isolation level. Valid values depend on the database backend:
"SERIALIZABLE""REPEATABLE READ""READ COMMITTED""READ UNCOMMITTED""AUTOCOMMIT"
Performance Options
Size of the SQL compilation cache. Set to
0 to disable caching.Number of rows per batch in executemany operations with RETURNING support.
Engine from Configuration
Create an engine from a configuration dictionary (useful for config files):Engine Methods
Connecting
Executing (Context Manager with Transaction)
Disposal
Call
engine.dispose() when you need to ensure all database connections are closed, such as before forking a process or at application shutdown.Engine Properties
engine.dialect- The Dialect instance for the database backendengine.pool- The Pool instance managing connectionsengine.url- The URL object representing the connection stringengine.driver- The DBAPI driver name (e.g., “psycopg2”)engine.name- The database backend name (e.g., “postgresql”)
Custom DBAPI Connection
Override the default connection creation:Advanced: Custom Connection Arguments
Pass additional arguments to the DBAPIconnect() method:
Best Practices
Create Once, Use Many
Create one Engine per database URL in your application. The Engine is designed to be a global singleton.
Use Connection Context Managers
Always use context managers to ensure connections are properly returned to the pool.
Configure Pool Appropriately
Tune pool settings based on your application’s concurrency and database limits.
See Also
- Connection Management - Working with connections and transactions
- MetaData System - Organizing database schema definitions
- Schema Definition - Defining tables, columns, and constraints