gmunozfe commented on code in PR #3784:
URL:
https://github.com/apache/incubator-kie-kogito-runtimes/pull/3784#discussion_r2044858942
##########
addons/common/persistence/jdbc/src/main/java/org/kie/kogito/persistence/jdbc/GenericRepository.java:
##########
@@ -160,74 +160,33 @@ Optional<Record> findByBusinessKey(String processId,
String processVersion, Stri
}
}
- private static class CloseableWrapper implements Runnable {
-
- private Deque<AutoCloseable> wrapped = new ArrayDeque<>();
-
- public <T extends AutoCloseable> T nest(T c) {
- wrapped.addFirst(c);
- return c;
- }
-
- @Override
- public void run() {
- try {
- close();
- } catch (Exception ex) {
- throw new RuntimeException("Error closing resources", ex);
- }
- }
-
- public void close() throws Exception {
- Exception exception = null;
- for (AutoCloseable wrap : wrapped) {
- try {
- wrap.close();
- } catch (Exception ex) {
- if (exception != null) {
- ex.addSuppressed(exception);
- }
- exception = ex;
- }
- }
- if (exception != null) {
- throw exception;
- }
- }
- }
-
@Override
Stream<Record> findAllInternal(String processId, String processVersion) {
- CloseableWrapper close = new CloseableWrapper();
- try {
- Connection connection = close.nest(dataSource.getConnection());
- PreparedStatement statement =
close.nest(connection.prepareStatement(sqlIncludingVersion(FIND_ALL,
processVersion)));
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement statement =
connection.prepareStatement(sqlIncludingVersion(FIND_ALL, processVersion))) {
statement.setString(1, processId);
if (processVersion != null) {
statement.setString(2, processVersion);
}
- ResultSet resultSet = close.nest(statement.executeQuery());
- return StreamSupport.stream(new
Spliterators.AbstractSpliterator<Record>(
- Long.MAX_VALUE, Spliterator.ORDERED) {
+ List<Record> record = new ArrayList<>();
+ try (ResultSet resultSet = statement.executeQuery()) {
+ while (resultSet.next()) {
+ record.add(from(resultSet));
+ }
+ }
+ return StreamSupport.stream(new
Spliterators.AbstractSpliterator<Record>(Long.MAX_VALUE, Spliterator.ORDERED) {
+ int idx = 0;
+
@Override
public boolean tryAdvance(Consumer<? super Record> action) {
- try {
- boolean hasNext = resultSet.next();
- if (hasNext) {
- action.accept(from(resultSet));
- }
- return hasNext;
- } catch (SQLException e) {
- throw uncheckedException(e, "Error finding all process
instances, for processId %s", processId);
+ if (idx < record.size()) {
+ action.accept(record.get(idx++));
+ return true;
}
+ return false;
}
- }, false).onClose(close);
+ }, false);
} catch (SQLException e) {
- try {
- close.close();
- } catch (Exception ex) {
- e.addSuppressed(ex);
- }
throw uncheckedException(e, "Error finding all process instances,
for processId %s", processId);
Review Comment:
You could consider also logging the `processVersion` in the exception
message to enrich the info about the error
##########
addons/common/persistence/jdbc/src/main/java/org/kie/kogito/persistence/jdbc/GenericRepository.java:
##########
@@ -160,74 +160,33 @@ Optional<Record> findByBusinessKey(String processId,
String processVersion, Stri
}
}
- private static class CloseableWrapper implements Runnable {
-
- private Deque<AutoCloseable> wrapped = new ArrayDeque<>();
-
- public <T extends AutoCloseable> T nest(T c) {
- wrapped.addFirst(c);
- return c;
- }
-
- @Override
- public void run() {
- try {
- close();
- } catch (Exception ex) {
- throw new RuntimeException("Error closing resources", ex);
- }
- }
-
- public void close() throws Exception {
- Exception exception = null;
- for (AutoCloseable wrap : wrapped) {
- try {
- wrap.close();
- } catch (Exception ex) {
- if (exception != null) {
- ex.addSuppressed(exception);
- }
- exception = ex;
- }
- }
- if (exception != null) {
- throw exception;
- }
- }
- }
-
@Override
Stream<Record> findAllInternal(String processId, String processVersion) {
- CloseableWrapper close = new CloseableWrapper();
- try {
- Connection connection = close.nest(dataSource.getConnection());
- PreparedStatement statement =
close.nest(connection.prepareStatement(sqlIncludingVersion(FIND_ALL,
processVersion)));
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement statement =
connection.prepareStatement(sqlIncludingVersion(FIND_ALL, processVersion))) {
statement.setString(1, processId);
if (processVersion != null) {
statement.setString(2, processVersion);
}
- ResultSet resultSet = close.nest(statement.executeQuery());
- return StreamSupport.stream(new
Spliterators.AbstractSpliterator<Record>(
- Long.MAX_VALUE, Spliterator.ORDERED) {
+ List<Record> record = new ArrayList<>();
+ try (ResultSet resultSet = statement.executeQuery()) {
+ while (resultSet.next()) {
+ record.add(from(resultSet));
+ }
+ }
+ return StreamSupport.stream(new
Spliterators.AbstractSpliterator<Record>(Long.MAX_VALUE, Spliterator.ORDERED) {
Review Comment:
not sure why is needed to overwrite `tryAdvance` method here, passing a
List<Record> that already has ordering and a known size.
Why don't directly `return record.stream();`
##########
addons/common/persistence/jdbc/src/main/java/org/kie/kogito/persistence/jdbc/GenericRepository.java:
##########
@@ -160,74 +160,33 @@ Optional<Record> findByBusinessKey(String processId,
String processVersion, Stri
}
}
- private static class CloseableWrapper implements Runnable {
-
- private Deque<AutoCloseable> wrapped = new ArrayDeque<>();
-
- public <T extends AutoCloseable> T nest(T c) {
- wrapped.addFirst(c);
- return c;
- }
-
- @Override
- public void run() {
- try {
- close();
- } catch (Exception ex) {
- throw new RuntimeException("Error closing resources", ex);
- }
- }
-
- public void close() throws Exception {
- Exception exception = null;
- for (AutoCloseable wrap : wrapped) {
- try {
- wrap.close();
- } catch (Exception ex) {
- if (exception != null) {
- ex.addSuppressed(exception);
- }
- exception = ex;
- }
- }
- if (exception != null) {
- throw exception;
- }
- }
- }
-
@Override
Stream<Record> findAllInternal(String processId, String processVersion) {
- CloseableWrapper close = new CloseableWrapper();
- try {
- Connection connection = close.nest(dataSource.getConnection());
- PreparedStatement statement =
close.nest(connection.prepareStatement(sqlIncludingVersion(FIND_ALL,
processVersion)));
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement statement =
connection.prepareStatement(sqlIncludingVersion(FIND_ALL, processVersion))) {
statement.setString(1, processId);
if (processVersion != null) {
statement.setString(2, processVersion);
}
- ResultSet resultSet = close.nest(statement.executeQuery());
- return StreamSupport.stream(new
Spliterators.AbstractSpliterator<Record>(
- Long.MAX_VALUE, Spliterator.ORDERED) {
+ List<Record> record = new ArrayList<>();
Review Comment:
Just a minor naming comment, use "_records_" in plural as it's an arraylist,
but up to you
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]