Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions snd/src/org/labkey/snd/SNDManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,24 @@ public boolean eventExists(Container c, User u, int eventId)
return selector.exists();
}

/**
* Returns the EventId for an existing event that was saved with the given ObjectId, or null if not found.
* Used by saveEvent for idempotent retries: when a client-generated ObjectId is provided and already
* exists in the DB, the event was already committed and should not be inserted again.
*/
public Integer getEventIdByObjectId(Container c, User u, String objectId)
{
UserSchema schema = getSndUserSchema(c, u);

SQLFragment sql = new SQLFragment("SELECT EventId FROM ");
sql.append(schema.getTable(SNDSchema.EVENTS_TABLE_NAME), "ev");
sql.append(" WHERE ObjectId = ?");
sql.add(objectId);
SqlSelector selector = new SqlSelector(schema.getDbSchema(), sql);

return selector.getObject(Integer.class);
}

/**
* Get a project ObjectId given a projectId and revision in the format projectId|rev (ex. 61|1).
*/
Expand Down
18 changes: 18 additions & 0 deletions snd/src/org/labkey/snd/SNDServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,24 @@ public Event saveEvent(Container c, User u, Event event, boolean validateOnly)
{
try (DbScope.Transaction tx = SNDSchema.getInstance().getSchema().getScope().ensureTransaction(lock))
{
// Idempotency check: if a client-generated objectId is present, look up whether
// this event was already committed (e.g. device lost the response after the server
// wrote the row). If found, override the auto-generated eventId with the existing one
// so the request falls through to the update path instead of creating a duplicate.
// Note: the Event constructor always auto-assigns an eventId via SNDSequencer even
// when the caller passes null, so checking event.getEventId() == null would never
// be true here and the idempotency check would never run. We guard only on objectId.
// For update requests the client omits objectId, so the controller supplies a random
// GUID that will never match an existing row — making this lookup a safe no-op.
if (event.getObjectId() != null)
{
Integer existingEventId = SNDManager.get().getEventIdByObjectId(c, u, event.getObjectId());
if (existingEventId != null)
{
event.setEventId(existingEventId);
}
}

if (event.getEventId() != null && SNDManager.get().eventExists(c, u, event.getEventId()))
{
if ((event.getEventData() == null || event.getEventData().isEmpty()) &&
Expand Down