Skip to content

Conversation

@plasticbox
Copy link

PR Info

Motivation:
When working with multiple connections to a single database and executing various queries concurrently, there was a need to directly control the number of prepared statements for better resource management. This feature allows fine-grained control over prepared statement caching to optimize memory usage and connection pool efficiency.

Note: This implementation was initially prototyped with the help of an AI assistant, so some parts of the code may not yet fully align with SeaORM’s internal design or best practices. I’m very open to feedback and happy to revise the approach as needed.

In our project we currently have a concrete need for this kind of fine-grained control over prepared statement persistence. Even if this exact pull request is not accepted, I would be grateful if a similar mechanism or configuration option could be considered for inclusion in SeaORM.

New Features

  • Added persistent option to control prepared statement caching behavior

    • Added persistent: Option<bool> field to Statement struct
    • Added persistent() method to query builders (Select, Update, Insert, Delete, Paginator)
    • Allows fine-grained control over prepared statement caching on a per-query basis
  • Added default persistent option at database connection level

    • Added default_persistent field to SQLx pool connections (MySQL, Postgres, SQLite)
    • Added with_default_persistent() method to set the default caching behavior
    • When set, the default value is applied to all queries unless explicitly overridden

Changes

  • Modified Statement struct to include persistent: Option<bool> field
  • Updated query executors to support the persistent option
  • Enhanced database drivers (SQLite, MySQL, PostgresSQL) to apply persistent settings
  • Added API methods for controlling prepared statement caching at both connection and query levels

Usage Example

// Set default persistent behavior at connection level
let mut db = Database::connect(url).await?;
db.with_default_persistent(false); // Disable statement caching by default

// Override per query
let results = Entity::find()
    .persistent(true) // Enable caching for this specific query
    .all(&db)
    .await?;

// Or use with update
Update::one(model)
    .persistent(false) // Disable caching for this update
    .exec(&db)
    .await?;

This feature is useful for scenarios where prepared statement caching may cause issues (e.g., memory constraints, connection pooling limitations) while still allowing selective caching for performance-critical queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant