Change the Ownership Mode
Explore the different Ecto sandbox ownership modes including :auto, :manual, and :shared. Understand when to use each mode to control connection access during tests, manage database state consistency across processes, and handle concurrency in your Elixir test suite. This lesson helps you optimize sandbox testing strategies for reliable database application development.
Types of ownership modes
The ownership mode affects the way the sandbox interacts with different processes. We can use the following three different modes.
:auto:manual:shared
The :auto mode
With :auto, the sandbox functions like a normal pool. Each process gets its own connection from the pool and has exclusive access to the connection while it is checked out. Moreover, like a normal pool, the connections are checked out automatically when our code needs to run a database operation and checked back in when the operation completes. The connection is still in a sandboxed transaction rolled back when the connection is checked in, but we can’t be sure that we’ll get the same connection each time we access the database. This means that there’s no guarantee that our calls to insert! and get! in ...