Skip to content

Adapter config for default_transaction_mode: IMMEDIATE #153

Closed
@egze

Description

@egze

I am trying to use sqlite in a project and have noticed that there is no option to set a IMMEDIATE transaction mode globally.

This has been brought up before here: #151 and the conclusion was to have the responsibility on the caller.

I want to raise the question again to have a configuration on the adapter level and maybe even make it default.

Here are 2 convincing sources:

  1. https://fractaledmind.github.io/2024/04/15/sqlite-on-rails-the-how-and-why-of-optimal-performance/
    The author did extensive testing with Rails and concluded that it is not viable to have DEFERRED in a typical Rails application. And to me a typical Rails application has the same usage pattern as a Phoenix application.

In a context where you only have one connection or you have a large amount of transactions that only do read operations, this is great for performance, because it means that SQLite doesn’t have to acquire a lock on the database for every transaction, only for transactions that actually write to the database. The problem is that this is not the context Rails apps are in. In a production Rails application, not only will you have multiple connections to the database from multiple threads, Rails will only wrap database queries that write to the database in a transaction. And, when we write our own explicit transactions, it is essentially a guarantee that we will include a write operation. So, in a production Rails application, SQLite will be working with multiple connections and every transaction will include a write operation. This is the opposite of the context that SQLite’s default deferred transaction mode is optimized for.

  1. https://highperformancesqlite.com/watch/transaction-modes
    Aaron Francis is a well known database expert and his sqlite course is amazing. Here are some quotes:

So on the application side, whenever you connect to the database, you're going to want to set your transaction mode to immediate.

You'll need to look up for your specific framework how to connect to the database and how to make sure that all of the rights are wrapped in a begin immediate transaction. Most web frameworks wrap the rights in a transaction regardless, and so you're looking for the thing where you can set, alright, we want that to be an immediate transaction. I think the rails adapter for SQLite, I think that does that by default. I know in PHP, you can set a p d o attribute to set it to immediate. So you'll have to do a little bit of digging.

To summarise: IMMEDIATE transaction mode makes the most sense for a web application. Passing manually mode: :immediate is not a good experience.

Main question: Are the maintainers open to a config on adapter level? In Rails the config is called default_transaction_mode: IMMEDIATE, we could do the same.

I am happy to do the work, with some pointers where to start best.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions