Identifying resource-using streams

Marko Topolnik marko.topolnik at gmail.com
Mon Apr 21 20:54:11 UTC 2014


With lazy streams, the scope of validity of a resource stops nicely following the scope of a block of code. I have a specific example from some recent work:

- in a Spring-based application the HTTP request body is wrapped into a stream of domain objects. This happens in a framework layer;

- the business logic layer receives the stream and adds a mapping stage to it. This stage is attached to a database result set;

- the stream goes back to the framework layer, where the output of the above stage is serialized into the response.

Both the request body stream and the database connection can be released only after the complete response has been generated, which is in awkward opposition to the classical case where the transaction would normally commit immediately upon leaving the business logic layer. A number of hacks and interventions is needed to prolong the result set's lifecycle. Error handling is even messier and I am still not clear whether in certain cases there is a resource leak.

In the above case, a single try-with-resources cannot possibly cover the entire lifecycle of a resource and as the stream changes hands there are many possibilities of errors happening which divert the control flow from the code path where the stream is closed.

So I believe this processing pattern is completely new territory and will take all-around support from the framework to work smoothly.


-marko



More information about the lambda-dev mailing list