You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Paul Timmins <pt...@hotmail.com> on 2004/08/04 17:48:35 UTC
Stale information in Cache when Inserting w/ 1:N's
When I add an object on the "one" side of a 1:N relationship (ie: adding a
book), the cached "many" (ie: a library) side is out of synch.
In pseudocode:
1. Access Library 1 and count the number of books (it's now cached)
2. Create a new book, set the book.library_id=1
3. PersistenceBroker.store(book) (book is now inserted)
4. Count the number of books in Library 1
Problem! In step 4, the number of books has not been updated... it's the
cached value from step 1, not the correct value.
What is the proper way to deal with this issue? Are we supposed to flush
the cache on every write? Or, manually update all the related objects? Or
what?
PS: In step 2, I could set the book.library object instead of the FK. Same
result.
--------------------
Scenario:
Let's say I have two tables Library and Book, with a FK to Library in Book
(therefore, this is a 1:N from Library to books)
Data objects:
public class Library {
Integer id; // PK
Collection books; // lazy loaded (Proxy=true)
}
public class Book {
Integer id;
Integer library_id; // FK to Library
Library library;
}
Repository_User.xml:
<class-descriptor
class="Book"
table="book">
<field-descriptor
name="id"
column="id"
jdbc-type="INTEGER"
primarykey="true"
autoincrement="true"
sequence-name="bookid"
/>
<field-descriptor
name="library_id"
column="library_id"
jdbc-type="INTEGER"
/>
<field-descriptor
name="name"
column="name"
jdbc-type="VARCHAR"
/>
<reference-descriptor
name="Library"
class-ref="library"
auto-retrieve="true"
auto-update="link"
auto-delete="false">
<foreignkey field-ref="library_id" />
</reference-descriptor>
</class-descriptor>
<class-descriptor
class="Library"
table="library">
<field-descriptor
name="id"
column="id"
jdbc-type="INTEGER"
primarykey="true"
autoincrement="true"
sequence-name="libraryid"
/>
<field-descriptor
name="name"
column="name"
jdbc-type="VARCHAR"
/>
<collection-descriptor
name="collbooks"
element-class-ref="Book"
auto-retrieve="true"
auto-update="link"
auto-delete="false"
proxy="false">
<inverse-foreignkey field-ref="library_id" />
</collection-descriptor>
</class-descriptor>
_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: Stale information in Cache when Inserting w/ 1:N's
Posted by Armin Waibel <ar...@apache.org>.
Hi Paul,
I assume you use ObjectCacheDefaultImpl for object caching. This
implementation is really simple and cache complete object graph (object
with all dependencies). So if you add a new book and lookup the library
object from OJB, the library will be found in cache, therefore the
cached books in the library object will not be updated - until the
cached library is timed out or removed from cache.
If you don't want this behavior you can change the used ObjectCache
implementation, e.g. use ObjectCachePerBrokerImpl. Or easiest way, set
the 'refresh' attribute of the library class collection-descriptor to
'true'.
http://db.apache.org/ojb/docu/guides/repository.html#collection-descriptor-N1059C
regards,
Armin
Paul Timmins wrote:
> When I add an object on the "one" side of a 1:N relationship (ie: adding
> a book), the cached "many" (ie: a library) side is out of synch.
>
> In pseudocode:
> 1. Access Library 1 and count the number of books (it's now cached)
> 2. Create a new book, set the book.library_id=1
> 3. PersistenceBroker.store(book) (book is now inserted)
> 4. Count the number of books in Library 1
>
> Problem! In step 4, the number of books has not been updated... it's
> the cached value from step 1, not the correct value.
>
> What is the proper way to deal with this issue? Are we supposed to
> flush the cache on every write? Or, manually update all the related
> objects? Or what?
>
> PS: In step 2, I could set the book.library object instead of the FK.
> Same result.
>
> --------------------
> Scenario:
> Let's say I have two tables Library and Book, with a FK to Library
> in Book (therefore, this is a 1:N from Library to books)
>
> Data objects:
> public class Library {
> Integer id; // PK
> Collection books; // lazy loaded (Proxy=true)
> }
>
> public class Book {
> Integer id;
> Integer library_id; // FK to Library
> Library library;
> }
>
> Repository_User.xml:
> <class-descriptor
> class="Book"
> table="book">
> <field-descriptor
> name="id"
> column="id"
> jdbc-type="INTEGER"
> primarykey="true"
> autoincrement="true"
> sequence-name="bookid"
> />
> <field-descriptor
> name="library_id"
> column="library_id"
> jdbc-type="INTEGER"
> />
> <field-descriptor
> name="name"
> column="name"
> jdbc-type="VARCHAR"
> />
> <reference-descriptor
> name="Library"
> class-ref="library"
> auto-retrieve="true"
> auto-update="link"
> auto-delete="false">
> <foreignkey field-ref="library_id" />
> </reference-descriptor>
> </class-descriptor>
>
> <class-descriptor
> class="Library"
> table="library">
> <field-descriptor
> name="id"
> column="id"
> jdbc-type="INTEGER"
> primarykey="true"
> autoincrement="true"
> sequence-name="libraryid"
> />
> <field-descriptor
> name="name"
> column="name"
> jdbc-type="VARCHAR"
> />
> <collection-descriptor
> name="collbooks"
> element-class-ref="Book"
> auto-retrieve="true"
> auto-update="link"
> auto-delete="false"
> proxy="false">
> <inverse-foreignkey field-ref="library_id" />
> </collection-descriptor>
> </class-descriptor>
>
> _________________________________________________________________
> Express yourself instantly with MSN Messenger! Download today - it's
> FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org