You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by "Barnett, Brian W." <bb...@scholarinc.com> on 2004/12/17 06:49:55 UTC

best practice for using ibatis cached objects

Let me see if I understand this correctly...

I get a list object back from a call to executeQueryForList(). I have
caching enabled in ibatis, so ibatis caches the list, or a reference to it.
As long as the cache has not been flushed, if I make another call to the
same mapped statement using executeQueryForList(), it returns the same list,
not a separate copy of the list but the same one as before.

I then modify the contents of this list, but nothing has been updated in the
database yet. Another user logs into the system and calls that same mapped
statement. He will get the list that has the modifications made by the first
user, even though nothing has been updated in the database. Is this correct?
I think this is the behavior I'm seeing.

So, what is best practice to avoid this? Should I make deep copies of all
objects returned from ibatis if I am using caching? What are my options?

Thanks,
Brian Barnett

Re: best practice for using ibatis cached objects

Posted by Brandon Goodin <br...@gmail.com>.
Page 30 of the SqlMaps manual states:

Read-Only vs. Read/Write

The framework supports both read-only and read/write caches. Read-only
caches are shared among all users and therefore offer greater
performance benefit. However, objects read from a read-only cache
should not be modified. Instead, a new object should be read from the
database (or a read/write cache) for updating. On the other hand, if
there is an intention to use objects for retrieval and modification, a
read/write cache is recommended (i.e. required). To use a read-only
cache, set readOnly="true" on the cache model element. To use a
read/write cache, set readOnly="false". The default is read-only
(true).


Serializable Read/Write Caches

As you may agree, caching per-session as described above may offer
little benefit to global application performance. Another type of
read/write cache that can offer a performance benefit to the entire
application (i.e. not just per session) is a serializable read/write
cache. This cache will return different instances (copies) of the
cached object to each session. Therefore each session can safely
modify the instance returned. Realize the difference in semantics
here, usually you would expect the same instance to be returned from a
cache, but in this case you'll get a different one. Also note that
every object stored by a serializable cache must be serializable. This
means that you will have difficulty using both lazy loading features
combined with a serializable cache, because lazy proxies are not
serializable. The best way to figure out what combination of caching,
lazy loading and table joining is simply to try it out. To use a
serializable cache, set readOnly="false" and serialize="true". By
default cache models are read-only and non-serializable. Read-only
caches will not be serialized (there's no benefit).

Hope that helps,
Brandon

On Thu, 16 Dec 2004 23:49:55 -0600, Barnett, Brian W.
<bb...@scholarinc.com> wrote:
> Let me see if I understand this correctly...
> 
> I get a list object back from a call to executeQueryForList(). I have
> caching enabled in ibatis, so ibatis caches the list, or a reference to it.
> As long as the cache has not been flushed, if I make another call to the
> same mapped statement using executeQueryForList(), it returns the same list,
> not a separate copy of the list but the same one as before.
> 
> I then modify the contents of this list, but nothing has been updated in the
> database yet. Another user logs into the system and calls that same mapped
> statement. He will get the list that has the modifications made by the first
> user, even though nothing has been updated in the database. Is this correct?
> I think this is the behavior I'm seeing.
> 
> So, what is best practice to avoid this? Should I make deep copies of all
> objects returned from ibatis if I am using caching? What are my options?
> 
> Thanks,
> Brian Barnett
>