Hi Fabricio,
This is a very common situation indeed. You have some lookup tables that
you want to reuse across contexts. While a better solution will likely not
come out before 1.1 or something, here is a trick that I am using right
now. I would create a "shared" DataContext, explicitly instantiated
somewhere at the application level, and never used directly by sessions.
All the read only "static" data is fetched via this context and stored
somewhere.
Every time an object or a list of objects is needed in a regular
DataContext (say to use in the relationship with another object), I create
a local copy of an object from "shared" DataContext. This may not be
extremely efficient, but it is of course *much* faster than refetching
same objects over and over again. In fact it work extremely well in the
project where I am using it. Here is a code sample that transfers a list
of objects to a given DataContext:
public static List getLocalObjects(DataContext context, List objects) {
List localList = new ArrayList(objects.size());
Iterator it = objects.iterator();
while (it.hasNext()) {
DataObject obj = (DataObject) it.next();
if (obj.getDataContext() != context) {
ObjEntity ent =
context.getEntityResolver().lookupObjEntity(
obj.getObjectId().getObjClass());
obj =
context.objectFromDataRow(
ent,
obj.getCurrentSnapshot(),
true);
}
localList.add(obj);
}
return localList;
}
Andrus
> Hi,
>
> I have a question about caching objects that I need some help.
>
> Lets suppose I have a table called Country and read-only objects
> mapped to this table. As they are very unlikely to change, I'd like to
> cache them somewhere instead of going to the DB everytime I need to use
> (display, associate, etc) them. A static Map name->object will do it.
> The problem is that every context has its own cache that keeps its own
> copies of objects. So the question is, can I load and store all Country
> objects using Context A and then assign one of them to an Address object
> on Context B? BTW, does the cache use equality and identity?
> In this case, it is not a problem if loading the Address from the DB
>
> on Context C creates another copy, different than the one in the cache,
> because they are all read-only.
>
>
> Any help is very much appreciated,
>
> --
> Fabricio Voznika
> Senior Programmer/Analyst
> Administrative Computing
> Dartmouth College
> Phone: 603-646-2007
This archive was generated by hypermail 2.0.0 : Thu Jul 10 2003 - 13:04:50 EDT