Skip to content

Commit e95eaf6

Browse files
committed
HHH-17887 Release mode After Statment with deferred result set access does not work
1 parent 189de9d commit e95eaf6

File tree

2 files changed

+66
-67
lines changed

2 files changed

+66
-67
lines changed

hibernate-core/src/main/java/org/hibernate/result/internal/OutputsImpl.java

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -182,74 +182,79 @@ protected List<Object> extractResults(ResultSet resultSet) {
182182
executionContext
183183
);
184184

185-
//noinspection unchecked
186-
final RowReader<Object> rowReader = (RowReader<Object>) ResultsHelper.createRowReader(
187-
executionContext,
188-
null,
189-
RowTransformerStandardImpl.INSTANCE,
190-
null,
191-
jdbcValues
192-
);
185+
try {
193186

194-
/*
195-
* Processing options effectively are only used for entity loading. Here we don't need these values.
196-
*/
197-
final JdbcValuesSourceProcessingOptions processingOptions = new JdbcValuesSourceProcessingOptions() {
198-
@Override
199-
public Object getEffectiveOptionalObject() {
200-
return null;
201-
}
187+
//noinspection unchecked
188+
final RowReader<Object> rowReader = (RowReader<Object>) ResultsHelper.createRowReader(
189+
executionContext,
190+
null,
191+
RowTransformerStandardImpl.INSTANCE,
192+
null,
193+
jdbcValues
194+
);
202195

203-
@Override
204-
public String getEffectiveOptionalEntityName() {
205-
return null;
206-
}
196+
/*
197+
* Processing options effectively are only used for entity loading. Here we don't need these values.
198+
*/
199+
final JdbcValuesSourceProcessingOptions processingOptions = new JdbcValuesSourceProcessingOptions() {
200+
@Override
201+
public Object getEffectiveOptionalObject() {
202+
return null;
203+
}
207204

208-
@Override
209-
public Serializable getEffectiveOptionalId() {
210-
return null;
211-
}
205+
@Override
206+
public String getEffectiveOptionalEntityName() {
207+
return null;
208+
}
212209

213-
@Override
214-
public boolean shouldReturnProxies() {
215-
return true;
216-
}
217-
};
210+
@Override
211+
public Serializable getEffectiveOptionalId() {
212+
return null;
213+
}
218214

219-
final JdbcValuesSourceProcessingStateStandardImpl jdbcValuesSourceProcessingState =
220-
new JdbcValuesSourceProcessingStateStandardImpl(
215+
@Override
216+
public boolean shouldReturnProxies() {
217+
return true;
218+
}
219+
};
220+
221+
final JdbcValuesSourceProcessingStateStandardImpl jdbcValuesSourceProcessingState =
222+
new JdbcValuesSourceProcessingStateStandardImpl(
223+
executionContext,
224+
processingOptions
225+
);
226+
final ArrayList<Object> results = new ArrayList<>();
227+
try {
228+
final RowProcessingStateStandardImpl rowProcessingState = new RowProcessingStateStandardImpl(
229+
jdbcValuesSourceProcessingState,
221230
executionContext,
222-
processingOptions
231+
rowReader,
232+
jdbcValues
223233
);
224-
final ArrayList<Object> results = new ArrayList<>();
225-
try {
226-
final RowProcessingStateStandardImpl rowProcessingState = new RowProcessingStateStandardImpl(
227-
jdbcValuesSourceProcessingState,
228-
executionContext,
229-
rowReader,
230-
jdbcValues
231-
);
232234

233-
rowReader.getInitializersList().startLoading( rowProcessingState );
235+
rowReader.getInitializersList().startLoading( rowProcessingState );
234236

235-
while ( rowProcessingState.next() ) {
236-
results.add( rowReader.readRow( rowProcessingState, processingOptions ) );
237-
rowProcessingState.finishRowProcessing( true );
237+
while ( rowProcessingState.next() ) {
238+
results.add( rowReader.readRow( rowProcessingState, processingOptions ) );
239+
rowProcessingState.finishRowProcessing( true );
240+
}
241+
if ( resultSetMapping.getNumberOfResultBuilders() == 0
242+
&& procedureCall.isFunctionCall()
243+
&& procedureCall.getFunctionReturn().getJdbcTypeCode() == Types.REF_CURSOR
244+
&& results.size() == 1
245+
&& results.get( 0 ) instanceof ResultSet ) {
246+
// When calling a function that returns a ref_cursor with as table function,
247+
// we have to unnest the ResultSet manually here
248+
return extractResults( (ResultSet) results.get( 0 ) );
249+
}
250+
return results;
238251
}
239-
if ( resultSetMapping.getNumberOfResultBuilders() == 0
240-
&& procedureCall.isFunctionCall()
241-
&& procedureCall.getFunctionReturn().getJdbcTypeCode() == Types.REF_CURSOR
242-
&& results.size() == 1
243-
&& results.get( 0 ) instanceof ResultSet ) {
244-
// When calling a function that returns a ref_cursor with as table function,
245-
// we have to unnest the ResultSet manually here
246-
return extractResults( (ResultSet) results.get( 0 ) );
252+
finally {
253+
rowReader.finishUp( jdbcValuesSourceProcessingState );
254+
jdbcValuesSourceProcessingState.finishUp( results.size() > 1 );
247255
}
248-
return results;
249256
}
250257
finally {
251-
rowReader.finishUp( jdbcValuesSourceProcessingState );
252-
jdbcValuesSourceProcessingState.finishUp( results.size() > 1 );
253258
jdbcValues.finishUp( this.context.getSession() );
254259
}
255260
}

hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/DeferredResultSetAccess.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ private void executeQuery() {
253253

254254
skipRows( resultSet );
255255
logicalConnection.getResourceRegistry().register( resultSet, preparedStatement );
256-
257256
}
258257
catch (SQLException e) {
259258
try {
@@ -267,9 +266,6 @@ private void executeQuery() {
267266
"JDBC exception executing SQL [" + finalSql + "]"
268267
);
269268
}
270-
finally {
271-
logicalConnection.afterStatement();
272-
}
273269
}
274270

275271
private JdbcSessionContext context() {
@@ -324,20 +320,18 @@ protected LockMode determineFollowOnLockMode(LockOptions lockOptions) {
324320

325321
@Override
326322
public void release() {
323+
final LogicalConnectionImplementor logicalConnection = getPersistenceContext().getJdbcCoordinator()
324+
.getLogicalConnection();
327325
if ( resultSet != null ) {
328-
getPersistenceContext().getJdbcCoordinator()
329-
.getLogicalConnection()
330-
.getResourceRegistry()
331-
.release( resultSet, preparedStatement );
326+
logicalConnection.getResourceRegistry().release( resultSet, preparedStatement );
332327
resultSet = null;
333328
}
334329

335330
if ( preparedStatement != null ) {
336-
getPersistenceContext().getJdbcCoordinator()
337-
.getLogicalConnection()
338-
.getResourceRegistry()
339-
.release( preparedStatement );
331+
logicalConnection.getResourceRegistry().release( preparedStatement );
340332
preparedStatement = null;
341333
}
334+
335+
logicalConnection.afterStatement();
342336
}
343337
}

0 commit comments

Comments
 (0)