<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Spring Builders: john clsads</title>
    <description>The latest articles on Spring Builders by john clsads (@john_clsads_d33c41b94a71b).</description>
    <link>https://springbuilders.dev/john_clsads_d33c41b94a71b</link>
    <image>
      <url>https://springbuilders.dev/images/EVdywpGIgOx7TGaKfGNDgu632YiLE2gaeZ5aPecbWFM/rs:fill:90:90/g:sm/mb:500000/ar:1/aHR0cHM6Ly9zcHJp/bmdidWlsZGVycy5k/ZXYvdXBsb2Fkcy91/c2VyL3Byb2ZpbGVf/aW1hZ2UvOTkzMy9j/YzYzNjlhNC1jNzc1/LTQ4YWQtYjkyMS03/YmYyZGY5OWI3MTUu/cG5n</url>
      <title>Spring Builders: john clsads</title>
      <link>https://springbuilders.dev/john_clsads_d33c41b94a71b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://springbuilders.dev/feed/john_clsads_d33c41b94a71b"/>
    <language>en</language>
    <item>
      <title>Handling Interrupted AI Image Requests Without Starting Over</title>
      <dc:creator>john clsads</dc:creator>
      <pubDate>Wed, 02 Dec 2026 14:51:41 +0000</pubDate>
      <link>https://springbuilders.dev/john_clsads_d33c41b94a71b/handling-interrupted-ai-image-requests-without-starting-over-555g</link>
      <guid>https://springbuilders.dev/john_clsads_d33c41b94a71b/handling-interrupted-ai-image-requests-without-starting-over-555g</guid>
      <description>&lt;p&gt;A user requests several AI kitchen concepts. One image finishes, another is still generating, and the browser loses contact with the application. The next interaction matters: does the interface recover the existing work, or encourage the user to submit the entire request again?&lt;/p&gt;

&lt;p&gt;AI Kitchen Design is an online tool for exploring kitchen styles, colors, and materials from a room photo. Its task workflow helps users &lt;a href="https://ai-kitchen-design.io/"&gt;track and recover kitchen design requests&lt;/a&gt; while keeping completed concepts available for review. The implementation offers a concrete example of managing uncertainty around an external image-generation service.&lt;/p&gt;

&lt;p&gt;This article comes from the AI Kitchen Design team and was prepared with AI assistance. It describes the product's TypeScript implementation; the recovery boundaries are also relevant when designing a Spring service around an external provider. The discussion is based on source review, with no reliability benchmark or failure-injection measurements reported.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does a timed-out AI request actually tell you?
&lt;/h2&gt;

&lt;p&gt;A timeout tells the application that a response was not received within the expected interval. The remote operation may still have been accepted. Recovery therefore needs evidence about the existing request before deciding whether another submission is appropriate.&lt;/p&gt;

&lt;p&gt;There are two useful boundaries to distinguish. The browser may lose the application's response after a batch was created. Separately, the application may lose the provider's response after dispatching an image job. The first can be investigated using the application request identifier; the second needs provider confirmation or reconciliation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Give the submission an identity before sending it
&lt;/h2&gt;

&lt;p&gt;AI Kitchen Design creates a batch UUID before submitting generation and remembers the pending request. When recovering an interrupted submission, the client first looks up that same batch. If it exists, the interface can resume observing its progress instead of creating another batch.&lt;/p&gt;

&lt;p&gt;The client only proceeds toward resubmission after an authoritative not-found response, and it retains the original UUID. On the server, batch creation checks the identifier and its owner; database conflict handling prevents an existing identifier from creating another batch.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Prepare the request.&lt;/strong&gt; Upload the required images and assign the generation batch its identifier.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remember the pending submission.&lt;/strong&gt; Keep the request identity available before the submission begins.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recover by checking that identity.&lt;/strong&gt; Treat a network error during lookup as continuing uncertainty.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reuse the identity when recovery permits submission.&lt;/strong&gt; Let the server recognize an already-created batch.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The pending marker uses session storage when available, with an in-memory fallback. That helps explain its scope: a marker can support recovery within the current browser session, while the fallback has a shorter lifetime. This mechanism does not establish cross-device recovery or unlimited persistence.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separate preparation failures from uncertain dispatch
&lt;/h2&gt;

