Skip to content

HibernateTransactionManager should allow holdability of ResultSet into the View layer [SPR-12349] #16954

@spring-projects-issues

Description

@spring-projects-issues

Marko Topolnik opened SPR-12349 and commented

I would like an old topic to be reconsidered in the light of new developments. One of the issues dealing with the topic was #6715 and it was resolved such that the Hibernate session releases its JDBC connection upon the completion of a transaction.

In the context of a REST Web Service, I want to use the standard MVC mechanism where a MessageConverter is dispatched to handle an object returned from a @Controller, but I want that mechanism to support Java 8 Streams and let the MesageConverter consume the stream while producing the response. The stream source would be Hibernate's ScrollableResults. Note the following advantages:

  • O(1) space complexity instead of O(N);
  • transaction semantics stay intact: the transaction is committed when exiting the service layer;
  • all the business logic stays within the service layer. The returned Stream wraps the logic needed to transform the SQL results into response DTOs;
  • the transformation can be automatically parallelized with no thread safety issues on the JDBC level (Spliterator's programming model is single-threaded).

I have set up the following:

  1. MappingJackson2HttpMessageConverter enriched with a JsonSerializer which handles a Java 8 Stream;
  2. a custom ScrollableResultSpliterator needed to wrap ScrollableResults into a Stream;
  3. OpenSessionInViewInterceptor needed to keep the Hibernate session open within the MessageConverter;
  4. set hibernate.connection.release_mode to ON_CLOSE;
  5. ensure that the JDBC connection has the necessary ResultSet holdability: con.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT)

The final stumbling point I have encountered is the policy used by HibernateTransactionManager on transaction commit: unless the underlying session is "Hibernate-managed", it will disconnect() it, closing my cursor along with everything else. Such a policy is useful in some special scenarios, specifically "conversation-scoped sessions", which are far removed from my requirements.

Since I regard my approach as "the right way" to generate ResultSet-backed REST responses, and since the Streams API makes this approach very convenient, I would like to have support for it from Spring.


Affects: 4.1.1

Reference URL: http://stackoverflow.com/questions/26324112/trouble-using-scrollableresults-backed-stream-as-return-type-in-spring-mvc

Issue Links:

Referenced from: commits 49f3a6b

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions