spring - How to rewrite jdbcTemplate query using JDK8 features? -
here's code. let's there many large images in db want keep 1 in memory @ time. how write using jdk 8 features lambda , streams?
i started using kept failing ( https://spring.io/guides/gs/relational-data-access/ , www.jooq.org/java-8-and-sql) , using resultsetextractor working intendent, there way without resultsetextractor?
jdbctemplate.query( "select id,image,mimetype images", new resultsetextractor(){ @override public list extractdata(resultset rs) throws sqlexception, dataaccessexception { while(rs.next()){ createthumbnail( new imageholder(rs.getint("id"), rs.getbytes("image"), rs.getstring("mimetype") )); } return null; } } );
this nice looking stream/lambda version works holds many things in memory , gives oome sooner or later.
jdbctemplate.query( "select id,image,mimetype images", (rs,rownum)->new imageholder(rs.getint("id"), rs.getbytes("image"),rs.getstring("mimetype")) ).stream().foreach( imageholder -> createthumbnail(imageholder) ) );
this loads rows before starts "streaming" them.
a solution move generation of thumbnail in first lambda, translating directly first example.
jdbctemplate.query( "select id,image,mimetype images", (rs,rownum)-> { createthumbnail(new imageholder( rs.getint("id"), rs.getbytes("image"), rs.getstring("mimetype") )); }) )
to have structure similar 1 tried achieve query method should return stream don't have collect before streaming.
Comments
Post a Comment