Re: AW: creating/saving transient objects to database

From: Craig L Russell (Craig.Russel..un.COM)
Date: Fri Jun 22 2007 - 17:43:35 EDT

  • Next message: Aristedes Maniatis: "Re: paged query slow when fetching big lists"

    On Jun 22, 2007, at 12:30 AM, Peter Schröder wrote:

    > this approach has the downside that you may run into concurrency
    > issues (check than act).
    > you may get a duplicate even if you first check for an existing entry.
    > this is really tricky in webapplications...

    You can overcome this by running the transaction in serializable
    mode. This will put read locks on ranges to prevent other
    transactions from inserting records that you are inserting after
    querying.

    Craig
    >
    > kind regards,
    > peter
    >
    > -----Ursprüngliche Nachricht-----
    > Von: Fredrik Liden [mailto:flide..ranslate.com]
    > Gesendet: Freitag, 22. Juni 2007 00:10
    > An: use..ayenne.apache.org
    > Betreff: RE: creating/saving transient objects to database
    >
    > If you're calling newObject you'll get a registered object with
    > PersistenceState.NEW. so consequently the commit tries to insert it.
    >
    > I'm thinking at some point you'll need to manually check if the object
    > exists in the database or not based on some criteria. Maybe you can
    > create a service class that checks using a query if the Pojo object
    > exists in the db. If it does it returns the existing DataObject and if
    > not it returns the object using newObject. Then the persistenceState
    > will be either NEW or COMMITED and commit will trigger insert and
    > update
    > respectively.
    >
    > Of course the Cayenne guys probably have some other nifty way to do
    > it.
    >
    > Fredrik
    >
    >
    >
    > -----Original Message-----
    > From: Michael Lepine [mailto:mikelepin..mail.com]
    > Sent: Thursday, June 21, 2007 2:46 PM
    > To: use..ayenne.apache.org
    > Subject: creating/saving transient objects to database
    >
    > I've got a situation where I've got strict POJO objects that I'll need
    > to
    > copy data from and into my generated Cayenne classes. My issue is that
    > when
    > I copy the data from the bean to the Cayenne class, I don't know
    > whether
    > the
    > object exists or not. Thus, I create the Cayenne class instance using
    > DataContext.newObject(). Obviously, when I call
    > DataContext.commitChanges(),
    > an insert is being attempted on the corresponding table even if a
    > record
    > already exists in the database.
    >
    > Is there a way to create the Cayenne instance so that the persistent
    > layer
    > will know to check whether the record exists and update it instead of
    > always
    > attempting an insert?
    >
    > Any help and guidance are appreciated.

    Craig Russell
    Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
    408 276-5638 mailto:Craig.Russel..un.com
    P.S. A good JDO? O, Gasp!





    This archive was generated by hypermail 2.0.0 : Fri Jun 22 2007 - 17:44:07 EDT