chetan
chetan

Reputation: 3255

perform sequential transaction using ejb2 stateless session bean

I want to perform client request in order they called session bean. But sometimes second request executed successfully before first one.

Is sequential client request execution is possible using ejb2 stateless session Bean ?

public generate(){
    .................
    update()
    .............
}
private update(){

  debugLog(" update query for "+id);

   //code for update query

  debugLog(" execute update query for "+id);


}

When I send two request simultaneously i got log like ..

 update query for 16
 update query for 16
 execute update query for 17
 execute update query for 16

But i want to execute it serially like

update query for 16
 update query for 16
 execute update query for 16
 execute update query for 17

Upvotes: 1

Views: 770

Answers (1)

Nayan Wadekar
Nayan Wadekar

Reputation: 11622

EJB-3.x Specific :

  • You need singleton bean - @Singleton, here you are using stateless bean which might execute parallely & independently for requests in random order.

  • Now sequencing the events in order, you have to use locking mechanism at class/method level based on your requirements.

    By default, beans annoted with @Singleton are container managed & uses locking mode LockType.WRITE, explicitly can apply @ConcurrencyManagement(CONTAINER). If any of the method is being called by a client, all the other requests will have to wait for the previous call to return.

    You can also annotate at method level with @Lock(LockType.WRITE). Therefore, the sequence of the call will pertain the order in which they are called by clients.

EJB-2.x Specific :

  • You have to create singleton on your own as annotation aren't available.

  • Not sure about container managed concurrency in EJB-2.x, but synchronizing the entry method would definitely help, as it is calling other methods internally.

    Edit : Delegate the requests from the beans to a singleton utility class & synchronize method appropriately. Therefore it will resolve both, the pooling & sychronizing issue with stateless beans.

Upvotes: 1

Related Questions