<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="127973" data-post-id="127973">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Ah yes I see, this is different. It happens because the transaction is hogging the connection process, so there aren’t any other available connections. I’m not entirely sure how to solve that <img src="https://forum.elixirforum.com/images/emoji/apple/thinking.png?v=15" title=":thinking:" class="emoji" alt=":thinking:" loading="lazy" width="20" height="20"></p>
<aside class="quote no-group" data-username="lasseebert" data-post="9" data-topic="19078">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lasseebert/48/11921_2.png" class="avatar"> lasseebert:</div>
<blockquote>
<p>Inside the transaction I call some logic in another process that performs a db-lookup (in other tables).</p>
</blockquote>
</aside>
<p>Is it possible to do all of that lookup information before you open the transaction? This will help minimize the amount of time the transaction is open which is itself a perk, and then your transaction can focus on just doing the changes to the database.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="127973" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/ecto-timeout-errors-when-wrapping-into-cachex-calls-in-tests/19078/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-127973" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="127973"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="127976" data-post-id="127976">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="lasseebert" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lasseebert/120/11921_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  lasseebert
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It would be complex to do the lookup beforehand of the transaction, since it is some logic not directly related to the code in question that does the inner db lookup in another process.</p>
<p>Anyway, I solved this particular issue with a mock in my tests (and it runs fine in dev and prod)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="127976" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/ecto-timeout-errors-when-wrapping-into-cachex-calls-in-tests/19078/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-127976" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="127976"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="127999" data-post-id="127999">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="lasseebert" data-post="13" data-topic="19078">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lasseebert/48/11921_2.png" class="avatar"> lasseebert:</div>
<blockquote>
<p>I solved this particular issue with a mock</p>
</blockquote>
</aside>
<p>Good solution!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="127999" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/ecto-timeout-errors-when-wrapping-into-cachex-calls-in-tests/19078/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-127999" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="127999"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="205163" data-post-id="205163">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="thbar" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/thbar/120/19561_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  thbar
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi,</p>
<aside class="quote no-group" data-username="cjk" data-post="3" data-topic="19078" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cjk/48/13388_2.png" class="avatar"> cjk:</div>
<blockquote>
<p>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 <code>Ecto.Adapters.SQL.Sandbox.allow</code>?</p>
</blockquote>
</aside>
<p>First thanks to everyone in this thread! I’ve been faced with a similar situation (using Cachex with Ecto calls), and faced the same troubles with regard to ownership - all your input has been very useful.</p>
<p>I’m stacking up a bit more links in case it helps someone later, because Cachex has worked reliably for us.</p>
<p>In my cases, it isn’t pratical to pass <code>caller: xyz</code> to Ecto, because it would involve changing a lot of code calls. Maybe there is a trick to set the caller for all the subsequent calls in a given block maybe, but I haven’t searched for that yet.</p>
<p>As described in <a href="https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.Sandbox.html#module-allowances" class="inline-onebox" rel="noopener nofollow ugc">Ecto.Adapters.SQL.Sandbox — Ecto SQL v3.14.0</a>, if one wants to keep their tests async, then the shared mode cannot be used, and allowance must be used instead.</p>
<p>It can be tricky because Cachex creates a number of nested processes per cache, which one can see with:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">import Cachex.Spec, only: [cache: 1]
# ...
Cachex.Services.services(Cachex.Spec.cache(name: :my_cache))
</code></pre>
<p>which gives:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[
  {Cachex.Services.Janitor, #PID&lt;0.734.0&gt;, :worker, [Cachex.Services.Janitor]},
  {Cachex.Services.Courier, #PID&lt;0.733.0&gt;, :worker, [Cachex.Services.Courier]},
  {Cachex.Services.Locksmith.Queue, #PID&lt;0.732.0&gt;, :worker,
   [Cachex.Services.Locksmith.Queue]}
]
</code></pre>
<p>The function passed to <code>Cachex.fetch</code> to lazily-compute the value also (apparently) runs in a different process.</p>
<p>So far the trick I’ve used is to wrap the call to “fetch” in a helper which enables the allowance during tests:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MyApp.Cache do
  require Logger

  def fetch(cache_key, value_fn) do
    parent = self()

    comp_fn = fn key -&gt;
      if Mix.env() == :test do
        Ecto.Adapters.SQL.Sandbox.allow(DB.Repo, parent, self())
      end

      Logger.info("Generating cached value for key #{key}")
      {:commit, value_fn.()}
    end

    {operation, result} = Cachex.fetch(:my_app_cache, cache_key, comp_fn, ttl: :timer.seconds(60))
    # SNIP
</code></pre>
<p>Ultimately I will extract a behaviour out of this module, and use a stub in most of the tests, only keeping a real Cachex-interacting test for the test of <code>MyApp.Cache</code> itself.</p>
<p>If anyone has better solutions or more information, I’ll happily read you!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="205163" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/ecto-timeout-errors-when-wrapping-into-cachex-calls-in-tests/19078/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-205163" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="205163"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="205168" data-post-id="205168">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="al2o3cr" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/120/3457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  al2o3cr
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>One gotcha with this - AFAIK there’s no way to “un-allow” a connection, and Ecto gets grumpy if you call <code>allow</code> with a second PID. This makes for intermittently-failing tests when there’s a worker pool, as you’ll only get an error when a worker gets reused.</p>
<p>We ran into this issue with <code>:poolboy</code>; our fix was to have workers return <code>:stop</code> in tests so they don’t get re-used. This works, EXCEPT for very occasionally when there’s already a process waiting for a pool worker when the call returns; a fast-path optimization in Poolboy hands the now-exiting worker PID to the waiting process, which then fails <img src="https://forum.elixirforum.com/images/emoji/apple/frowning.png?v=15" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="205168" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/ecto-timeout-errors-when-wrapping-into-cachex-calls-in-tests/19078/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-205168" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="205168"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>