Skip to content

Commit 2bfa6df

Browse files
committed
HHH-17887 Release mode After Statment with deferred result set access does not work
1 parent 638b5c7 commit 2bfa6df

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,72 +182,77 @@ 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-
);
193-
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-
}
185+
try {
186+
//noinspection unchecked
187+
final RowReader<Object> rowReader = (RowReader<Object>) ResultsHelper.createRowReader(
188+
executionContext,
189+
null,
190+
RowTransformerStandardImpl.INSTANCE,
191+
null,
192+
jdbcValues
193+
);
202194

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

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

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

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

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

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)