Database Connections & Pooling
Relevant source files
The following files were used as context for generating this wiki page:
SeaORM provides a robust abstraction layer for managing database connections and transactions across multiple backends including MySQL, PostgreSQL, and SQLite. The system supports both asynchronous execution via SQLx and synchronous execution for lightweight applications, while maintaining a unified API for query execution and transaction management.
Sources: src/lib.rs:1-15, sea-orm-sync/src/lib.rs:445-460
Connection Architecture
SeaORM utilizes a hierarchical connection structure. At the top level, DbConn serves as the primary gateway for database operations. It abstracts the underlying driver-specific connection details, allowing developers to write database-agnostic code. The architecture distinguishes between a standard connection pool and an active transaction.
Connection Types
| Component | Description |
|---|---|
DbConn | The main database connection handler, typically representing a connection pool. |
DatabaseConnection | An alias or underlying implementation for the database handle. |
MockDatabase | A specialized connection type used for unit testing without a live database. |
Transaction | A specialized connection state that ensures atomicity for multiple operations. |
Sources: src/lib.rs:468-472, src/docs.rs:77-85
Connection Workflow
The following diagram illustrates how a user request interacts with the SeaORM connection layer to execute queries.
This workflow ensures that application logic remains decoupled from the specific execution mode (async vs sync) or the physical database backend.
Sources: src/lib.rs:445-455, sea-orm-sync/src/lib.rs:440-450, CLAUDE.md:143-150
Transaction Management
Transactions in SeaORM are managed through the TransactionTrait. This trait provides methods to begin, commit, and rollback units of work. In SeaORM 2.0, transaction handling has been enhanced with support for advanced options such as isolation levels and SQLite-specific modes.
SQLite Transaction Modes
For SQLite backends, SeaORM supports specific transaction modes to handle concurrency and locking behavior.
| Mode | Description |
|---|---|
DEFERRED | No locks are acquired until the first database access. |
IMMEDIATE | Reserved locks are acquired immediately; no other connection can write. |
EXCLUSIVE | Exclusive locks are acquired; no other connection can read or write. |
Sources: changelog/2.0.0-rc.35.md:5-15
Transaction Execution Flow
The begin_with_options method allows fine-grained control over the transaction, including sqlite_transaction_mode, access modes, and isolation levels. Nested transactions are handled using SAVEPOINT logic.
Sources: changelog/2.0.0-rc.35.md:8-18
Synchronous vs. Asynchronous Connections
SeaORM provides two primary modes of operation to suit different project requirements.
Asynchronous (Default)
Powered by SQLx, this mode is designed for high-concurrency web services. It requires an async runtime (like Tokio or async-std) and uses await for all database interactions.
Sources: src/lib.rs:1-10, src/docs.rs:1-10
Synchronous (sea-orm-sync)
Provides the full SeaORM API without requiring an async runtime. This is ideal for lightweight CLI programs or environments where async overhead is undesirable. It replaces futures_util::lock::Mutex with std::sync::Mutex and strips async/await keywords through automated build transforms.
Sources: sea-orm-sync/src/lib.rs:440-444, sea-orm-sync/CLAUDE.md:1-10
Testing with Mock Connections
SeaORM enables rigorous unit testing via the MockDatabase component. This allows developers to simulate database responses and verify the generated SQL statements (Transaction Log) without a running database instance.
// Setup mock connection
let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([
[cake::Model { id: 1, name: "New York Cheese".into() }],
])
.into_connection();
// Verify logic
assert_eq!(cake::Entity::find().one(&db).await?, Some(cake::Model { id: 1, ... }));
// Compare against transaction log
assert_eq!(db.into_transaction_log(), [Transaction::from_sql_and_values(...)]);
Sources: src/docs.rs:77-105
Conclusion
Database connections and pooling in SeaORM are designed for flexibility and type safety. By abstracting driver-specific implementations into DbConn and providing unified transaction traits, SeaORM allows developers to switch between backends (MySQL, Postgres, SQLite), execution modes (Async, Sync), and environments (Live, Mock) with minimal code changes.