Campano, Troy wrote:
>         Hello,
> I am having a problem trying to figure out JSTL with connection pooling.
> I am using Tomcat. I understand that <sql:setDataSource> can't handle
> connection pooling.
>
>
>         So I create a datasource the way I always do without JSTL (using
> JNDI).
> I'm having a problem passing that connection to <sql:query>
> I don't know how to use variables in the tags.
>
>         My datasource is called 'ds', but I don't know how to use it with
> sql:query.
>
>         pageScope.ds doesn't work.
>
>         I'm a complete JSTL newbie and totally confused.
>
>
>         Does anyone have any ideas?
>
>         Below is the code

You're working too hard; there's no need for this scriptlet:

 > [...]
>         <%
> Connection conn = null;
> try
> {
> //**************************************************************************
> ***********************
> // Request Connection from Connection Pool
> //**************************************************************************
> ***********************
> Context initContext = new InitialContext();
> Context envContext  = (Context) initContext.lookup("java:/comp/env");
> DataSource ds = (DataSource) envContext.lookup("jdbc/ReportManagerPool");
> conn = ds.getConnection();
> //**************************************************************************
> ***********************
> %>

Just use the JNDI name in the <sql:query> action element:

   <sql:query dataSource="jdbc/ReportManagerPool" var="myresultset"
     sql="SELECT table_name FROM all_tables" />

If you want to, you can even declare the data source in the web.xml
file and use the JSTL SQL actions without a dataSource attribute in
your JSP pages.

And you should definitely not have the following scriptlet. The JSTL
action takes care of closing the connection (returning it to the pool),
even if there's an exception. If you still need to handle the exception
somehow, you can use the JSTL <c:catch> action (see the spec or a JSTL
book, like my JavaServer Pages, 2nd Edition, for details).

>         <%
> }
> catch(SQLException e)
> {
>  throw new SQLException(e.getMessage());
> }
>
>           finally
>   {
>     if (conn != null)
>         {
>       try
>           {
>         conn.close();
>       }
>           catch (SQLException e)
>           {
>           // Nothing coherent to do here.
>           // Pray nothing else goes wrong.
>           e.printStackTrace();
>       }
>         }
>   }
> %>

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
JavaServer Pages        http://TheJSPBook.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to