Avatar billede fredand Forsker
21. september 2017 - 13:58 Der er 4 kommentarer og
1 løsning

Why is the value of a member preserved in our EJB?

Hello guys!
Just before I had to run away a colleague of mine presented a problem that he had between runnings of a statless EJB. A private member kept a value between different executions. The idea was that he should read a value from a database and If true do some logging. But it did not changed even though he changed it in the database. I could not explain why. Below I hope I recall hus code.

@Statless
Public class Myejb
{
Private Boolean log;

Public vid Dosomestuff()
{
If (log==null)
{
log=Readlogsettingfromdb();
}
If(log)
{
system.out.println("logging is true");
}
}

He claims that he started this in a Jboss with the value in the dB set to false during first run and he got no logging. Next run the value set to true in the dB but he get no logging.

The idea was that the EJB would have been initiated before every time it is executed and the log would have been null before each time.

What would you expect would be the outcome?
Or do you think there is a bug somewhere?
Best regards
Fredrik
Avatar billede arne_v Ekspert
21. september 2017 - 17:02 #1
Bug in the code.

:-)

Both stateless and stateful sessions beans are reused pooled objects.

The difference is that multiple calls to a SF bean is guaranteed to hit the same instance while SL are not.

But you can get the same instance for SL.
Avatar billede arne_v Ekspert
21. september 2017 - 17:04 #2
What about a singleton EJB with a scheduled method to update log info?
Avatar billede fredand Forsker
21. september 2017 - 20:26 #3
Thank you, you could not make it more clear!
Best regards
Btw I will look into the scheduled method solution, that I have missed. Intressting.
Avatar billede arne_v Ekspert
21. september 2017 - 21:50 #4
http://www.vajhoej.dk/arne/articles/modernjee.html#ex_test4

scroll down a couple of pages - it has an example of singleton EJB with scheduled method
Avatar billede arne_v Ekspert
21. september 2017 - 21:52 #5
It also has this blurb:



SLSB (StateLess Session Bean)
    if some client code make multiple calls to a SLSB then they may end up being processes by different instance of the EJB class
SFSB (StateFul Session Bean)
    if some client code make multiple calls to a SFSB then they will always end up being processes by the same instance of the EJB class


Hmmm. It looks like I have to fix some spelling errors.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester