avatar
简单的EJB问题# Java - 爪哇娇娃
c*k
1
不同的client之间如何共享数据?
比如我现在有一个List,有一个stateful sessionbean可以访问这个list
在servlet中,会初始化一个static sessionbean instance
servlet will call method in this sessionbean to get the list
So every web client will ask servlet for this list
(this list is not persisted)
My problem is, it seems like I will get concurrentaccess exception
because if several clients happen to ask for the servlet for the same time.
What's the correct/simple way to implement such scenario?
avatar
A*o
2
make it a Vector. //grin

.

【在 c***k 的大作中提到】
: 不同的client之间如何共享数据?
: 比如我现在有一个List,有一个stateful sessionbean可以访问这个list
: 在servlet中,会初始化一个static sessionbean instance
: servlet will call method in this sessionbean to get the list
: So every web client will ask servlet for this list
: (this list is not persisted)
: My problem is, it seems like I will get concurrentaccess exception
: because if several clients happen to ask for the servlet for the same time.
: What's the correct/simple way to implement such scenario?

avatar
c*k
3
Seriously ^^
What's the simplest way to share data between sessions?
Can I use another singleton session bean to serve as an repository?
Thanks
avatar
g*g
4
If all the threads are read only, then you should have no problem.
Otherwise, you should declare transaction. You can check how to
do declarative transaction in your EJB container.
Usually, data is shared through DB.

.

【在 c***k 的大作中提到】
: 不同的client之间如何共享数据?
: 比如我现在有一个List,有一个stateful sessionbean可以访问这个list
: 在servlet中,会初始化一个static sessionbean instance
: servlet will call method in this sessionbean to get the list
: So every web client will ask servlet for this list
: (this list is not persisted)
: My problem is, it seems like I will get concurrentaccess exception
: because if several clients happen to ask for the servlet for the same time.
: What's the correct/simple way to implement such scenario?

avatar
s*e
5
in java 5.0+, copyonwritearraylist can be a candidate. Please read api for
info when to use it.
another solution is trying to sychronize all access to that list (both
mutative and nonmutative operations) depending on your biz requirements. it
can be very expensive. this is pretty much bringing the concurrency to your
knees. So be careful.
the third one is something like optimistic locking. probably you also need
read-consistency. It will be quite complex. I am not sure if you want to go
for tha
avatar
c*k
6
I see. So the normal way to share data is through DB (entity Bean in EJB2).
Thanks for all the replying. :)
avatar
t*e
7
Stateful session beans are designed to be used within a session scope,
meaning, different bean instances of the same bean class are instantiated to
maintain client-specific data. If you make a SFSB application scope --
shared by all client sessions, EJB containers are supposed to raise a
concurrent access exception according to the EJB specification. In WebLogic,
you may specify "allow-concurrent-calls" to be true in weblogic-ejb-jar.xml
file to override the default behavior. However, it is stil
avatar
c*k
8
So, if shared information needed to be passed between sessions. We can
either use Message-driven bean or through DB. Right?
avatar
t*e
9

There are different means to share data across web sessions.
An object stored in ServletContext, a stateless session bean (readonly),
SFSB with DB access...

【在 c***k 的大作中提到】
: So, if shared information needed to be passed between sessions. We can
: either use Message-driven bean or through DB. Right?

avatar
g*g
10
It all comes down to whether the data should be persisted.
If it should be, then I would use slsb with DB access.
If not, then slsb with synchronized access if RW.
I would never use sfsb to share data across sessions.

【在 t*******e 的大作中提到】
:
: There are different means to share data across web sessions.
: An object stored in ServletContext, a stateless session bean (readonly),
: SFSB with DB access...

avatar
c*k
11
Can I use synchronized method inside SLSB?
I remembered somewhere it said we shouldn't use static field or synchronized
method in the EJB.
avatar
t*e
12

synchronized
You don't have to synchronize the method. Instance pooling in SLSB makes
sure that one instance of SLSB is allocated per thread. That is why SLSB is good
for read only operations. If you do updates, you need more complicated
approaches to synchronize data changes, e.g., database persistence.

【在 c***k 的大作中提到】
: Can I use synchronized method inside SLSB?
: I remembered somewhere it said we shouldn't use static field or synchronized
: method in the EJB.

avatar
w*f
13
Just make sure your session bean is thread safe, otherwise I suggest you
make the session bean as the local variable inside your service() method.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。