c***k 发帖数: 1589 | 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? |
A**o 发帖数: 1550 | 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?
|
c***k 发帖数: 1589 | 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 |
g*****g 发帖数: 34805 | 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?
|
s******e 发帖数: 493 | 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 |
c***k 发帖数: 1589 | 6 I see. So the normal way to share data is through DB (entity Bean in EJB2).
Thanks for all the replying. :) |
t*******e 发帖数: 684 | 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 |
c***k 发帖数: 1589 | 8 So, if shared information needed to be passed between sessions. We can
either use Message-driven bean or through DB. Right? |
t*******e 发帖数: 684 | 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?
|
g*****g 发帖数: 34805 | 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...
|
c***k 发帖数: 1589 | 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. |
t*******e 发帖数: 684 | 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.
|
w******f 发帖数: 620 | 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. |