wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Stateful session beans and JNDI-Lookup in Netbeans 6.8/EJB 3.1 does not work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Stateful session beans and JNDI-Lookup in Netbeans 6.8/EJB 3.1 does not work" Watch "Stateful session beans and JNDI-Lookup in Netbeans 6.8/EJB 3.1 does not work" New topic
Author

Stateful session beans and JNDI-Lookup in Netbeans 6.8/EJB 3.1 does not work

Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
I wanted to test the functionality of a very simple stateful session bean, but nothing works! The JNDI-Lookup does not work as the bean is not found and when I call the bean by Depency-Inection via @EJB annotation the value is not memorized.


The stateful session bean:



In the above bean, the variable "memory" should retain its value when called from the same browser, eg. by browser refresh.

The client is programmed with @EJB dependency inection. The variable state of memory is lost.

I marked the JNDI-Lookup bold. This results in a javax.naming.NamingExdeption: Lookup failed for...
I opened an error no 181367 at netbeans.




Urgent helb needed!!!

Docendo discimus (we learn by teaching)
SCJP 5 Zertifizierung, Vorbereitungsbuch; SCJP 6 Zertifizierung, Vorbereitungsbuch

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9912
    
158

Are you trying EJB3.1 or EJB3.0?

With EJB3.0, you can have a nointerface view for a bean:



If you are on EJB3.0, have you specified a remote/local business interface for that bean through xml? If not, you will have to either specify it through @Remote/@Local or use their xml counterparts.

[My Blog] [JavaRanch Journal]
Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
I use the Netbeans 6.8 Bundle (EJB 3.1, EE6) and want to program a shopping cart example with a stateful session bean.

When I call the bean per injection I reach the bean, but the variable does not retain its value. It has to do something with thread safety:

@EJB
private NewSessionBean newSessionBean;

When I click the JNDI call together (all is done by Netbeans 6.8) there ist an error.







I do not type one line by myself! The following error occurs:

java.lang.RuntimeException: javax.naming.NamingEception: Lookup failed for 'java:global/StatefulAufruf-war/StatefulBeanAufruf-ejb/NewSessionBean!ch.geo.bohne.NewSessionBean

There must be an error in this lookup statement or there is something wrong with the application server.

The lookup string is composed as follows:

java:global [<app-name>]<module-name>/<bean-name>!<fully-qualified-interface-name>

The generated lookup string seems to be correct. The <app-name> is omitted, <module-name> is the -war Archive, the <bean-name> is StatefulBeanAufruf-ejb and the <fully-qualified-interface-name> is ch.geo.bohne.NewSessionBean.







Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Terence,

have a look at the examples in EJB 3.1 spec, section 4.4.2. I think the lookup-name should be

java:global/StatefulAufruf-war/NewSessionBean

In my opinion it's a disadvantage of glassfish (compared with JBoss, for example) that not all JNDI bindings are listed by the administration tool. But if you start glassfish with the verbose option (use startserv.bat in windows) then the global JNDI name of the ejb should is printed to the console.

As an alternative to using a global JNDI name you could also choose the classical solution for servlets: Also note: Stateful session beans shouldn't be injected or initialized as instance variable because a servlet can be used in multiple threads.

SCJP 5 (98%) - SCBCD 5 (98%)
Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
Hi Ralph

it works! But there is still something else which is not clear:

I want to show, that a stateful session bean can store variable values across browser calls. In the following example that does not work.

a) I have the stateful session bean, the member variable intMemory should memorize its value.



b) I have a servlet, which sets variable intMemory to 10.




c) I have an identical servlet, who reads the the variable IntMemory: out.println(newSessionBean.getIntMemory());

Now the problem is, that the value set with the first applet ist not stored when I call it with the reading Applet. I used an Integer in case primitive Variables are not serialized. But this does not help.



Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
Designing a shopping cart with a stateful session bean

I found a solution, but it is very inelegant:

The servlet has alzheimer and does not remember the refernce variable of the stateful session bean after a refresh. So you have to save the reference in a HttpSession variable.

The stateful session Bean in wich the state of intMemory should be preserved:




The client Servlet reads the Value of intMemory and adds 10 to it. If yo do not save the reference to the Bean the value will stay 10. If you uncomment the blocks and comment out

NewSessionBean newSessionBean = lookupNewSessionBeanBean();

it works as expected, the values of the intMemory variables are as expected 10, 20, 30....




It ist still not time to

First: Suppose I have 2 catalogs with goods and one cart (which is the stateful session bean). If the reference to the stateful session bean is unique, it would not be possible to save values from two catalogs (i.e. servlet clients) to the one single bean. The shop could have only one catalog or the reference has to be promoted to another catalog, i.e. in a session

Second: The above solution where the reference variable of the stateful session bean is saved in a HttpSession seems inelegant, somebody knows a better solution?

Third: Can the variable intMemory be an int instead an Integer and still be preserved? (you usually can only save Objects in a session, but here an int is a compound of an object, I will try and report).


 
Consider Paul's rocket mass heater.
 
subject: Stateful session beans and JNDI-Lookup in Netbeans 6.8/EJB 3.1 does not work
 
Similar Threads
How to call a JPA-Facade from a JSP
Hello world ejb3 problem
EJB, JNDI
Problem in running session beans
how to create statefull session bean in netbeans 6.9.1