简体   繁体   English

在EJB中@Eingleton上@Stateless的实际用例是什么?

[英]What is a real use-case for @Stateless over @Singleton in EJB

if I understand EJB correctly, @Singleton is actually the same as Singleton in plain Java and also singleton in spring -> one instance, every call goes through the same instance concurrently. 如果我正确理解EJB,@ Singleton实际上与普通Java中的Singleton相同,而且在spring中也是单例 - >一个实例,每个调用同时经历同一个实例。 @Stateless declares a bean, that could (but must not) have multiple instance, with the limitation that only one call can be in an instance at the same time. @Stateless声明一个bean,它可以(但不能)有多个实例,但有一个限制,即同时只有一个调用可以在一个实例中。 Right sofar? 对吗? This remains me on the servlet programming model: in theory servlet containers are allowed to make multiple copies of the servlet, in practice I haven't seen any servlet container to do so. 这仍然是我的servlet编程模型:理论上,servlet容器允许创建servlet的多个副本,实际上我还没有看到任何servlet容器这样做。 So assuming I do not have REALLY LIMITED resources like doors, windows or printers in my code (and if I did I could still solve it with queues and stuff), what is the REAL example, where usage of @Stateless is advantageous over usage of @Singleton. 所以假设我的代码中没有真正有限的资源,比如门,窗口或打印机(如果我这样做,我仍然可以用队列和东西解决它),那么真实的例子是什么,@ Stateless的使用优于使用@辛格尔顿。

regards Leon 对莱昂来说

You can have multiple instances of a stateless bean to increase throughput. 您可以拥有无​​状态bean的多个实例来提高吞吐量。

On the other hand there is only one instance of a singleton. 另一方面,只有一个单身实例。 The reason for this is normally to share state in application scope, serializes access to resources etc., and this implies locking or synchronization. 其原因通常是在应用程序范围内共享状态,序列化对资源的访问等,这意味着锁定或同步。

So if you are not really having a singleton, then use a stateless bean. 所以如果你真的不是单身人士,那就使用无状态bean。

If you have a "stateless singleton", there is no difference. 如果你有一个“无国籍单身人士”,那就没有区别了。 But if you read "singleton", it has a special meaning by convention (= there must be a reason for using the singleton pattern). 但是如果你读“单身”,它按惯例有特殊意义(=必须有使用单身模式的理由)。

Stateless implies that the bean is thread safe. 无状态意味着bean是线程安全的。 This is because there is no code in the bean that relies on state. 这是因为bean中没有依赖于状态的代码。 This means that running any of its methods will not affect future running of said methods. 这意味着运行其任何方法都不会影响所述方法的未来运行。

An example of a stateless class would a class that does addition and subtraction. 无状态类的一个例子是进行加法和减法的类。 All the necessary parameters are passed into the method. 所有必要的参数都传递给方法。 Doing an addition or subtraction does not alter the way these methods work at a later call. 执行加法或减法不会改变这些方法在以后调用时的工作方式。 This implies that you do not need to worry about concurrency with the class. 这意味着您不必担心类的并发性。

A singleton is usually used for a class that is very expensive to create such as a Database connection. 单例通常用于创建非常昂贵的类,例如数据库连接。 You do not want every class creating a new Database connection every time they need to use the database so you have it instantiated once at program start up. 您不希望每个类在每次需要使用数据库时都创建新的数据库连接,因此您可以在程序启动时将其实例化一次。 Being a singleton does not necessarily mean that the class is thread safe (although it absolutely should be). 作为一个单例并不一定意味着该类是线程安全的(虽然它绝对应该是)。

So Stateless means the class is threadsafe. 所以无状态意味着这个类是线程安全的。

Singleton refers to the fact that the class is only created once. Singleton指的是这个类只创建一次。 While this heavily implies that the class is (AND IT SHOULD BE) thread safe, it does not directly imply it like stateless does. 虽然这很大程度上意味着该类是(并且应该是)线程安全的,但它并不像无状态那样直接暗示它。

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

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