Reserve SQLite writer before reading subscription event receipts

This commit is contained in:
root
2026-10-21 10:11:57 +08:00
parent d6eb25a0f6
commit 788fd1e7e2
3 changed files with 29 additions and 0 deletions
@@ -7,6 +7,10 @@ import org.apache.ibatis.annotations.*;
@Mapper
public interface SubscriptionEventMapper {
/** Reserve SQLite's writer before the receipt read; a deferred read-to-write upgrade can fail immediately. */
@Update("update subscription_event_receipt set ingested_at=ingested_at where 0")
void reserveWriter();
@Select("select fingerprint from subscription_event_receipt where event_id=#{eventId}")
String fingerprint(String eventId);
@@ -123,6 +123,7 @@ public class SubscriptionEventService {
try {
// Return acknowledgements only after execute has returned and the entire transaction committed.
List<String> acknowledged = transactions.execute(status -> {
events.reserveWriter();
List<String> ids = new ArrayList<>();
for (SubscriptionEvent event : batch) if (persist(event, "standby", null)) ids.add(event.getEventId());
return ids;
@@ -133,6 +134,7 @@ public class SubscriptionEventService {
public void recordPrimary(SubscriptionEvent event, SubBind binding) {
transactions.executeWithoutResult(status -> {
events.reserveWriter();
if (!persist(event, "primary", binding)) throw new IllegalStateException("subscription user unavailable");
});
}