&lt;p&gt;The job implementation tracks whether submission has crossed the provider-dispatch boundary. An input-preparation failure can be classified before that boundary. A lost confirmation after dispatch leaves a different question: did the provider accept the job?&lt;/p&gt;

&lt;p&gt;Those situations receive different states and next actions:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Evidence or state&lt;/th&gt;
&lt;th&gt;Meaning for recovery&lt;/th&gt;
&lt;th&gt;Next action&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Confirmed preparation failure&lt;/td&gt;
&lt;td&gt;The request failed before dispatch&lt;/td&gt;
&lt;td&gt;Retain inputs and offer an explicit retry&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Unknown submission outcome&lt;/td&gt;
&lt;td&gt;Provider acceptance is unresolved&lt;/td&gt;
&lt;td&gt;Continue confirmation and avoid automatic resubmission&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Generating&lt;/td&gt;
&lt;td&gt;An accepted job remains in progress&lt;/td&gt;
&lt;td&gt;Keep the existing job visible and refresh its status&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Saving&lt;/td&gt;
&lt;td&gt;The provider produced an image that still needs storing&lt;/td&gt;
&lt;td&gt;Retry the saving stage without requesting another image&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;AI Kitchen Design includes an &lt;em&gt;unknown&lt;/em&gt; state for unresolved submission outcomes. Its interface labels that condition as &lt;em&gt;Confirming progress&lt;/em&gt; and asks the user to wait. A generation loop reaching that state directs attention to the saved task rather than silently generating another candidate.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep successful candidates available during partial failure
&lt;/h2&gt;

&lt;p&gt;A batch can contain candidates in different states. AI Kitchen Design returns per-job status and available results, allowing the interface to display completed designs while other candidates remain unresolved or have failed.&lt;/p&gt;

&lt;p&gt;The task panel also handles partial image-loading failures. It loads available result assets with &lt;code&gt;Promise.allSettled&lt;/code&gt;, forwards the successfully opened designs, and records those successes as seen. An image that could not be opened remains eligible for a later loading attempt.&lt;/p&gt;

&lt;p&gt;That separates three operations: generating an image, saving the image, and loading it into the browser. A problem in the last operation should not automatically restart the first. For the person comparing kitchen concepts, this means an available design can remain useful while the application checks the missing result.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make retry a separate, identifiable command
&lt;/h2&gt;

&lt;p&gt;AI Kitchen Design's retry path selects candidates whose state is confirmed as failed. It also gives the retry command its own request UUID. If a retry response is interrupted, the client checks whether that retry identifier was already accepted before issuing the command again.&lt;/p&gt;

&lt;p&gt;The server records the retry receipt against its owner and batch. Its selection excludes completed and unresolved candidates. This separates recovering the original submission from intentionally attempting a failed candidate again.&lt;/p&gt;

&lt;p&gt;Consider an illustrative three-candidate batch: one design is ready, one has a confirmed failure, and one has an unknown submission outcome. The retry operation addresses the failed candidate. The ready design stays available, while the unknown candidate remains in confirmation. This is a state-handling example, not a measured incident or customer story.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can a Spring service take from this design?
&lt;/h2&gt;

&lt;p&gt;Carry request identity and job state across the boundaries where uncertainty appears. For a Spring application calling an external AI service, the useful design questions concern persistence, dispatch, reconciliation, and the exact operation a retry should repeat.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;At request acceptance:&lt;/strong&gt; Can the client look up the operation after losing the response?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;At provider dispatch:&lt;/strong&gt; Can the service distinguish local preparation from uncertain remote acceptance?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;At result storage:&lt;/strong&gt; Can storing an existing provider result be retried independently?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;At explicit retry:&lt;/strong&gt; Can the service identify the retry command and select only eligible failed work?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are design questions for a Spring implementation to answer with its own persistence and execution model. The example demonstrates why they matter without prescribing a particular Spring configuration or claiming an exactly-once guarantee across an external provider.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Should every timeout enable a retry button?
&lt;/h3&gt;

&lt;p&gt;A retry action should reflect the operation's known state. If provider acceptance is unresolved, first recover or reconcile the existing request. A confirmed failure supports a different action from a missing response.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does retrying require all kitchen designs to be regenerated?
&lt;/h3&gt;

&lt;p&gt;AI Kitchen Design's explicit retry operation targets failed candidates within the batch. Completed results and candidates awaiting confirmation are excluded from that selection.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does a saving error mean image generation failed?
&lt;/h3&gt;

&lt;p&gt;The saving stage can fail after the provider has produced an image. The task interface distinguishes that condition and explains that saving can be retried without asking for another generation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Design the next action around what is known
&lt;/h2&gt;

&lt;p&gt;AI Kitchen Design connects image generation to visible task status and recovery controls. The broader engineering lesson is to preserve useful results, keep uncertain work identifiable, and make each retry repeat a clearly defined operation. That gives users a more informed next step when a long-running request is interrupted.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Build an Idempotent Stop Endpoint for Live Sessions in Spring Boot</title>
      <dc:creator>john clsads</dc:creator>
      <pubDate>Wed, 02 Dec 2026 14:50:37 +0000</pubDate>
      <link>https://springbuilders.dev/john_clsads_d33c41b94a71b/how-to-build-an-idempotent-stop-endpoint-for-live-sessions-in-spring-boot-4g2f</link>
      <guid>https://springbuilders.dev/john_clsads_d33c41b94a71b/how-to-build-an-idempotent-stop-endpoint-for-live-sessions-in-spring-boot-4g2f</guid>
      <description>&lt;h1&gt;
  
  
  How to Build an Idempotent Stop Endpoint for Live Sessions in Spring Boot
&lt;/h1&gt;

&lt;p&gt;A user clicks &lt;strong&gt;Stop&lt;/strong&gt;. The browser closes its preview, the server records the request, and the provider takes a few seconds to confirm shutdown. If the browser retries after losing the first response, the server should return the existing stop state with the original usage cutoff and leave a single shutdown job for the worker.&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://live-face-swap.ai/"&gt;Live Face Swap webcam studio&lt;/a&gt; we build, creators can end the live transformation and continue reviewing takes they already recorded. The server keeps the customer billing cutoff separate from provider shutdown confirmation and final settlement. A stop response can report that shutdown is still pending while the stored command gives the server a way to finish the work after the request ends.&lt;/p&gt;

&lt;p&gt;Here is a Spring Boot and PostgreSQL example inspired by that workflow. It models a session that has already reached &lt;code&gt;RUNNING&lt;/code&gt;. Each application session has an immutable ID and refers to one upstream session for the duration of this example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Give the stop response a precise meaning
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;POST /sessions/{id}/stop&lt;/code&gt; with the owner identity derived from the authenticated principal. Return &lt;code&gt;202 Accepted&lt;/code&gt; with &lt;code&gt;STOPPING&lt;/code&gt; after committing the stop request. Once the worker confirms closure, the same endpoint can return &lt;code&gt;200 OK&lt;/code&gt; with &lt;code&gt;STOPPED&lt;/code&gt;. Include the current state and the stored cutoff in either response. Use an owner-scoped status endpoint so a client can check progress without sending another stop.&lt;/p&gt;

&lt;p&gt;Here, &lt;code&gt;STOPPED&lt;/code&gt; means the upstream session is closed. If your billing process needs a final usage report, keep its settlement status separate. The client can stop showing an active preview while both pieces of server work continue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write the cutoff and shutdown job in one transaction
&lt;/h2&gt;

