Re: Some doubts about DataContext and memory allocation

From: mr.abanjo (mr.abanj..mail.com)
Date: Sun May 23 2010 - 07:25:40 UTC

  • Next message: Gary Jarrel: "Maven2 cgen task problem"

    Thanks Mike and Michael!
    As usual, you are very kind.

    So, there are a lot of "strategy" using datacontext, but the singleton is
    the bad one :-)
    The write operation i perfrom in my site is very simple. It's just an insert
    of two fields in a single table. So maybe also singleton should work, but
    i'm agree with you.. in this way any commit or rollback operations are out
    of control if i don't "connect" them almost with a user session.

    Thanks again!
    Davide

    On Sat, May 22, 2010 at 2:09 PM, Mike Kienenberger <mkienen..mail.com>wrote:

    > Note that just because there are multiple data contexts doesn't mean
    > you can't share the object cache. By default, they are shared across
    > the application. A data context is typically a subset of your total
    > fetched objects -- those which are you are making specific changes to
    > as a group.
    >
    > http://cayenne.apache.org/doc30/caching-and-fresh-data.html
    > http://cayenne.apache.org/doc30/individual-object-caching.html
    >
    > Also, using a per-session datacontext isn't the "right way". It's
    > just one of many ways. I've used per-request data contexts, and even
    > per-method data contexts. It mostly depends on the length of the
    > scope of your database operations -- when you start modifying the data
    > and when you commit it. You'll want your data context to live for
    > however long that length of time is.
    >
    > On Sat, May 22, 2010 at 7:50 AM, Michael Gentry <mgentr..asslight.net>
    > wrote:
    > > Hi Davide,
    > >
    > > 1) Unless your application is read-mostly, and it doesn't sound like it
    > is,
    > > you'll want separate DataContexts per user to track their individual
    > changes
    > > in distinct DataContexts. Otherwise the changes will get co-mingled and
    > > chaos likely will ensue.
    > >
    > > 2) You should be fine, but you can always fine-tune later if required by
    > > kicking objects out of the DataContext yourself where it makes sense. I'd
    > > optimize this later, though, as it may not be needed.
    > >
    > > mrg
    > >
    > >
    > > On May 22, 2010, at 5:03 AM, "mr.abanjo" <mr.abanj..mail.com> wrote:
    > >
    > >> Hi,
    > >> I'm developing a simple web application that store in a table an user
    > >> preference.
    > >> When a new user comes, i save a new record to a database using
    > >> datacontext:
    > >> *
    > >> *
    > >>
    > >> *datacontext.registerNewObject<
    > http://cayenne.apache.org/doc30/api/org/apache/cayenne/access/DataContext.html#registerNewObject(java.lang.Object)
    > >
    > >>
    > >> *(Object<
    > http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html?is-external=true
    > >
    > >> myobject);
    > >>
    > >> *datacontext.**commitChanges<
    > http://cayenne.apache.org/doc30/api/org/apache/cayenne/access/DataContext.html#commitChanges()
    > >
    > >> *();
    > >>
    > >> Cayenne perform an insert and all works fine.
    > >> I have 2 questions:
    > >>
    > >> 1) Reading the documentation the right way in a web application to
    > create
    > >> a
    > >> datacontext is per session:
    > >> http://cayenne.apache.org/doc30/obtaining-datacontext.html
    > >> What happens if i treat it as a singleton? ( So i have one instance for
    > >> all
    > >> the users )
    > >>
    > >> 2) My web application has a high traffic, so i think that in a few time
    > >> i've
    > >> "registered" in datacontext thousands of record. This objects are
    > >> correctly
    > >> garbage collected or they stay in memory?
    > >> Reading the documentation:
    > >>
    > >> Since Cayenne 3.0, by default DataContext uses weak references to store
    > >> registered objects. So objects are allowed to be garbage collected by
    > the
    > >> VM
    > >> if they are not referenced elsewhere in the application. "Elsewhere"
    > >> usually
    > >> means one of the following:
    > >>
    > >> - An object is directly or indirectly referenced by the application.
    > [*NO
    > >> reference to them by any object in my application]*
    > >> - An object is a part of the cached query result stored by Cayenne. [*I
    > >> never configured cayenne for cache.. so if i'm not wrong, the default
    > is
    > >> NO-CACHE]*
    > >> - An object is "dirty" (i.e. new, modified or deleted). In this case
    > >> Cayenne ensures that such object is retained at least until commit or
    > >> rollback. [*I perform always a commit after registering a new object]*
    > >>
    > >> So... i think that the memory should be correctly freed.
    > >>
    > >> N.B. I'm using Cayenne 3.0!
    > >>
    > >> Thanks for help!
    > >> Davide
    > >
    >



    This archive was generated by hypermail 2.0.0 : Sun May 23 2010 - 07:26:16 UTC