Skip to content

Commit

Permalink
Fix #31372 problem (#31377)
Browse files Browse the repository at this point in the history
* Fix #31372 problem

* Fix #31372 problem

* Fix #31372 problem
  • Loading branch information
wmouren committed May 26, 2024
1 parent 1103aa6 commit ed179a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,17 @@ public List<String> getChildrenKeys(final String key) {

@Override
public boolean isExisted(final String key) {
return !Strings.isNullOrEmpty(query(key));
try (
Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(repositorySQL.getSelectByKeySQL())) {
preparedStatement.setString(1, key);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
return resultSet.next();
}
} catch (final SQLException ex) {
log.error("Check existence of {} data by key: {} failed", getType(), key, ex);
}
return Boolean.FALSE;
}

@Override
Expand All @@ -138,8 +148,7 @@ public void persist(final String key, final String value) {
// Create key level directory recursively.
for (int i = 0; i < paths.length - 1; i++) {
String tempKey = tempPrefix + SEPARATOR + paths[i];
String tempKeyVal = query(tempKey);
if (Strings.isNullOrEmpty(tempKeyVal)) {
if (!isExisted(tempKey)) {
insert(tempKey, "", parent);
}
tempPrefix = tempKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ void assertPersistWithUpdateForSimpleKeys() throws SQLException {
when(mockJdbcConnection.prepareStatement(repositorySQL.getUpdateSQL())).thenReturn(mockPreparedStatementForPersist);
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
when(mockResultSet.next()).thenReturn(true);
when(mockResultSet.getString("value")).thenReturn("oldValue");
repository.persist(key, value);
verify(mockPreparedStatement).setString(1, key);
verify(mockPreparedStatementForPersist).setString(eq(1), anyString());
Expand Down Expand Up @@ -199,7 +198,6 @@ void assertPersistFailureDuringUpdate() throws SQLException {
when(mockJdbcConnection.prepareStatement(repositorySQL.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
when(mockResultSet.next()).thenReturn(true);
when(mockResultSet.getString("value")).thenReturn("oldValue");
when(mockJdbcConnection.prepareStatement(repositorySQL.getUpdateSQL())).thenReturn(mockPreparedStatement);
repository.persist(key, "value");
verify(mockPreparedStatementForPersist, times(0)).executeUpdate();
Expand Down

0 comments on commit ed179a4

Please sign in to comment.