Skip to content

Commit b476779

Browse files
authored
JDBC: SERIALIZABLE/EntityNotFoundException (#2219)
With serializable isolation level, if either the primary-key or the check-constraint are violated, the lookup of the conflicting entity is not guaranteed to yield a non-null result (transaction start matters). This changes works around this situation w/ serializable isolation.
1 parent 355adae commit b476779

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcBasePersistenceImpl.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,17 @@ private void persistEntity(
183183
entity.getParentId(),
184184
entity.getTypeCode(),
185185
entity.getName());
186-
throw new EntityAlreadyExistsException(existingEntity, e);
187-
} else {
188-
throw new RuntimeException(
189-
String.format("Failed to write entity due to %s", e.getMessage()), e);
186+
// This happens in two scenarios:
187+
// 1. PRIMARY KEY violated
188+
// 2. UNIQUE CONSTRAINT on (realm_id, catalog_id, parent_id, type_code, name) violated
189+
// With SERIALIZABLE isolation, the conflicting entity may _not_ be visible and
190+
// existingEntity can be null, which would cause an NPE in
191+
// EntityAlreadyExistsException.message().
192+
throw new EntityAlreadyExistsException(
193+
existingEntity != null ? existingEntity : entity, e);
190194
}
195+
throw new RuntimeException(
196+
String.format("Failed to write entity due to %s", e.getMessage()), e);
191197
}
192198
} else {
193199
Map<String, Object> params =

0 commit comments

Comments
 (0)