Forums Register Login

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

+Pie Number of slices to send: Send
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!!!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.







+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.



+Pie Number of slices to send: Send
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).


Something must be done about this. Let's start by reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 10084 times.
Similar Threads
how to create statefull session bean in netbeans 6.9.1
Hello world ejb3 problem
Problem in running session beans
Unable to run servlet code in netbeans with glass fish
EJB, JNDI
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:13:00.