Title: Struts examples,docs for 1.0

Hi All,

Great job on Struts!

We have been playing with 0.5 but now that we are going to start development we are going to download the latest 1.0 beta, and we are aware of some deprecations, changes etc etc.  Are there any real traps to look out for? Will the current example code work or has it been updated in the latest build.

cheers,

Bill Pearce

-----Original Message-----
From: Craig R. McClanahan [mailto:[EMAIL PROTECTED]]
Sent: Thursday, December 07, 2000 11:36 AM
To: [EMAIL PROTECTED]
Subject: Re: EJB references


"Boulatian, Misak" wrote:

> Hi Jim and Alex,
>
> I think both of you understood me wrong. I am not planning to access EJBs
> from JSP pages at all. I will have a lot of session EJBs to manipulate
> Entity EJBs. But, the suggested approach accessing them from servlets was:
> - Declare EJB references as private member variables
> - in init() of servlet: initialize enterprise naming context, use that
> context to call lookup to obtain reference, use reference to call portable
> remote object's narrow method (see an example below)
>

There is one problem with your suggested coding approach that makes it
essentially unusable :-(

Instance variables in a servlet (whether private, protected, or public makes no
difference) are shared among all the requests that are simultaneously accessing
your servlet.  The same thing would happen if you use an instance variable to
store an EJB reference in an Action class.

This conflicts with the EJB requirement that a session bean be used by only one
thread at a time.  As soon as two simultaneous requests occur, you will have two
threads trying to use "m_home" at the same time.

>
> BEGIN EXAMPLE
> public class myservlet extends HttpServlet throws ... {
> private CustomerHome m_home;
>
> public void init() ... {
> try {
>   Properties p = new Properties();
>   // ... specify some JNDI properties specific to the vendor
>   Context jndicontext = new javax.naming.InitialContext (p);
>   Object ref = jndicontext.lookup("CustomerHome");
>    m_home = (CustomerHome) PortableRemoteObject.narrow(ref,
> CustomerHome.class);
>    ....
> } catch ... {
> }
> END EXAMPLE
>
> Then you hold on to m_home reference and reuse it in each thread that
> accesses your servlet. So, my question was, since I am going to have many
> m_home(s) like this what should I do? If I put those in each action class
> then every single time I am going to do above procedure (very resource
> consuming). Or, should I create private member variables in action classes
> and hold on to those home references? As Craig recommended, to create a
> startup servlet and initialize those in there. But, how am I going to make
> all of those availbe to action classes (If this is very simple, pardon my
> knowledge. I am new to using EJBs)?
>
> If you can give me some more ideas I really appreciated it.

The code part of your init() is pretty close to the recommended procedure.  The
big  difference is that you should do the lookup for each request, and store the
reference that gets returned in a *local* variable, not an instance variable.
The J2EE Specification, and the Blueprints manual (both available via
<http://java.sun.com/j2ee> both go into more depth on the recommended idioms for
accessing EJBs and other resources from web applications.  These practices work
just fine in a Struts environment as well.

>
> Thanks,
> Misak
>

Craig McClanahan

Reply via email to