Why Stateless session beans?

前端 未结 6 1459
谎友^
谎友^ 2020-12-03 06:09

I was reading about stateless session bean and couldn\'t understand it\'s use.

Excerpt from sun tutorial below

\"..Because stateless session beans can suppor

6条回答
  •  没有蜡笔的小新
    2020-12-03 06:59

    Having used EJB 3.0, in my opinion Stateless Session beans are there as to complete the Enterprise Bean landscape. They indeed are there to setup a Facade to the rest of your business logic. People often suggest SLSB's to be threadsafe, but this is misleading to say the least.

    They are definitely not thread safe when their codepath includes calling non-threadsafe code (eg. a shared non threadsafe cache).The only guarantee that SLSLB give is that a single SLSB instance is used by at most one thread at the same time. This basically boils down to SLSB's having synchronized method access, and that there will be multiple instances to serve client calls. But having a SLSB method calling code from a shared non-thread safe class from these multiple instance could still wreak havoc and would render the SLSB in question non-threadsafe.

    Since EE contexts (transactions , security resources etc) are bound to the thread already I see no need for SLSB over say Spring Singletons. They do complement Statefull Session beans in an EJB-only application.

    In my opinion the route they choose with SLSB's and the new lock concurrency settings for EJB 3.1 is an attempt to dumb down the programmer and have the Mighty Container serve your needs. Do yourself a favor and go read Java Concurrency in Practice and start using singletons combined with stock java thread concurrency constructs. (synchronized, volatile, concurrent collections etc.)

提交回复
热议问题