简体   繁体   English

使用ejb2无状态会话bean执行顺序事务

[英]perform sequential transaction using ejb2 stateless session bean

I want to perform client request in order they called session bean. 我想按照客户端调用会话bean的顺序执行客户端请求。 But sometimes second request executed successfully before first one. 但是有时第二个请求会在第一个请求之前成功执行。

Is sequential client request execution is possible using ejb2 stateless session Bean ? 使用ejb2无状态会话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

EJB-3.x Specific : EJB-3.x特定于:

  • You need singleton bean - @Singleton , here you are using stateless bean which might execute parallely & independently for requests in random order. 您需要单例@Singleton Singleton,这里使用的是无状态bean,它可能以并行顺序独立地执行请求。

  • 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) . 默认情况下,用@Singleton注释的@Singleton是容器管理的,并使用锁定模式LockType.WRITE ,可以显式地应用@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) . 您也可以使用@Lock(LockType.WRITE)在方法级别进行注释。 Therefore, the sequence of the call will pertain the order in which they are called by clients. 因此,调用顺序将与客户端调用它们的顺序有关。

EJB-2.x Specific : EJB-2.x特定于:

  • 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. 不确定EJB-2.x中的容器管理并发性,但是同步入口方法肯定会有所帮助,因为它在内部调用其他方法。

    Edit : Delegate the requests from the beans to a singleton utility class & synchronize method appropriately. 编辑:将来自Bean的请求委托给Singleton实用程序类并适当地同步方法。 Therefore it will resolve both, the pooling & sychronizing issue with stateless beans. 因此,它将解决无状态bean的池化和同步问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM