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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I am not sure OP wanted distributed locks, I think they simply were looking for a way to achieve what other languages achieve with a lot of dancing on their forehead while singing “Hallelujah!” (i.e. mutexes, semaphores, condition variables and all the other horrors).</p>
<p>If there’s no distributed requirement then simply serializing access to certain pieces of data – or external resources – via a <code>GenServer</code> is plenty enough. And even if there <em>are</em> distributed requirements then you can just use Oban Pro with the right uniqueness parameters set. Done.</p>
<p>Normally I don’t pursue after OP accepted an answer but you might be doing disservice to them here by giving them a “lock equivalent”. And I am not sure it’s much of an equivalent with so many disclaimers.</p>
<p>Erlang BEAM VM’s philosophy is to avoid locking and just serialize access to things in a singular process. If there are multiple tables (whatever that means?) involved then why not access them in your <code>GenServer</code> message handler, one after another? A <code>GenServer</code> will never process more than one message at a time anyway. You can connect to 3 different databases and 5 different Amazon S3 buckets and 8 separate message queues with zero consistency and contention problems – integrate the saga pattern in your function (like <a href="https://hexdocs.pm/sage/readme.html" rel="noopener nofollow ugc">this library</a> does) and you’re done. Problem solved!</p>
<p>It works flawlessly unless you are stepping in the territory of C, C++, Rust et. al. in which case, well, you know, just code in them.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="292204" 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/how-to-achieve-atomicity-all-or-nothing/56611/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-292204" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="292204"
                     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="292208" data-post-id="292208">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="unfode" data-post="3" data-topic="56611">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/unfode/48/30977_2.png" class="avatar"> unfode:</div>
<blockquote>
<p>Suppose I have two tables in a database. I need to mutate both tables atomically in a transaction.</p>
</blockquote>
</aside>
<p>If you’re using Ecto, it has a built in way of achieving this via <code>Ecto.Multi</code></p>
<blockquote>
<p><a href="https://hexdocs.pm/ecto/Ecto.Multi.html#content" rel="noopener nofollow ugc"><code>Ecto.Multi</code></a> is a data structure for grouping multiple Repo operations.</p>
<p><a href="https://hexdocs.pm/ecto/Ecto.Multi.html#content" rel="noopener nofollow ugc"><code>Ecto.Multi</code></a> makes it possible to pack operations that should be performed in a single database transaction…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule PasswordManager do
  alias Ecto.Multi

  def reset(account, params) do
    Multi.new()
    |&gt; Multi.update(:account, Account.password_reset_changeset(account, params))
    |&gt; Multi.insert(:log, Log.password_reset_changeset(account, params))
    |&gt; Multi.delete_all(:sessions, Ecto.assoc(account, :sessions))
  end
end
</code></pre>
</blockquote> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="292208" data-batch-url="/posts/batch_likers">
                        4
                      </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/how-to-achieve-atomicity-all-or-nothing/56611/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-292208" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="292208"
                     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>