@@ -12,12 +12,12 @@ management.
1212 Prevents violation of access policies and/or a glut of open file handles and
1313 (mostly) idle threads
1414 * ** Connection pooling** :
15- * Read-only pool for concurrent reads (up to 6 connections)
15+ * Read-only pool for concurrent reads (default: 6 connections, configurable )
1616 * ** Lazy write pool** : Single write connection pool (max_connections=1) initialized on
1717 first use
1818 * ** Exclusive write access** : WriteGuard ensures serialized writes
19- * ** WAL mode** : Automatically enabled on first ` acquire_writer() ` call
20- ( idempotent)
19+ * ** WAL mode** : Automatically enabled on first ` acquire_writer() ` call (setting
20+ journal mode to WAL is safe and idempotent)
2121 * See [ WAL documentation] ( https://www.sqlite.org/wal.html ) for details
2222 * ** 30-second idle timeout** : Both read and write connections close after
2323 30 seconds of inactivity
@@ -44,18 +44,19 @@ use std::sync::Arc;
4444#[tokio:: main]
4545async fn main () -> Result <(), sqlx_sqlite_conn_mgr :: Error > {
4646 // Connect to database (creates if missing, returns Arc<SqliteDatabase>)
47+ // (See below for how to customize the configuration)
4748 let db = SqliteDatabase :: connect (" example.db" ). await ? ;
4849
4950 // Multiple connects to the same path return the same instance
5051 let db2 = SqliteDatabase :: connect (" example.db" ). await ? ;
5152 assert! (Arc :: ptr_eq (& db , & db2 ));
5253
53- // Use read_pool() for SELECT queries (supports concurrent reads)
54+ // Use read_pool() for read queries (supports concurrent reads)
5455 let rows = query (" SELECT * FROM users" )
5556 . fetch_all (db . read_pool ()? )
5657 . await ? ;
5758
58- // Optionally acquire writer for INSERT/UPDATE/DELETE (exclusive access)
59+ // Optionally acquire writer for write queries (exclusive access)
5960 // WAL mode is enabled automatically on first call
6061 let mut writer = db . acquire_writer (). await ? ;
6162 query (" INSERT INTO users (name) VALUES (?)" )
@@ -70,12 +71,39 @@ async fn main() -> Result<(), sqlx_sqlite_conn_mgr::Error> {
7071}
7172```
7273
74+ ### Custom Configuration
75+
76+ Only customize the configuration when the defaults don't meet your requirements:
77+
78+ ``` rust
79+ use sqlx_sqlite_conn_mgr :: {SqliteDatabase , SqliteDatabaseConfig };
80+ use std :: time :: Duration ;
81+
82+ #[tokio:: main]
83+ async fn main () -> Result <(), sqlx_sqlite_conn_mgr :: Error > {
84+ // Only create custom configuration when defaults aren't suitable
85+ let custom_config = SqliteDatabaseConfig {
86+ max_read_connections : 10 ,
87+ idle_timeout : Duration :: from_secs (60 ),
88+ };
89+
90+ // Pass custom configuration to connect()
91+ let db = SqliteDatabase :: connect (" example.db" , Some (custom_config )). await ? ;
92+
93+ // Use the database as normal...
94+ db . close (). await ? ;
95+ Ok (())
96+ }
97+ ```
98+
7399## API Overview
74100
75101### ` SqliteDatabase `
76102
77- * ` connect(path) ` - Connect to a database (creates if missing, returns cached
78- ` Arc<SqliteDatabase> ` if already open)
103+ * ` connect(path, custom_config) ` - Connect to a database (creates if missing,
104+ returns cached ` Arc<SqliteDatabase> ` if already open). Pass ` None ` for
105+ ` custom_config ` to use defaults (recommended for most use cases), or
106+ ` Some(SqliteDatabaseConfig) ` when you need to customize the configuration
79107 * ` read_pool() ` - Get reference to the read-only connection pool for read
80108 operations (returns ` Result ` )
81109 * ` acquire_writer() ` - Acquire exclusive write access (returns
@@ -85,6 +113,15 @@ async fn main() -> Result<(), sqlx_sqlite_conn_mgr::Error> {
85113 * ` close_and_remove() ` - Close and delete all database files (.db, .db-wal,
86114 .db-shm)
87115
116+ ### ` SqliteDatabaseConfig `
117+
118+ Configuration for connection pool behavior:
119+
120+ * ` max_read_connections: u32 ` - Maximum number of concurrent read connections
121+ (default: 6)
122+ * ` idle_timeout: Duration ` - How long idle connections remain open before
123+ being closed (default: 30 seconds)
124+
88125### ` WriteGuard `
89126
90127RAII guard that provides exclusive write access. Automatically returns the
@@ -111,12 +148,11 @@ queries.
111148 is released via ` WriteGuard ` drop.
112149
1131505 . ** Connection Management** :
114- * Read pool: 6 concurrent connections by default, 0 cached
115- * Can be configured via ` SqliteDatabaseConfig `
116- * Write pool: max 1 connection, 0 cached
117- * Idle timeout: 30 seconds for both pools
118- * Can be configured via ` SqliteDatabaseConfig `
119- * No perpetual caching to minimize idle thread overhead
151+ * Read pool: 6 concurrent connections by default (configurable via ` custom_config ` )
152+ * Write pool: max 1 connection
153+ * Minimum connections: 0 (no perpetual caching)
154+ * Idle timeout: 30 seconds by default (configurable via ` custom_config ` )
155+ * Only customize ` SqliteDatabaseConfig ` when defaults don't meet your needs
120156
121157## Error Handling
122158
0 commit comments