&lt;p&gt;The example uses &lt;code&gt;JdbcClient&lt;/code&gt; from Spring Framework 6.1 or later. Assume &lt;code&gt;live_session&lt;/code&gt; has a UUID primary key, a text owner ID, a state, and a nullable &lt;code&gt;usage_cutoff_at&lt;/code&gt; column of type &lt;code&gt;timestamptz&lt;/code&gt;. The cutoff is populated when the row enters &lt;code&gt;STOPPING&lt;/code&gt; and stays populated after closure. Give &lt;code&gt;session_stop_job.session_id&lt;/code&gt; a primary key and a foreign key to the session. The insert creates a pending job.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.time.Instant&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.time.OffsetDateTime&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.UUID&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.http.HttpStatus&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.jdbc.core.simple.JdbcClient&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.stereotype.Service&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.transaction.annotation.Transactional&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.server.ResponseStatusException&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SessionStopService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;StopAck&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt; &lt;span class="n"&gt;usageCutoffAt&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;JdbcClient&lt;/span&gt; &lt;span class="n"&gt;jdbc&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;SessionStopService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;JdbcClient&lt;/span&gt; &lt;span class="n"&gt;jdbc&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;jdbc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;jdbc&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Transactional&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;StopAck&lt;/span&gt; &lt;span class="nf"&gt;requestStop&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;UUID&lt;/span&gt; &lt;span class="n"&gt;sessionId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;ownerId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;cutoff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;jdbc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""
                update live_session
                   set state = 'STOPPING',
                       usage_cutoff_at = clock_timestamp()
                 where id = :id and owner_id = :owner and state = 'RUNNING'
                returning usage_cutoff_at
                """&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;param&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sessionId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;param&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"owner"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ownerId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;query&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;rs&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;rs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;OffsetDateTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;toInstant&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;optional&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cutoff&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isPresent&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;jdbc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"insert into session_stop_job (session_id) values (:id)"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;param&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sessionId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;update&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;StopAck&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"STOPPING"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cutoff&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;jdbc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""
                select state, usage_cutoff_at
                  from live_session
                 where id = :id and owner_id = :owner
                """&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;param&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sessionId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;param&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"owner"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ownerId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;query&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;rs&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StopAck&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;rs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"state"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
                &lt;span class="n"&gt;rs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"usage_cutoff_at"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;OffsetDateTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;toInstant&lt;/span&gt;&lt;span class="o"&gt;()))&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;optional&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;orElseThrow&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseStatusException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;NOT_FOUND&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Call this method through the Spring-managed service so the update and job insert share one transaction. A job insert failure rolls back the state change and cutoff. Under PostgreSQL's default Read Committed isolation, a competing stop request waits for the row update and then rechecks the &lt;code&gt;RUNNING&lt;/code&gt; predicate. Once the first request commits, the second skips the update and reads the stored state. The controller can map that state to its HTTP response after the service transaction commits.&lt;/p&gt;

&lt;p&gt;For example, suppose the server stores a cutoff of &lt;code&gt;12:00:03.200&lt;/code&gt;, commits the transaction, and loses the response. A retry at &lt;code&gt;12:00:06.900&lt;/code&gt; returns the same cutoff. If the worker confirms provider closure at &lt;code&gt;12:00:08.100&lt;/code&gt;, it can store the confirmation time separately for reconciliation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finish shutdown after the request ends
&lt;/h2&gt;

&lt;p&gt;Have a worker claim pending jobs with a lease and contact the provider outside the database transaction. After it has evidence of closure, have it verify that it still owns the lease and mark the session stopped and the job completed in one transaction. If the process exits after the provider closes the session but before the database commit, a later worker will see the job again. Reuse a stable provider idempotency key where supported, or use the provider's status API and documented response semantics to establish whether the same session is already closed.&lt;/p&gt;

&lt;p&gt;A lost stop signal is a different case because the server has no queued job to retry. If your session expires on heartbeat silence, have a periodic scan identify candidates and verify expiry again at the point of the stop transition. A heartbeat can arrive between scanning and updating. Rechecking under the session lock or in the guarded update protects a session that has just renewed its lease.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which cases should you test first?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Two simultaneous stops.&lt;/strong&gt; Both responses refer to the same cutoff. The job table has one row for that session.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A lost response after commit.&lt;/strong&gt; The next request reads the same cutoff and reports the current state, which may already be &lt;code&gt;STOPPED&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A provider timeout after receiving the close request.&lt;/strong&gt; The job stays recoverable and the worker checks the provider outcome before marking it complete.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A late heartbeat during an expiry scan.&lt;/strong&gt; The expiry check sees the renewed lease and skips the stop transition.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
