You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "AHUTwzw (via GitHub)" <gi...@apache.org> on 2025/08/13 05:02:48 UTC
[I] Why doesn't the saveBatch() method clear the cache? Occasionally, it may occur that the previous ID result set has been read. [shardingsphere]
AHUTwzw opened a new issue, #36281:
URL: https://github.com/apache/shardingsphere/issues/36281
ShardingJDBC(version:5.5.2)
Through breakpoints, it was found that when the exception occurs, the `generatedValues` in `ShardingSpherePreparedStatement` is not empty, and the previously cached result is returned, causing the error. In the `executeBatch()` method, only `statements.clear()` is executed in the callback. Neither the `clearPrevious()` method is used before execution as in the `executeUpdate()` and `executeQuery()` methods to clear the cached objects, nor is it cleared in the `finally` block. I don't understand why this is done.
```
@Override
public int[] executeBatch() throws SQLException {
try {
return executeBatchExecutor.executeBatch(usedDatabase, sqlStatementContext, generatedValues, statementOption,
(StatementAddCallback<PreparedStatement>) (statements, parameterSets) -> this.statements.addAll(statements),
this::replaySetParameter,
() -> {
currentBatchGeneratedKeysResultSet = getGeneratedKeys();
statements.clear();
});
// CHECKSTYLE:OFF
} catch (final RuntimeException ex) {
// CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaData);
throw SQLExceptionTransformEngine.toSQLException(ex, usedDatabase.getProtocolType());
} finally {
clearBatch();
}
}
```
```
private void clearPrevious() {
currentResultSet = null;
statements.clear();
parameterSets.clear();
generatedValues.clear();
}
```
```
@Override
public ResultSet executeQuery() throws SQLException {
try {
if (statementsCacheable && !statements.isEmpty()) {
resetParameters();
return statements.iterator().next().executeQuery();
}
clearPrevious();
QueryContext queryContext = createQueryContext();
handleAutoCommit(queryContext.getSqlStatementContext().getSqlStatement());
findGeneratedKey().ifPresent(optional -> generatedValues.addAll(optional.getGeneratedValues()));
currentResultSet =
driverExecutorFacade.executeQuery(usedDatabase, metaData, queryContext, this, columnLabelAndIndexMap, (StatementAddCallback<PreparedStatement>) this::addStatements, this::replay);
if (currentResultSet instanceof ShardingSphereResultSet) {
columnLabelAndIndexMap = ((ShardingSphereResultSet) currentResultSet).getColumnLabelAndIndexMap();
}
return currentResultSet;
// CHECKSTYLE:OFF
} catch (final RuntimeException | SQLException ex) {
// CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaData);
throw SQLExceptionTransformEngine.toSQLException(ex, usedDatabase.getProtocolType());
} finally {
executeBatchExecutor.clear();
clearParameters();
}
}
```
I manually added this line of code, is there any problem with it?
<img width="2122" height="690" alt="Image" src="https://github.com/user-attachments/assets/8fc9692f-533b-43b1-aedf-a9b2c1d857fd" />
--
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: notifications-unsubscribe@shardingsphere.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Why doesn't the saveBatch() method clear the cache? Occasionally, it may occur that the previous ID result set has been read. [shardingsphere]
Posted by "terrymanu (via GitHub)" <gi...@apache.org>.
terrymanu closed issue #36281: Why doesn't the saveBatch() method clear the cache? Occasionally, it may occur that the previous ID result set has been read.
URL: https://github.com/apache/shardingsphere/issues/36281
--
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: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Why doesn't the saveBatch() method clear the cache? Occasionally, it may occur that the previous ID result set has been read. [shardingsphere]
Posted by "aviralgarg05 (via GitHub)" <gi...@apache.org>.
aviralgarg05 commented on issue #36281:
URL: https://github.com/apache/shardingsphere/issues/36281#issuecomment-3938485730
I would like to solve the issue, please assign me
--
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: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org