Auto increment with a Unit Of Work

Posted by Derick on Stack Overflow See other posts from Stack Overflow or by Derick
Published on 2010-04-21T20:40:50Z Indexed on 2010/04/21 20:43 UTC
Read the original article Hit count: 243

Filed under:
|
|

Context

I'm building a persistence layer to abstract different types of databases that I'll be needing. On the relational part I have mySQL, Oracle and PostgreSQL.

Let's take the following simplified MySQL tables:

CREATE TABLE Contact (
  ID varchar(15),
  NAME varchar(30)
);

CREATE TABLE Address (
  ID varchar(15),
  CONTACT_ID varchar(15),
  NAME varchar(50)
);

I use code to generate system specific alpha numeric unique ID's fitting 15 chars in this case. Thus, if I insert a Contact record with it's Addresses I have my generated Contact.ID and Address.CONTACT_IDs before committing.

I've created a Unit of Work (amongst others) as per Martin Fowler's patterns to add transaction support. I'm using a key based Identity Map in the UoW to track the changed records in memory. It works like a charm for the scenario above, all pretty standard stuff so far.

The question scenario comes in when I have a database that is not under my control and the ID fields are auto-increment (or in Oracle sequences). In this case I do not have the db generated Contact.ID beforehand, so when I create my Address I do not have a value for Address.CONTACT_ID. The transaction has not been started on the DB session since all is kept in the Identity Map in memory.

Question: What is a good approach to address this? (Avoiding unnecessary db round trips)

Some ideas: Retrieve the last ID: I can do a call to the database to retrieve the last Id like:

SELECT Auto_increment FROM information_schema.tables WHERE table_name='Contact'; 

But this is MySQL specific and probably something similar can be done for the other databases. If do this then would need to do the 1st insert, get the ID and then update the children (Address.CONTACT_IDs) – all in the current transaction context.

© Stack Overflow or respective owner

Related posts about qt4

Related posts about design-patterns