-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
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:
MappingJackson2HttpMessageConverterenriched with aJsonSerializerwhich handles a Java 8Stream;- a custom
ScrollableResultSpliteratorneeded to wrapScrollableResultsinto aStream; OpenSessionInViewInterceptorneeded to keep the Hibernate session open within theMessageConverter;- set
hibernate.connection.release_modetoON_CLOSE; - 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:
- Hibernate4 version of SpringSessionContext.currentSession() does not create a session if TransactionSynchronizationManager does not contain one [SPR-9020] #13659 Hibernate4 version of SpringSessionContext.currentSession() does not create a session if TransactionSynchronizationManager does not contain one
Referenced from: commits 49f3a6b