Concerning JNDI Database Connection Pooling Sources, I
have read that if you fail to explicitely close Result
Sets, Statements, or Connections to the DataSource
from WITHIN the web application, a connection in the
pool will be "lost."  (I read this at:
http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jndi-datasource-examples-howto.html).

I understand that you can set an option to reclaim
"lost" connections.  That's great.

But to avoid this in the first place, I have a
question.

I have a JSP which sends Statement objects to a Bean
(not a bean by exact definition) which accesses the
database and returns a ResultSet.

The jsp code is like this:

<% com.xxxxx.DatabaseInterface web = new
com.xxxxx.DatabaseInterface(); %>
<% web.connect(); %>
<% String selectSQL = "SELECT * FROM place;"; %>
<% ResultSet result = web.selectQuery(selectSQL); %>

<% while ( result.next() ) { %>
        <%= result.getString("placename") %>
<% } %>

<% web.disconnect(); %>

Of course, there are statement objects and resultset
objects in the code above.  Do I have to explicitely 
close them IN JSP AND also the ones IN THE BEAN or
just  the ones in the BEAN?  I suppose this is a
fundamental principle of Java which I do not fully
understand as the objects .  Can someone please
enlighten me?

The Bean looks like this:

package com.xxxx;

import java.sql.*;
import javax.sql.*;
import java.io.*;
import javax.naming.InitialContext;
import javax.naming.Context;

public class DatabaseInterface {

        String error;
        
        DataSource xxxx;
        
        
        Connection database;
        ResultSet result;
        Statement select;
        Statement update;

        public DatabaseInterface() { }

        public void connect() throws Exception {
                try {
                        InitialContext initContext = new InitialContext();
                        xxxx = (DataSource)
initContext.lookup("java:comp/env/jdbc/xxxx");
                        database = xxxx.getConnection();
                } catch (Exception e) {
                        error = "Data Source Opening Connection Error: " +
e;
                        throw new Exception(error);
                }
        }

        public void disconnect() throws Exception {
                try {
                        
                        database.close();
                } catch (Exception e) {
                        error = "Data Source Closing Connection Error: " +
e;
                        throw new Exception(error);
                }

        }

        public ResultSet selectQuery(String webStatement)
throws SQLException, Exception {
                try {
                        if ( database != null) {
                                select = database.createStatement();
                                result = select.executeQuery(webStatement);
                        }
                } catch (SQLException sqle) {
                        error = "SQL Error Disconnecting: " + sqle;
                        throw new SQLException(error);
                } catch (Exception e) {
                        error = "Error in selectQuery block:" + e;
                        throw new Exception(e);
                }
                return result;
        }

        public void insertQuery(String webStatement) throws
SQLException, Exception {
                try {
                        if ( database != null) {
                                update = database.createStatement();
                                update.execute(webStatement);
                        }
                } catch (SQLException sqle) {
                        error = "SQL Error: " + sqle;
                        throw new SQLException(error);
                } catch (Exception e) {
                        error = "Error in insertQuery block:" + e;
                        throw new Exception(e);
                }
        }
}



        
                
______________________________________________________
Click here to donate to the Hurricane Katrina relief effort.
http://store.yahoo.com/redcross-donate3/

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to