I'm trying to figure out if I have something wrong in the way I'm
designing this application or if this is a limitation of the
J2EE architecture.
I'm writing a web application running on Tomcat 4.0, using Servlets,
JSPs and JavaBeans. I'm using PoolMan for DB pooling to a Postgres
backend. I would like to have a MVC model for this application where the
JavaBeans do the object relational mapping and business logic, like
EJBs. I would like to have them pull data from the database via the
PoolMan datasource. I would then like to display manipulate the beans
via servlets and JSP pages.
I have tried to place a reference to the PoolMan DataSource object into
the JNDI namespace 'java:comp/env' three different ways,
1) Using the Tomcat <resource-ref> tags and the DataSource factory
2) Using the <load-at-startup> tag on a servlet with an empty
service() method which has the following in init():
public void init(ServletConfig config) throws ServletException {
try {
String datasource = config.getInitParameter("database");
DataSource ds = PoolMan.findDataSource(datasource):
config.getServletContext().setAttribute("com.arrayservices.datasource",ds);
}
catch (SQLException sqle) {
sqle.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
3) By writing a custom ConnectionFactory that will allow me to create
Connections from the DataSource from PoolMan.
None of these methods are accessable from JavaBeans although they seem
to work fine using DBtags from taglibs and from JSPs
The JavaBean I have been testing with currently accesses the database
using the following code:
DataSource ds = PoolMan.findDataSource("jCart");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlquery);
Those lines are broken up into various methods in the Bean in order to
increase reuse of connections. I would like to pass around the
DataSource object, either between JavaBeans directly or via JNDI. Since
JavaBeans are not J2EE components, they aren't allowed to access
'java:comp/env', and I can't see any way for them to reference one
another without servlet intervention, which breaks the whole MVC model.
I want to stay away from EJB containers due to the overhead and the rmi
registry because I would like to be able to run this application in a
shared environment. I'm wondering if maybe I could write BMP Enity
Beans and Session Beans and then treat them as regular beans without
using an EJB container or if there is some other method of making these
JavaBeans aware of shared resources that I'm missing here.
I'm stumped and feel that I may have to write something kludgy, such as
having every JavaBean use the PoolMan.findDataSource() method in order
to access the DataSource pool. If anyone can shed any light on
something I haven't thought of or any misconceptions I have, I'd really
appreciate it.
BTW, I've looked through all the books I have, several times, searched
on Google, checked the mailing list archives on tomcat-user, tomcat-dev,
jboss-user, jboss-dev, searched on a few forums over at the Java
Developer's Connection, read through the Tomcat and Poolman docs
(several times), looked at the API docs and the Servlet 2.3 spec,
checked the web-app DTD, and generally exausted every resource for
finding some info on this I can think of.
Thanks in advance.
--
Curt Johnson [EMAIL PROTECTED]
Array Services, Inc. http://www.arrayservices.com
"Open Source, Open Standards"
Unix and Internet Applications development, integration and administration