singhpk234 commented on code in PR #1371:
URL: https://github.com/apache/polaris/pull/1371#discussion_r2060991246
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -127,88 +175,13 @@ public void writeEntity(
}
}
- @Override
- public void writeEntities(
- @Nonnull PolarisCallContext callCtx,
- @Nonnull List<PolarisBaseEntity> entities,
- List<PolarisBaseEntity> originalEntities) {
- try {
- datasourceOperations.runWithinTransaction(
- statement -> {
- for (int i = 0; i < entities.size(); i++) {
- PolarisBaseEntity entity = entities.get(i);
- ModelEntity modelEntity = ModelEntity.fromEntity(entity);
-
- // first, check if the entity has already been created, in which
case we will simply
- // return it.
- PolarisBaseEntity entityFound =
- lookupEntity(
- callCtx, entity.getCatalogId(), entity.getId(),
entity.getTypeCode());
- if (entityFound != null) {
- // probably the client retried, simply return it
- // TODO: Check correctness of returning entityFound vs entity
here. It may have
- // already been updated after the creation.
- continue;
- }
- // lookup by name
- EntityNameLookupRecord exists =
- lookupEntityIdAndSubTypeByName(
- callCtx,
- entity.getCatalogId(),
- entity.getParentId(),
- entity.getTypeCode(),
- entity.getName());
- if (exists != null) {
- throw new EntityAlreadyExistsException(entity);
- }
- String query;
- if (originalEntities == null || originalEntities.get(i) == null)
{
- try {
- query = generateInsertQuery(modelEntity, realmId);
- statement.executeUpdate(query);
- } catch (SQLException e) {
- if ((datasourceOperations.isConstraintViolation(e)
- || datasourceOperations.isAlreadyExistsException(e))) {
- throw new EntityAlreadyExistsException(entity, e);
- } else {
- throw new RuntimeException(
- String.format("Failed to write entity due to %s",
e.getMessage()), e);
- }
- }
- } else {
- Map<String, Object> params =
- Map.of(
- "id",
- originalEntities.get(i).getId(),
- "catalog_id",
- originalEntities.get(i).getCatalogId(),
- "entity_version",
- originalEntities.get(i).getEntityVersion(),
- "realm_id",
- realmId);
- query = generateUpdateQuery(modelEntity, params);
- try {
- int rowsUpdated = statement.executeUpdate(query);
- if (rowsUpdated == 0) {
- throw new RetryOnConcurrencyException(
- "Entity '%s' id '%s' concurrently modified; expected
version %s",
- entity.getName(),
- entity.getId(),
- originalEntities.get(i).getEntityVersion());
- }
- } catch (SQLException e) {
- throw new RuntimeException(
- String.format("Failed to write entity due to %s",
e.getMessage()), e);
- }
- }
- }
- return true;
- });
- } catch (SQLException e) {
- throw new RuntimeException(
- String.format(
- "Error executing the transaction for writing entities due to
%s", e.getMessage()),
- e);
+ private int execute(Object executor, String query) throws SQLException {
+ if (executor instanceof Statement) {
+ return ((Statement) executor).executeUpdate(query);
+ } else if (executor instanceof DatasourceOperations) {
+ return ((DatasourceOperations) executor).executeUpdate(query);
+ } else {
Review Comment:
I think perfer the current way, rather than running transaction within
another transaction, though i do agree may be i need to think how can i model
the transaction and connection sharing
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]