You want to use a single connection for all your other beans.  You don't
want to instantiate a new connection for each bean.  So you want to use the
** one ** instance of your connection bean.  Beans of course can use each
other.  The problem is creating a singleton instance and getting your other
beans to use that singleton.  In JSP pages, I solve this problem by having a
useDb(db) in each of the other beans.  Your last question asked "from the
container".  What is the container in your scenario?  If you are not running
from a JSP, do you have a container?  Something must be driving your main
logic.  Each time that something uses a new bean, you can use the useDb
approach to tell the new bean what connection to use.  Alternatively, you
can try adding a static method to your connection class which instantiates a
singleton and then returns that to the caller.  If you invoke the JVM
multiple times, however, you'll end up with multiple instances of the
singleton.

-----Original Message-----
From: Sam [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, March 27, 2001 4:06 AM
To: [EMAIL PROTECTED]
Subject: Re: bean accessing bean


In the connection bean i have information specific for the current user. the
bean therefore resides in the session-scope.
So what's the code to grab the bean from the container?



----- Original Message -----
From: "Kris Gonzalez" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, March 27, 2001 10:48 AM
Subject: Re: bean accessing bean


> Then why not import your connection bean directly into the beans' .java
files
> and instantiate your connection bean in the constructor for all of the
other
> beans which will use it?
>
> Sam wrote:
>
> > I'm not trying to access a bean in jsp, i'm trying to access a bean from
> > another bean directly, without jsp as an intermediate.
> > I don't want to pass that connection allong to all the classes (quite a
> > tree), because not all the classes have to use the connection, just the
once
> > at the bottom of the tree.
> >
> > ----- Original Message -----
> > From: "Kris Gonzalez" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Tuesday, March 27, 2001 10:31 AM
> > Subject: Re: bean accessing bean
> >
> > > Sam...
> > >
> > > I do the same thing, and accomplish this in JSP by importing the bean
> > > class via:
> > >
> > > <%@ page import="ConnectionBean" %>
> > >
> > > and instantiate the bean with
> > >
> > > <jsp:useBean id="conn" class="ConnectionBean" scope="request" />
> > >
> > > and using it via
> > >
> > > <%    conn.getConnection();   %>
> > >
> > > or some like sequence...
> > >
> > > Sam wrote:
> > >
> > > > Hi, I'm using Tomcat and have several Beans, who all require a
> > > > database connection.I've defined a connectionBean (which ofcourse
> > > > handles the database connection via connectionPooling). I want to be
> > > > able to call this connectionBean in every other bean.Can someone
tell
> > > > me how to do that? Sam.
> > >
>

Reply via email to