How would you change the EJB to prevent multiple clients from simultaneously accessing the sayHello method of a single bean instance?

Given the following stateless session bean:

How would you change the EJB to prevent multiple clients from simultaneously accessing the
sayHello method of a single bean instance?

Given the following stateless session bean:

How would you change the EJB to prevent multiple clients from simultaneously accessing the
sayHello method of a single bean instance?

A.
Convert sayHello into a synchronized method

B.
Execute the call to generateLocalizedHello in a synchronized block

C.
Convert generateLocalizehello into a synchronized method

D.
Convert HelloWordBean into a singleton bean

E.
No changes are needed

Explanation:
* It is not possible for two invocations of synchronized methods on the same object
to interleave. When one thread is executing a synchronized method for an object, all other threads
that invoke synchronized methods for the same object block (suspend execution) until the first
thread is done with the object.
* When a synchronized method exits, it automatically establishes a happens-before relationship
with any subsequent invocation of a synchronized method for the same object. This guarantees
that changes to the state of the object are visible to all threads.
Reference: The Java Tutorial, Synchronized Methods



Leave a Reply 5

Your email address will not be published. Required fields are marked *


Tamer

Tamer

E: stateless, & statefull dont support concurrent access

Mau Lop Riv

Mau Lop Riv

Stateless & Stateful session bean execution is thread safe

JavaBatman

JavaBatman

A is right solution!