You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-users@jakarta.apache.org by emily chen <em...@yahoo.com> on 2006/06/23 19:28:26 UTC

JCS thread safe question.

Hi there,
   
  I want to use JCS for my web application, and have some thread safe question.
   
  Here is scenario: 
   
  I have a JCS cache named “ePINBatchReportCache”, which is used to save the ePIN Batch Report data.
   
  Thread 1:
  1)      Get report data from Cache for key.
  2)      If not data found in the cache, then lock the cache for that key.
  3)      Get report data from database.
  4)      Put the report data in cache for key
  5)      Release lock for that key.
   
  Thread 2:
  1)      Get the data from cache for key.
  2)      Wait for lock to be released (should have max wait time here).
  3)      Get the data from cache.
   
  Does JCS have the above functionality -- lock the key of cache?  Or I have to put lock on the key in my web application source code?
   
  Thank for your help.
   
  Lucy 

 		
---------------------------------
Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less.

Sync on restart?

Posted by Michael Toback <Mi...@chordiant.com>.
I am using a memory-based UDP Discovery lateral cache. All seems to work
great, but we are having some issues with failover.

Say multiple instances are running. The first instance unexpectedly goes
offline and is restarted. 

Meantime, several pieces of information are placed into the cache on the
second instance. The broadcasting occurs, but no one is there to listen.


As I understand it, the first node will never see that data unless I
write some sort of recovery mechanism?

Michael I Toback 
Principal Engineer
Orchestrating Business Processes
Chordiant Software
Cupertino, Ca 95014
(408)-517-6165

The information transmitted herewith is sensitive      information of Chordiant Software or its customers and is intended only for use to the individual or entity to which it is addressed. If the reader of this message is not the intended recipient, you are hereby notified that any review, retransmission, dissemination, distribution, copying or other use of, or taking of any action in reliance upon, this information is strictly prohibited. If you have received this communication in error, please contact the sender and delete the material from your computer.

---------------------------------------------------------------------
To unsubscribe, e-mail: jcs-users-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jcs-users-help@jakarta.apache.org


Re: JCS thread safe question.

Posted by Aaron Smuts <as...@yahoo.com>.
There is a utility called jcs worker that will do what
you need.

http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/utils/access/JCSWorker.java?revision=332196&view=markup





--- emily chen <em...@yahoo.com> wrote:

> Hi Aaron,
>    
>   If I don't use a lock, thread 1 gets the report
> from cache by key, not data found, so it goes to DB
> to get the report;  before it puts the report into
> cache, thread 2 accesses the cache by the same key,
> and can not find the report, so it goes to DB to get
> the data again. That means two thread will access DB
> twice. But I want they just acces DB only one time
> to get the same report.
>    
>   In my web application, it takes serval mins to run
> a long report. Does it make sense to use a lock to
> reduce DB access?
>    
>   Thanks,
>    
>   Lucy
>    
>   
> 
> Aaron Smuts <as...@yahoo.com> wrote:
>   I don't understand what you need a lock for. You
> can
> go get the report, and then put it in the cache
> under
> the same key. You don't need to lock the key. Until
> the new report is ready, client will get the old one
> from the cache. Simply put the new value in the
> cache
> under the smae key name and it will replace the old
> value.
> 
> Aaron
> 
> --- emily chen wrote:
> 
> > Hi Aaron,
> > 
> > I got you. I will implement the lock in my web
> > application.
> > 
> > Many thanks,
> > 
> > Lucy
> > 
> > Aaron Smuts wrote:
> > Of course JCS is Thread safe, if you mean that it
> > can
> > handle multiple threads accessing the same regions
> > at
> > the same time. 
> > 
> > It doesn't work the way you described, but it is
> > thread safe. You won't get a corrupted value for a
> > key. 
> > 
> > As for the memory cache, it is a reference cache.
> It
> > returns references to objects in memory. It does
> not
> > clone the values. If you are manipulating objects
> > that are in the cache, any other Thread could get
> a
> > reference to that same object. . .. It is best to
> > clone objects that are retrieved from the cache
> > prior
> > to modifying them or just don't modify objects
> that
> > have been put in the cache.
> > 
> > Aaron
> > 
> > --- emily chen wrote:
> > 
> > > Hi there,
> > > 
> > > I want to use JCS for my web application, and
> have
> > > some thread safe question.
> > > 
> > > Here is scenario: 
> > > 
> > > I have a JCS cache named
> > �ePINBatchReportCache�?
> > > which is used to save the ePIN Batch Report
> data.
> > > 
> > > Thread 1:
> > > 1) Get report data from Cache for key.
> > > 2) If not data found in the cache, then lock
> > > the cache for that key.
> > > 3) Get report data from database.
> > > 4) Put the report data in cache for key
> > > 5) Release lock for that key.
> > > 
> > > Thread 2:
> > > 1) Get the data from cache for key.
> > > 2) Wait for lock to be released (should have
> > > max wait time here).
> > > 3) Get the data from cache.
> > > 
> > > Does JCS have the above functionality -- lock
> the
> > > key of cache? Or I have to put lock on the key
> in
> > > my web application source code?
> > > 
> > > Thank for your help.
> > > 
> > > Lucy 
> > > 
> > > 
> > > ---------------------------------
> > > Yahoo! Messenger with Voice. Make PC-to-Phone
> > Calls
> > > to the US (and 30+ countries) for 2�?min or
> less.
> > 
> > 
> >
>
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> > jcs-users-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> > jcs-users-help@jakarta.apache.org
> > 
> > 
> > 
> > 
> > ---------------------------------
> > Yahoo! Messenger with Voice. PC-to-Phone calls for
> > ridiculously low rates.
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> jcs-users-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> jcs-users-help@jakarta.apache.org
> 
> 
> 
>  		
> ---------------------------------
> Ring'em or ping'em. Make  PC-to-phone calls as low
> as 1�/min with Yahoo! Messenger with Voice.


---------------------------------------------------------------------
To unsubscribe, e-mail: jcs-users-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jcs-users-help@jakarta.apache.org


Re: JCS thread safe question.

Posted by emily chen <em...@yahoo.com>.
Hi Aaron,
   
  If I don't use a lock, thread 1 gets the report from cache by key, not data found, so it goes to DB to get the report;  before it puts the report into cache, thread 2 accesses the cache by the same key, and can not find the report, so it goes to DB to get the data again. That means two thread will access DB twice. But I want they just acces DB only one time to get the same report.
   
  In my web application, it takes serval mins to run a long report. Does it make sense to use a lock to reduce DB access?
   
  Thanks,
   
  Lucy
   
  

Aaron Smuts <as...@yahoo.com> wrote:
  I don't understand what you need a lock for. You can
go get the report, and then put it in the cache under
the same key. You don't need to lock the key. Until
the new report is ready, client will get the old one
from the cache. Simply put the new value in the cache
under the smae key name and it will replace the old
value.

Aaron

--- emily chen wrote:

> Hi Aaron,
> 
> I got you. I will implement the lock in my web
> application.
> 
> Many thanks,
> 
> Lucy
> 
> Aaron Smuts wrote:
> Of course JCS is Thread safe, if you mean that it
> can
> handle multiple threads accessing the same regions
> at
> the same time. 
> 
> It doesn't work the way you described, but it is
> thread safe. You won't get a corrupted value for a
> key. 
> 
> As for the memory cache, it is a reference cache. It
> returns references to objects in memory. It does not
> clone the values. If you are manipulating objects
> that are in the cache, any other Thread could get a
> reference to that same object. . .. It is best to
> clone objects that are retrieved from the cache
> prior
> to modifying them or just don't modify objects that
> have been put in the cache.
> 
> Aaron
> 
> --- emily chen wrote:
> 
> > Hi there,
> > 
> > I want to use JCS for my web application, and have
> > some thread safe question.
> > 
> > Here is scenario: 
> > 
> > I have a JCS cache named
> �ePINBatchReportCache�?
> > which is used to save the ePIN Batch Report data.
> > 
> > Thread 1:
> > 1) Get report data from Cache for key.
> > 2) If not data found in the cache, then lock
> > the cache for that key.
> > 3) Get report data from database.
> > 4) Put the report data in cache for key
> > 5) Release lock for that key.
> > 
> > Thread 2:
> > 1) Get the data from cache for key.
> > 2) Wait for lock to be released (should have
> > max wait time here).
> > 3) Get the data from cache.
> > 
> > Does JCS have the above functionality -- lock the
> > key of cache? Or I have to put lock on the key in
> > my web application source code?
> > 
> > Thank for your help.
> > 
> > Lucy 
> > 
> > 
> > ---------------------------------
> > Yahoo! Messenger with Voice. Make PC-to-Phone
> Calls
> > to the US (and 30+ countries) for 2�?min or
less.
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> jcs-users-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> jcs-users-help@jakarta.apache.org
> 
> 
> 
> 
> ---------------------------------
> Yahoo! Messenger with Voice. PC-to-Phone calls for
> ridiculously low rates.


---------------------------------------------------------------------
To unsubscribe, e-mail: jcs-users-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jcs-users-help@jakarta.apache.org



 		
---------------------------------
Ring'em or ping'em. Make  PC-to-phone calls as low as 1¢/min with Yahoo! Messenger with Voice.

Re: JCS thread safe question.

Posted by Aaron Smuts <as...@yahoo.com>.
I don't understand what you need a lock for.  You can
go get the report, and then put it in the cache under
the same key.  You don't need to lock the key.  Until
the new report is ready, client will get the old one
from the cache.  Simply put the new value in the cache
under the smae key name and it will replace the old
value.

Aaron

--- emily chen <em...@yahoo.com> wrote:

> Hi Aaron,
>    
>   I got you. I will implement the lock in my web
> application.
>    
>   Many thanks,
>    
>   Lucy
> 
> Aaron Smuts <as...@yahoo.com> wrote:
>   Of course JCS is Thread safe, if you mean that it
> can
> handle multiple threads accessing the same regions
> at
> the same time. 
> 
> It doesn't work the way you described, but it is
> thread safe. You won't get a corrupted value for a
> key. 
> 
> As for the memory cache, it is a reference cache. It
> returns references to objects in memory. It does not
> clone the values. If you are manipulating objects
> that are in the cache, any other Thread could get a
> reference to that same object. . .. It is best to
> clone objects that are retrieved from the cache
> prior
> to modifying them or just don't modify objects that
> have been put in the cache.
> 
> Aaron
> 
> --- emily chen wrote:
> 
> > Hi there,
> > 
> > I want to use JCS for my web application, and have
> > some thread safe question.
> > 
> > Here is scenario: 
> > 
> > I have a JCS cache named
> �ePINBatchReportCache�?
> > which is used to save the ePIN Batch Report data.
> > 
> > Thread 1:
> > 1) Get report data from Cache for key.
> > 2) If not data found in the cache, then lock
> > the cache for that key.
> > 3) Get report data from database.
> > 4) Put the report data in cache for key
> > 5) Release lock for that key.
> > 
> > Thread 2:
> > 1) Get the data from cache for key.
> > 2) Wait for lock to be released (should have
> > max wait time here).
> > 3) Get the data from cache.
> > 
> > Does JCS have the above functionality -- lock the
> > key of cache? Or I have to put lock on the key in
> > my web application source code?
> > 
> > Thank for your help.
> > 
> > Lucy 
> > 
> > 
> > ---------------------------------
> > Yahoo! Messenger with Voice. Make PC-to-Phone
> Calls
> > to the US (and 30+ countries) for 2�?min or
less.
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> jcs-users-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> jcs-users-help@jakarta.apache.org
> 
> 
> 
>  		
> ---------------------------------
> Yahoo! Messenger with Voice. PC-to-Phone calls for
> ridiculously low rates.


