jstl的sql标签如何工作? [英] How does jstl's sql tag work?

查看:78
本文介绍了jstl的sql标签如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从我的jsp查询数据库,但我想知道更多关于幕后发生的事情。

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.

标签是直接访问ResultSet,还是查询结果存储在内存中的数据结构中?

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

什么时候连接关闭?

<%@ 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>

注意:我一直反对在jsp中运行查询,但我的结果集太大了在我的动作和我的jsp之间存储在内存中。使用此标记库看起来是最简单的解决方案。

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.

推荐答案

基于org.apache.taglibs.standard.tag的源代码进行观察.common.sql.QueryTagSupport

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

taglib遍历ResultSet并将所有数据放入数组,映射和列表中。因此,在开始循环之前,所有内容都会加载到内存中。

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.

遇到查询开始标记时,会打开连接(doStartTag方法)。遇到查询结束标记时检索结果(doEndTag方法)。连接在doFinally方法中关闭。

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.

这篇关于jstl的sql标签如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