• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stateful Session Bean - Conversational State

 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I implmeneted a stateful session bean to represent a shopping cart and invoking this bean from servlet. Every time I add an item to the cart I am showing the number of items in the Servlet. However, the bean is returning the recently added items always and not preserving previously added items in the list. The following is the code I am using.

Stateful Session Bean Code:



Servlet Code:



Item Code:



My understanding is, the bean's @PostConstruct call back is being called every time I add an item to the cart. I do not know why a new instance is being returned for each servlet call eventhough I am using the same bean reference to add item to cart and to get items from cart. Please help!

I deployed this bean to Glassfish V3 along with web application as an EAR using Eclipse.
 
Christian Nicoll
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sai,

i'm bit confused about your servlet-code. In which method (doGet, doPost, ...) do you 've put this servlet-code? Please show the whole servlet-code.

My understanding is, the bean's @PostConstruct call back is being called every time I add an item to the cart.


That is not my understanding.

I believe that this makes not really sense. It is more logical if you call an method (addItem) of a stateful session bean, that add's the item to customers shopping cart. Otherwise if the @PostConstruct call back would be called then there would exists one stateful session bean for each call, so each Bean would 've only one item.
 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nicoll,

Thanks for the reply I managed to make it work with DI using @EJB annotation.

Still not sure why this code in servlet not working as desired. The servlet code I posted is exactly from doPost(). Just calling the addItem() may not work since the shopping cart array list is not initialized hence I did initialization in @PostConstruct.
 
Christian Nicoll
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sai,

I would try the following code in the doPost()-method:

Hoping it helps.
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, that every time look up Item via JNDI you end up with new instance.
Consider this text from ejb core spec, page. 74-75

A session bean instance’s life starts when a client obtains a reference to a stateful session bean
instance through dependency injection or JNDI lookup, or when the client invokes a create<METHOD> method
on the session bean’s home interface. This causes the container to
invoke newInstance on the session bean class to create a new session bean instance. Next,
the container injects the bean’s SessionContext, if applicable, and performs any other
dependency injection as specified by metadata annotations on the bean class or by the deployment
descriptor. The container then calls the PostConstruct lifecycle callback interceptor
method(s) for the bean


So what I think you should do is to store the reference to HttpSession as attribute and use it in subsequent requests to your servlet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic