views:

1734

answers:

5

I'm using the following code to query a database from my jsp, but I'd like to know more about what's happening behind the scenes.

These are my two primary questions.

Does the tag access the ResultSet directly, or is the query result being stored in a datastructure in memory?

When is the connection closed?

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

<sql:query var="query" dataSource="${ds}" sql="${listQuery}"></sql:query>
<c:forEach var="row" items="${query.rows}" begin="0">
    ${row.data }
    ${row.more_data }
</c:forEach>

Note: I've always been against running queries in the jsp, but my result set is too large to store in memory between my action and my jsp. Using this tag library looks like the easiest solution.

+2  A: 

I wouldn't even bother. Remove this sql from you jsp! :) SQL + Code is already bad enough. SQL + Presentation = Big no!

Anyway, it must create a regular JDBC connection with the datasource, send the query to the DBMS and close the connection.

Marcio Aguiar
The result set is too big to be stored in memory. That's the only reason I'm attempting this.
ScArcher2
I think it's really great that you so don't use the textbook jsp solution but wouldn't it be more helpful to offer a solution other than "don't do that!"?
+1  A: 

I totally agree with Marcio. The JSTL sql:* tags encourage what most people consider to be bad practice. You should not be implementing business or data access logic on your JSP page.

I would also recommend that you also use the c:* tags sparingly.

If you are trying to present the result of query in tabular format with pagination I recommend that you have a look at the Display Tag Library.

If the result set is very large then you can wrap your query results in an object implementing their org.displaytag.pagination.PaginatedList interface.

bmatthews68
The result set is too big to be stored in memory. That's the only reason I'm attempting this.
ScArcher2
+1  A: 
Will Hartung
+4  A: 

Observations based on the source for org.apache.taglibs.standard.tag.common.sql.QueryTagSupport

The taglib traverses through the ResultSet and puts all of the data in arrays, Maps, and Lists. So, everything is loaded into memory before you even start looping.

The connection is opened when the query start tag is encountered (doStartTag method). The results are retrieved when the query end tag is encountered (doEndTag method). The connection is closed in the doFinally method.

It a nutshell, it is absolutely awful.

jt
A: 

Unless you are trying to build something really quickly for Proof of Concept or something, DO NOT USE THIS!

bpapa