tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Justin Jaynes <justinjaynesresea...@yahoo.com>
Subject JNDI DBCP Resources: Pool Leak
Date Thu, 08 Sep 2005 05:05:46 GMT
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: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message