Skip to content

Commit a86b7e4

Browse files
gavinkingmbellade
authored andcommitted
HHH-19522 report failed optimistic lock checking for upsert()
- also fix the problem for Oracle
1 parent 0edd307 commit a86b7e4

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

hibernate-core/src/main/java/org/hibernate/sql/model/jdbc/DeleteOrUpsertOperation.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import org.hibernate.engine.jdbc.mutation.internal.PreparedStatementGroupSingleTable;
2121
import org.hibernate.engine.jdbc.mutation.spi.Binding;
2222
import org.hibernate.engine.jdbc.mutation.spi.BindingGroup;
23+
import org.hibernate.engine.jdbc.spi.JdbcServices;
2324
import org.hibernate.engine.spi.SharedSessionContractImplementor;
25+
import org.hibernate.jdbc.Expectation;
2426
import org.hibernate.persister.entity.mutation.EntityMutationTarget;
2527
import org.hibernate.persister.entity.mutation.EntityTableMapping;
2628
import org.hibernate.persister.entity.mutation.UpdateValuesAnalysis;
@@ -46,6 +48,7 @@ public class DeleteOrUpsertOperation implements SelfExecutingUpdateOperation {
4648

4749
private final OptionalTableUpdate optionalTableUpdate;
4850

51+
private final Expectation expectation = new Expectation.RowCount();
4952

5053
public DeleteOrUpsertOperation(
5154
EntityMutationTarget mutationTarget,
@@ -118,7 +121,8 @@ private void performDelete(JdbcValueBindings jdbcValueBindings, SharedSessionCon
118121

119122
try {
120123
final PreparedStatement upsertDeleteStatement = statementDetails.resolveStatement();
121-
session.getJdbcServices().getSqlStatementLogger().logStatement( statementDetails.getSqlString() );
124+
final JdbcServices jdbcServices = session.getJdbcServices();
125+
jdbcServices.getSqlStatementLogger().logStatement( statementDetails.getSqlString() );
122126

123127
bindDeleteKeyValues(
124128
jdbcValueBindings,
@@ -130,6 +134,16 @@ private void performDelete(JdbcValueBindings jdbcValueBindings, SharedSessionCon
130134
final int rowCount = session.getJdbcCoordinator().getResultSetReturn()
131135
.executeUpdate( upsertDeleteStatement, statementDetails.getSqlString() );
132136
MODEL_MUTATION_LOGGER.tracef( "`%s` rows upsert-deleted from `%s`", rowCount, tableMapping.getTableName() );
137+
try {
138+
expectation.verifyOutcome( rowCount, upsertDeleteStatement, -1, statementDetails.getSqlString() );
139+
}
140+
catch (SQLException e) {
141+
throw jdbcServices.getSqlExceptionHelper().convert(
142+
e,
143+
"Unable to verify outcome for upsert delete",
144+
statementDetails.getSqlString()
145+
);
146+
}
133147
}
134148
finally {
135149
statementDetails.releaseStatement( session );
@@ -197,14 +211,24 @@ private void performUpsert(JdbcValueBindings jdbcValueBindings, SharedSessionCon
197211

198212
try {
199213
final PreparedStatement updateStatement = statementDetails.resolveStatement();
200-
session.getJdbcServices().getSqlStatementLogger().logStatement( statementDetails.getSqlString() );
201-
214+
final JdbcServices jdbcServices = session.getJdbcServices();
215+
jdbcServices.getSqlStatementLogger().logStatement( statementDetails.getSqlString() );
202216
jdbcValueBindings.beforeStatement( statementDetails );
203217

204218
final int rowCount = session.getJdbcCoordinator().getResultSetReturn()
205219
.executeUpdate( updateStatement, statementDetails.getSqlString() );
206220

207221
MODEL_MUTATION_LOGGER.tracef( "`%s` rows upserted into `%s`", rowCount, tableMapping.getTableName() );
222+
try {
223+
expectation.verifyOutcome( rowCount, updateStatement, -1, statementDetails.getSqlString() );
224+
}
225+
catch (SQLException e) {
226+
throw jdbcServices.getSqlExceptionHelper().convert(
227+
e,
228+
"Unable to verify outcome for upsert",
229+
statementDetails.getSqlString()
230+
);
231+
}
208232
}
209233
finally {
210234
statementDetails.releaseStatement( session );

0 commit comments

Comments
 (0)