You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Christian J. Dechery" <ch...@finep.gov.br> on 2002/07/10 21:16:32 UTC

Big problem 2, the Mission!

First I would like to thank all of you for ur patience with me... 

my problem is I HATE reading documentation and references. I'm a trial-and-error developer... sometimes that's good, and sometimes (like today) it is not. 

I will now try to explain my problem with some code to be sure I won't get missunderstood. 

But before anything... I don't have ANY Servlets here... I was totally tripping... I tought a Servlet was the solution, but it is not. 

I only have JSPs (which I know are Servlets) and Beans. 

let's go: 

I have a webapp with lots of JSPs and classes... in my JSPs I have something like this: 
<%@page import="SomeBusinessObject"%> 

<% 
SomeBusinessObject sbo = new SomeBusinessObject(); 
sbo.setParameters( request.getParameter("id1"), request.getParameter("id2") ); 

sbo.getSomeAttribute(); 
sbo.setSomeAttribute(); 
sbo.update(); sbo.delete()... etc.. etc... 

... lots of other code... 
%> 

this SomeBusinessObject will have a reference to a DAOSomeBusinessObject where all the queries (insert, delete, update, select) resides... and that setParameters() method is only for it to know which row of the database we're handling with, the parameters match with the table keys of course. But that is irrelevant here.
I have also a class called DAO which provides all of the other DAO*s with a Connection... this is done internally within each DAO*. 

the method bellow is in DAO, which all the other DAO*s call to get the Connection.
 
public static synchronized Connection getConnection(String usr, String pwd, String url, String driver) {
     Connection con  = null;
 
// some code here to make some checks... irrelevant....
 
  try {
   Class.forName( driver );
      con = DriverManager.getConnection(url, usr, pwd);
     } catch(ClassNotFoundException cnf) {
      System.out.println ("*** Erro: driver nao encontrado! "+cnf.getMessage());
     } catch(SQLException sql) {
   String detalhes = "URL = "+url+"\nUSER = "+usr+"\nPWD = "+pwd;
      System.out.println("*** Erro ao obter conexão! "+sql.getMessage()+"\n"+detalhes );
     }
 
// a little bit more code....
 
  return con;
 }

now the problem - I'll try to be very clear (my english SUX, please be patient with me).
 
the webapp points to a specific db URL... this URL is in the DAOMain... if I want to change the db, I'll have to change DAOMain, recompile it, and overwrite it... nothing weird so far. Right?
 
The big thing is: this webapp gets replicated... so I copy the ENTIRE dir... classes, JSPs, jars... everything... and I know how stupid this is... and that's the problem...
 
- in each of these webapps, the only thing that changes is the JSPs, and maybe the DB url...
 
WHAT I WANT IS: I want to put our classes, jars and the oracle driver in the $TOMCAT_HOME\common dir, because they don't change from one webapp to another.... BUT since DAOMain holds the URL this is not possible (each webapp must have its own DAOMain)... I wanna create a CLASS that will receive a request from DAO or DAOMain (residing in \common) and provide a Connection based on the Context of the JSP that used SomeBusinessObject -> DAOSomeBusinessObject -> DAO.
 
or maybe DAO could do that... let's say I would write some code in getConnection() to find out where (Context) it is (well... actually DAO is always in the same place.. but the JSP that uses it, and calls it, is not) to provide the right Connection... BUT without ANY change of parameters to getConnection(), since that would snowball into a mega change of method's signatures that would end in the JSPs...
 
(I'm tired...)
 
well... that's it... I don't think I can be clearer than that... my english stops here... :)


 
 
.:| Christian J. Dechery
.:| FINEP - Depto. de Sistemas
.:| christian@finep.gov.br 
.:| (21) 2555-0332


Re: Big problem 2, the Mission!

Posted by Arshad Mahmood <ar...@compuvision.co.uk>.
Hi,

Let me see if I understand your problem. You have a JSP that creates
"business objects", the business object internally used some "DAO objects",
each DAO object when it performs it's processing calls "DAO.getConnection"
(this happens to take some parameters which I assume are hardwired into
DAOMain).

Because you have a lot of JSP's your easiest (but not the simplest!)
solution is to use JNDI. You could use parameters but then you would have to
modify every single JSP.