---------------------------------------------------------------------
To unsubscribe, e-mail: jcs-users-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jcs-users-help@jakarta.apache.org


Re: JCS thread safe question.

Posted by emily chen <em...@yahoo.com>.
Hi Aaron,
   
  I got you. I will implement the lock in my web application.
   
  Many thanks,
   
  Lucy

Aaron Smuts <as...@yahoo.com> wrote:
  Of course JCS is Thread safe, if you mean that it can
handle multiple threads accessing the same regions at
the same time. 

It doesn't work the way you described, but it is
thread safe. You won't get a corrupted value for a
key. 

As for the memory cache, it is a reference cache. It
returns references to objects in memory. It does not
clone the values. If you are manipulating objects
that are in the cache, any other Thread could get a
reference to that same object. . .. It is best to
clone objects that are retrieved from the cache prior
to modifying them or just don't modify objects that
have been put in the cache.

Aaron

--- emily chen wrote:

> Hi there,
> 
> I want to use JCS for my web application, and have
> some thread safe question.
> 
> Here is scenario: 
> 
> I have a JCS cache named
�ePINBatchReportCache�
> which is used to save the ePIN Batch Report data.
> 
> Thread 1:
> 1) Get report data from Cache for key.
> 2) If not data found in the cache, then lock
> the cache for that key.
> 3) Get report data from database.
> 4) Put the report data in cache for key
> 5) Release lock for that key.
> 
> Thread 2:
> 1) Get the data from cache for key.
> 2) Wait for lock to be released (should have
> max wait time here).
> 3) Get the data from cache.
> 
> Does JCS have the above functionality -- lock the
> key of cache? Or I have to put lock on the key in
> my web application source code?
> 
> Thank for your help.
> 
> Lucy 
> 
> 
> ---------------------------------
> Yahoo! Messenger with Voice. Make PC-to-Phone Calls
> to the US (and 30+ countries) for 2ï¿?min or less.


---------------------------------------------------------------------
To unsubscribe, e-mail: jcs-users-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jcs-users-help@jakarta.apache.org



 		
---------------------------------
Yahoo! Messenger with Voice. PC-to-Phone calls for ridiculously low rates.

Re: JCS thread safe question.

Posted by Aaron Smuts <as...@yahoo.com>.
Of course JCS is Thread safe, if you mean that it can
handle multiple threads accessing the same regions at
the same time.  

It doesn't work the way you described, but it is
thread safe.  You won't get a corrupted value for a
key.   

As for the memory cache, it is a reference cache.  It
returns references to objects in memory.  It does not
clone the values.  If you are manipulating objects
that are in the cache, any other Thread could get a
reference to that same object.  . ..  It is best to
clone objects that are retrieved from the cache prior
to modifying them or just don't modify objects that
have been put in the cache.

Aaron

--- emily chen <em...@yahoo.com> wrote:

> Hi there,
>    
>   I want to use JCS for my web application, and have
> some thread safe question.
>    
>   Here is scenario: 
>    
>   I have a JCS cache named
�ePINBatchReportCache�,
> which is used to save the ePIN Batch Report data.
>    
>   Thread 1:
>   1)      Get report data from Cache for key.
>   2)      If not data found in the cache, then lock
> the cache for that key.
>   3)      Get report data from database.
>   4)      Put the report data in cache for key
>   5)      Release lock for that key.
>    
>   Thread 2:
>   1)      Get the data from cache for key.
>   2)      Wait for lock to be released (should have
> max wait time here).
>   3)      Get the data from cache.
>    
>   Does JCS have the above functionality -- lock the
> key of cache?  Or I have to put lock on the key in
> my web application source code?
>    
>   Thank for your help.
>    
>   Lucy 
> 
>  		
> ---------------------------------
> Yahoo! Messenger with Voice. Make PC-to-Phone Calls
> to the US (and 30+ countries) for 2�/min or less.


---------------------------------------------------------------------
To unsubscribe, e-mail: jcs-users-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jcs-users-help@jakarta.apache.org