[odb-users] Optimal way of checking if DB entry exists before persisting?

Szumowski, Thomas thomas.szumowski at lmco.com
Thu Jun 23 19:30:17 EDT 2011


Suppose I have have an "employee" object that as a member has a pointer to an "employer" object as in the one-way relationship example from the user guide. I am periodically receiving "employee" objects from an external system and would like to persist the employees in one table and corresponding employers in another (as in the example).

If I blindly try to persist the employer (before attempting to persist employee), and an object with that employer ID already exists in the database, ODB throws an error stating the object is already persisted. As an alternative, upon receipt of an employee object, I can do the following:

- open transaction
- Get employer ID from received employee object
- Query the database to see if this employer ID exists
                - If it does, then call db->update(..)
                - If it doesn't, then call db->persist(..)
- commit

This procedure is similar to that outlined at the end of section 2.6 from the manual.

My question are:
(1) Is this the most efficient way of updating/persisting objects that may already exist in the DB?

(2) Since the objects come in periodically, is there a more efficient way of persisting each employee to the database? I assume if the data rate is high, doing commits so often may be inefficient and it may be better to commit based off some timer periodically and cache. (Is there a similar feature in ODB?)

Thank you for your help,

-Tom


Tom Szumowski
Lockheed Martin - Advanced Technology Laboratories
Phone: (856) 792-9077
E-mail:  thomas.szumowski at lmco.com<mailto:thomas.szumowski at lmco.com>



More information about the odb-users mailing list