cjk
Hi there,
I use Cachex to cache very frequent queries:
@spec get_setting_for_forum(%Forum{}) :: %Setting{} | nil
def get_setting_for_forum(%Forum{} = forum) do
Cachex.fetch!(:cforum, "settings/forums/#{forum.forum_id}", fn ->
from(setting in Setting, where: setting.forum_id == ^forum.forum_id)
|> Repo.one()
end)
end
@spec get_setting_for_user(%User{}) :: %Setting{} | nil
def get_setting_for_user(%User{} = user) do
Cachex.fetch!(:cforum, "settings/users/#{user.user_id}", fn ->
from(setting in Setting, where: setting.user_id == ^user.user_id)
|> Repo.one()
end)
end
This function loads a settings object from the database. It works fine in production and dev code, but in tests I get this error:
** (Cachex.ExecutionError) connection not available and request was dropped from queue after 602ms. You can configure how long requests wait in the queue using :queue_target and :queue_interval. See DBConnection.start_link/2 for more information
I call the getter functions like this:
def load_relevant_settings(forum, user)
def load_relevant_settings(nil, nil) do
[get_global_setting()]
|> Enum.reject(&is_nil(&1))
end
def load_relevant_settings(%Forum{} = forum, nil) do
[get_global_setting(), get_setting_for_forum(forum)]
|> Enum.reject(&is_nil(&1))
end
def load_relevant_settings(nil, %User{} = user) do
[get_global_setting(), get_setting_for_user(user)]
|> Enum.reject(&is_nil(&1))
end
def load_relevant_settings(%Forum{} = forum, %User{} = user) do
[get_global_setting(), get_setting_for_forum(forum), get_setting_for_user(user)]
|> Enum.reject(&is_nil(&1))
end
I can’t figure out what’s going wrong. Has anybody an idea?
Edit: I already tried to set the pool size to a very high value (100), but it didn’t help.
Best regards,
CK
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
cjk
I think I figured out what happens. The full stacktrace is as follows:
The application is in the middle of a database operation. Since the
Ecto.Adapters.SQL.Sandboxadapter wraps every test case in a giant transaction there is only one ecto process available, right? This would then lead to a deadlock when requesting multiple times for a database connection, wouldn’t it?How can I circumvent this?
Edit: but if this is the case, why does everything work fine when not wrapping the getter functions in
Cachex.fetch?Best regards,
CK
cjk
I might have found the reason why it works when not using Cachex. Cachex spawns its own process, and the fallback function for querying the database gets executed in the context of the cachex process. And this process has no access to the database connection. Is this assumption right? If yes, how do I fix that w/o calling
Ecto.Adapters.SQL.Sandbox.allow?benwilson512
You can do:
cjk
Hi Ben,
sadly that doesn’t change anything. I still get the timeouts:
This issue causes me headaches
cjk
I could do something like this:
This solves the issue but this just circumvents the problem. I’d like to understand what’s happening and how to fix it the right way[tm].
benwilson512
Oh sorry it’s a timeout issue not an ownership issue, that wasn’t clear to me initially since the first stack trace didn’t include the actual error message.
To clear up a misconception though:
no. There is a pool of connections. This makes it possible to do concurrent tests. Each concurrent test case would checkout a different connection, and then each start their own transaction. This is why the
caller:argument exists to Repo calls, it can indicate to Ecto which connection process to use.Unfortunately none of that really helps answer the timeout question. I’ll have to dig into Cachex to see how Cachex.fetch! works.
cjk
The ownership issue was just an assumption I made (thus the question mark
) because it works when called from outside of the process of cachex.
Well, I will dig deeper into cachex then, thanks.
lasseebert
I face the same issue, but not with Cachex, just with a normal ecto transaction.
I run a bunch of db operations inside a transaction. Inside the transaction I call some logic in another process that performs a db-lookup (in other tables). This works fine when not running tests, but in the test it dies with
connection not available and request was dropped from queue after 989ms.I have not found a way around this yet.
benwilson512
lasseebert
Thanks @benwilson512, but I already use
:sharedmode on the connection, so it shouldn’t make a difference. (I also tried and it didn’t).I don’t get the
cannot find ownership processerror, which would be solved withallow/3or:shared, but aconnection not available.For now, I “solved” the issue by replacing the db-call inside the existing transaction with a mock that does not hit the db.
Here is a small example that reproduces my issue:
This is the setup code that is run before each db test:
It will give this error: