java ee 6 - Stateful session bean forgets values -


with java ee need use stateful session bean.

@stateful @transactionmanagement(transactionmanagementtype.bean) public class facadeexercice extends abstractfacade<entitybeanexercice>   implements ifacadeexercice {  @persistencecontext(unitname = "gestioncours-ejbpu") private entitymanager em; @resource private usertransaction transaction; private int lastchange; private int connections;  [...]  @override public entitybeanexercice find(object id) {     entitybeanexercice ex = null;     connections += 5;     try {         transaction.begin();         ex = super.find(id);         lastchange = ex.getlastchange();         transaction.commit();     } catch (exception ex1) {         logger.getlogger(facadeexercice.class.getname()).log(           level.severe, null, ex1);     }     return ex; } } 

but every time enter in bean, connections variable set 0.

i have no idea can search solution.

this problem can arise in these situations:

  • the lifetime of sfsb connected lifetime of client.

your sfsb works fine, if have command line client example. when command line application terminated, sfsb removed well.

if sfsb used jsp/servlet example, it's lifetime ends, when http request completed. if survive http request, have put it's handle in http session: after have got instance jndi lookup, should put instance attribute in httpsession. next http request use sfsb must handle httpsession.

  • each jndi lookup returns new instance

a quote ejb 3.1, 4.6 stateful session bean state diagram

when stateful session bean looked or otherwise obtained through explicit jndi lookup mechanisms, container must provide new stateful session bean instance, required java ee specification (section “java naming , directory interface (jndi) naming context” [12]).

so should not lookup sfsb more 1 time.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -