You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Jeff Schnitzer <je...@infohazard.org> on 2002/05/04 23:41:55 UTC
RE: Overhead creating a Vector of objects to send to VTL ? ( Re:foreach)
Funny. I just wrote something like this. You can use the
ResultSetMapAdapter, or the BeanIterator which creates and populates
JavaBeans using the commons-beanutils.
I haven't actually tested it yet, but have at it anyways. It's pretty
simple.
The only problem is closing the database connection. This is a problem
with any approach that uses the "live" ResultSet... you *must* enclose
the connection creation with a finally() block which calls
connection.close(), otherwise an exception is going to result in a
connection leak (at least, until it gets garbage collected).
This is the reason, I think, that almost all O/R systems suck all the
data out of the ResultSet before returning from the query method.
Jeff Schnitzer
jeff@infohazard.org
> -----Original Message-----
> From: Geir Magnusson Jr. [mailto:geirm@adeptra.com]
> Sent: Saturday, May 04, 2002 1:59 PM
> To: velocity-user@jakarta.apache.org
> Subject: Re: Overhead creating a Vector of objects to send to VTL ?
> ( Re:foreach)
>
> On 5/4/02 4:12 PM, "Stephen Riek" <st...@yahoo.co.uk> wrote:
>
> >
> > I work solely in the Web world. Probably the most common place where
> > one would use #foreach is looping over a list of results from a
query.
> > For example, viewing a catalog, or viewing the results from a search
> > engine. I have never found Velocity lacking, mainly because I've
> > usually handled all the logic in the SQL before I return the list of
> > results to Velocity.
> > Consider sending the results of a SQL query to a velocity template.
> > Each result represents an object (eg. an item in your catalog) for
> > which you have a corresponding Javabean. At the moment I loop over
> > the JDBC ResultSet within the Java code and create a Vector of
> > catalogItem objects, which is sent to the Velocity Template.
> > This contrasts greatly with the JSP way of handling the JDBC
ResultSet
> > directly. However, is my mechanism using too much Object creation
> > (the Vector of Javabeans) which I understand is "expensive" overhead
> > in Java ?I'm trying to optimize performance and scalability so is
this a
> > real concern ?
>
> If you are not accessing everything in the result set, this may be
> wasteful
> (although java is getting efficient).
>
> One solution is to access the data 'just in time'. To do this, make a
> little class that you put the result set into, and then that class
into
> the
> context. It might be a little bit of a pain as you will need to let
> Velocity iterator over something. If you only will iterate over it
once,
> then you can just make it look like an Iterator. I have no idea if
the
> following works, let alone compiles. I am just making this up :)
>
> public class Foo implements java.util.Iterator {
>
> ResultSet myResultSet;
> MyMapper mapper;
>
> public Foo(ResultSet rs)
> {
> myResultSet = rs;
> mapper = new Mapper(myResultSet);
> }
>
> public boolean hasNext() {
>
> return rs.next();
> }
>
> public Object next() {
> return mapper;
> }
> }
>
> public class MyMapper implements Map{
> ResultSet rs;
>
> public MyMapper(ResultSet rs)
> {
> this.rs = rs;
> }
>
> public Object get(String key)
> {
> return rs.getString(key);
> }
>
> ... Rest of Map methods
> }
>
>
> So the idea is that you wrap the result set in something that looks
like a
> java.util.Iterator, so Vel will iterate over it and put that into the
> context. No new objects yet.
>
> Then, each item in the set will be a mapper that also wraps the result
set
> (so no new objects created).
>
> Then, when you use it
>
> #foreach($item in $resultset)
>
> $item.foo
>
> #end
>
> Will only access the result set creating a string for the column
'foo'.
>
>
> >
> > I have never bought into the "most people working in the MVC View
> > are not programmers" argument, which is the basis of Struts and a
> > multitude of frameworks which seem to have been born in ivory
towers.
> > Sure, it's true there are more non-programmers in the View area, but
I
> > wouldn't expect any designer to be able to use VTL. No disrespect to
> > any of them, but the hundreds of designers I've worked with just
> wouldn't
> > want to know and prefer not to have the responsibility. Therefore,
> > it's a non-issue for me. Velocity Rocks.
> >
> > Thank you for any feedback on the initial question.
> > Stephen Riek.
> >
> >
> >
> >
> > ---------------------------------
> > Do You Yahoo!?
> > Get personalised at My Yahoo!.
> >
>
> --
> Geir Magnusson Jr.
> Research & Development, Adeptra Inc.
> geirm@adeptra.com
> +1-203-247-1713
>
>
>
> --
> To unsubscribe, e-mail: <mailto:velocity-user-
> unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:velocity-user-
> help@jakarta.apache.org>
RE: Overhead creating a Vector of objects to send to VTL ? ( Re:foreach)
Posted by Stephen Riek <st...@yahoo.co.uk>.
Thank you very much Jeff and Geir. Thank you.
Stephen Riek.
Jeff Schnitzer <je...@infohazard.org> wrote: Funny. I just wrote something like this. You can use the
ResultSetMapAdapter, or the BeanIterator which creates and populates
JavaBeans using the commons-beanutils.
I haven't actually tested it yet, but have at it anyways. It's pretty
simple.
The only problem is closing the database connection. This is a problem
with any approach that uses the "live" ResultSet... you *must* enclose
the connection creation with a finally() block which calls
connection.close(), otherwise an exception is going to result in a
connection leak (at least, until it gets garbage collected).
This is the reason, I think, that almost all O/R systems suck all the
data out of the ResultSet before returning from the query method.
Jeff Schnitzer
jeff@infohazard.org
> -----Original Message-----
> From: Geir Magnusson Jr. [mailto:geirm@adeptra.com]
> Sent: Saturday, May 04, 2002 1:59 PM
> To: velocity-user@jakarta.apache.org
> Subject: Re: Overhead creating a Vector of objects to send to VTL ?
> ( Re:foreach)
>
> On 5/4/02 4:12 PM, "Stephen Riek" wrote:
>
> >
> > I work solely in the Web world. Probably the most common place where
> > one would use #foreach is looping over a list of results from a
query.
> > For example, viewing a catalog, or viewing the results from a search
> > engine. I have never found Velocity lacking, mainly because I've
> > usually handled all the logic in the SQL before I return the list of
> > results to Velocity.
> > Consider sending the results of a SQL query to a velocity template.
> > Each result represents an object (eg. an item in your catalog) for
> > which you have a corresponding Javabean. At the moment I loop over
> > the JDBC ResultSet within the Java code and create a Vector of
> > catalogItem objects, which is sent to the Velocity Template.
> > This contrasts greatly with the JSP way of handling the JDBC
ResultSet
> > directly. However, is my mechanism using too much Object creation
> > (the Vector of Javabeans) which I understand is "expensive" overhead
> > in Java ?I'm trying to optimize performance and scalability so is
this a
> > real concern ?
>
> If you are not accessing everything in the result set, this may be
> wasteful
> (although java is getting efficient).
>
> One solution is to access the data 'just in time'. To do this, make a
> little class that you put the result set into, and then that class
into
> the
> context. It might be a little bit of a pain as you will need to let
> Velocity iterator over something. If you only will iterate over it
once,
> then you can just make it look like an Iterator. I have no idea if
the
> following works, let alone compiles. I am just making this up :)
>
> public class Foo implements java.util.Iterator {
>
> ResultSet myResultSet;
> MyMapper mapper;
>
> public Foo(ResultSet rs)
> {
> myResultSet = rs;
> mapper = new Mapper(myResultSet);
> }
>
> public boolean hasNext() {
>
> return rs.next();
> }
>
> public Object next() {
> return mapper;
> }
> }
>
> public class MyMapper implements Map{
> ResultSet rs;
>
> public MyMapper(ResultSet rs)
> {
> this.rs = rs;
> }
>
> public Object get(String key)
> {
> return rs.getString(key);
> }
>
> ... Rest of Map methods
> }
>
>
> So the idea is that you wrap the result set in something that looks
like a
> java.util.Iterator, so Vel will iterate over it and put that into the
> context. No new objects yet.
>
> Then, each item in the set will be a mapper that also wraps the result
set
> (so no new objects created).
>
> Then, when you use it
>
> #foreach($item in $resultset)
>
> $item.foo
>
> #end
>
> Will only access the result set creating a string for the column
'foo'.
>
>
> >
> > I have never bought into the "most people working in the MVC View
> > are not programmers" argument, which is the basis of Struts and a
> > multitude of frameworks which seem to have been born in ivory
towers.
> > Sure, it's true there are more non-programmers in the View area, but
I
> > wouldn't expect any designer to be able to use VTL. No disrespect to
> > any of them, but the hundreds of designers I've worked with just
> wouldn't
> > want to know and prefer not to have the responsibility. Therefore,
> > it's a non-issue for me. Velocity Rocks.
> >
> > Thank you for any feedback on the initial question.
> > Stephen Riek.
> >
> >
> >
> >
> > ---------------------------------
> > Do You Yahoo!?
> > Get personalised at My Yahoo!.
> >
>
> --
> Geir Magnusson Jr.
> Research & Development, Adeptra Inc.
> geirm@adeptra.com
> +1-203-247-1713
>
>
>
> --
> To unsubscribe, e-mail: > unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: > help@jakarta.apache.org>
> ATTACHMENT part 2 application/octet-stream name=BeanIterator.java > ATTACHMENT part 3 application/octet-stream name=QueryUtils.java > ATTACHMENT part 4 application/octet-stream name=ResultSetMapAdapter.java --
To unsubscribe, e-mail:
For additional commands, e-mail:
---------------------------------
Do You Yahoo!?
Get personalised at My Yahoo!.