To each context add an environment entry of the following type:
    <Context ...........>
        ......
        <Environment name="db.url" type="java.lang.String"
value="jdbc:postgres://localhost/mydb" />
    </Context>

To read this value you need code of the following form (courtesy Will
Hartung), you can add it anywhere, in your DAOMain, business object, etc.
You probably want to add it to the function that initialises your database
parameters.

import javax.naming.*;

InitialContext ctx = new InitialContext();
String dbUrl= (String)ctx.lookup("java:comp/env/db.url");

I have tested this Tomcat 4.1.7.

Regards.

----- Original Message -----
From: "Christian J. Dechery" <ch...@finep.gov.br>
To: <to...@jakarta.apache.org>
Sent: Wednesday, July 10, 2002 8:16 PM
Subject: Big problem 2, the Mission!


First I would like to thank all of you for ur patience with me...

my problem is I HATE reading documentation and references. I'm a
trial-and-error developer... sometimes that's good, and sometimes (like
today) it is not.

I will now try to explain my problem with some code to be sure I won't get
missunderstood.

But before anything... I don't have ANY Servlets here... I was totally
tripping... I tought a Servlet was the solution, but it is not.

I only have JSPs (which I know are Servlets) and Beans.

let's go:

I have a webapp with lots of JSPs and classes... in my JSPs I have something
like this:
<%@page import="SomeBusinessObject"%>

<%
SomeBusinessObject sbo = new SomeBusinessObject();
sbo.setParameters( request.getParameter("id1"),
request.getParameter("id2") );

sbo.getSomeAttribute();
sbo.setSomeAttribute();
sbo.update(); sbo.delete()... etc.. etc...

... lots of other code...
%>

this SomeBusinessObject will have a reference to a DAOSomeBusinessObject
where all the queries (insert, delete, update, select) resides... and that
setParameters() method is only for it to know which row of the database
we're handling with, the parameters match with the table keys of course. But
that is irrelevant here.
I have also a class called DAO which provides all of the other DAO*s with a
Connection... this is done internally within each DAO*.

the method bellow is in DAO, which all the other DAO*s call to get the
Connection.

public static synchronized Connection getConnection(String usr, String pwd,
String url, String driver) {
     Connection con  = null;

// some code here to make some checks... irrelevant....

  try {
   Class.forName( driver );
      con = DriverManager.getConnection(url, usr, pwd);
     } catch(ClassNotFoundException cnf) {
      System.out.println ("*** Erro: driver nao encontrado!
"+cnf.getMessage());
     } catch(SQLException sql) {
   String detalhes = "URL = "+url+"\nUSER = "+usr+"\nPWD = "+pwd;
      System.out.println("*** Erro ao obter conexão!
"+sql.getMessage()+"\n"+detalhes );
     }

// a little bit more code....

  return con;
 }

now the problem - I'll try to be very clear (my english SUX, please be
patient with me).

the webapp points to a specific db URL... this URL is in the DAOMain... if I
want to change the db, I'll have to change DAOMain, recompile it, and
overwrite it... nothing weird so far. Right?

The big thing is: this webapp gets replicated... so I copy the ENTIRE dir...
classes, JSPs, jars... everything... and I know how stupid this is... and
that's the problem...

- in each of these webapps, the only thing that changes is the JSPs, and
maybe the DB url...

WHAT I WANT IS: I want to put our classes, jars and the oracle driver in the
$TOMCAT_HOME\common dir, because they don't change from one webapp to
another.... BUT since DAOMain holds the URL this is not possible (each
webapp must have its own DAOMain)... I wanna create a CLASS that will
receive a request from DAO or DAOMain (residing in \common) and provide a
Connection based on the Context of the JSP that used SomeBusinessObject ->
DAOSomeBusinessObject -> DAO.

or maybe DAO could do that... let's say I would write some code in
getConnection() to find out where (Context) it is (well... actually DAO is
always in the same place.. but the JSP that uses it, and calls it, is not)
to provide the right Connection... BUT without ANY change of parameters to
getConnection(), since that would snowball into a mega change of method's
signatures that would end in the JSPs...

(I'm tired...)

well... that's it... I don't think I can be clearer than that... my english
stops here... :)




.:| Christian J. Dechery
.:| FINEP - Depto. de Sistemas
.:| christian@finep.gov.br
.:| (21) 2555-0332




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>