Skip to content

Commit 577211c

Browse files
committed
Remove transaction from atomic writes
1 parent e4eabb5 commit 577211c

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

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

+15-8
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ public void writeEntity(
9393
boolean nameOrParentChanged,
9494
PolarisBaseEntity originalEntity) {
9595
try {
96-
datasourceOperations.runWithinTransaction(
97-
statement -> {
98-
persistEntity(callCtx, entity, originalEntity, statement);
99-
return true;
100-
});
96+
persistEntity(callCtx, entity, originalEntity, datasourceOperations);
10197
} catch (SQLException e) {
10298
throw new RuntimeException("Error persisting entity", e);
10399
}
@@ -143,12 +139,12 @@ private void persistEntity(
143139
@Nonnull PolarisCallContext callCtx,
144140
@Nonnull PolarisBaseEntity entity,
145141
PolarisBaseEntity originalEntity,
146-
Statement statement)
142+
Object executor)
147143
throws SQLException {
148144
ModelEntity modelEntity = ModelEntity.fromEntity(entity);
149145
if (originalEntity == null) {
150146
try {
151-
statement.executeUpdate(generateInsertQuery(modelEntity, realmId));
147+
execute(executor, generateInsertQuery(modelEntity, realmId));
152148
} catch (SQLException e) {
153149
if (datasourceOperations.isConstraintViolation(e)) {
154150
PolarisBaseEntity existingEntity =
@@ -176,7 +172,7 @@ private void persistEntity(
176172
"realm_id",
177173
realmId);
178174
try {
179-
int rowsUpdated = statement.executeUpdate(generateUpdateQuery(modelEntity, params));
175+
int rowsUpdated = execute(executor, generateUpdateQuery(modelEntity, params));
180176
if (rowsUpdated == 0) {
181177
throw new RetryOnConcurrencyException(
182178
"Entity '%s' id '%s' concurrently modified; expected version %s",
@@ -189,6 +185,17 @@ private void persistEntity(
189185
}
190186
}
191187

188+
private int execute(Object executor, String query) throws SQLException {
189+
if (executor instanceof Statement) {
190+
// used for running in transaction
191+
return ((Statement) executor).executeUpdate(query);
192+
} else if (executor instanceof DatasourceOperations) {
193+
return ((DatasourceOperations) executor).executeUpdate(query);
194+
} else {
195+
throw new IllegalArgumentException("Unsupported executor: " + executor);
196+
}
197+
}
198+
192199
@Override
193200
public void writeToGrantRecords(
194201
@Nonnull PolarisCallContext callCtx, @Nonnull PolarisGrantRecord grantRec) {

0 commit comments

Comments
 (0)