org.apache.ojb.broker.util.sequence
Class SequenceManagerStoredProcedureImpl

java.lang.Object
  extended by org.apache.ojb.broker.util.sequence.AbstractSequenceManager
      extended by org.apache.ojb.broker.util.sequence.SequenceManagerStoredProcedureImpl
All Implemented Interfaces:
SequenceManager

public class SequenceManagerStoredProcedureImpl
extends AbstractSequenceManager

This solution will give those seeking an oracle-style sequence generator a final answer (Identity columns really suck).
The SequenceManagerStoredProcedureImpl implementation enabled database sequence key generation for all databases (e.g. MSSQL, MySQL, DB2, ...) with a JDBC 2.0 compliant driver.
First add a new table OJB_NEXTVAL_SEQ to your database.

 CREATE TABLE OJB_NEXTVAL_SEQ
 (
     SEQ_NAME    VARCHAR(150) NOT NULL,
     MAX_KEY     BIGINT,
     CONSTRAINT SYS_PK_OJB_NEXTVAL_SEQ PRIMARY KEY(SEQ_NAME)
 )
 
You will also need the stored procedure OJB_NEXTVAL will will take care of giving you a guaranteed unique sequence number, in multi server environments.
 CREATE PROCEDURE ojb_nextval_proc @SEQ_NAME varchar(100)
              AS
                declare @MAX_KEY BIGINT
              -- return an error if sequence does not exist
              -- so we will know if someone truncates the table
              set @MAX_KEY = 0

              UPDATE OJB_NEXTVAL_SEQ
              SET    @MAX_KEY = MAX_KEY = MAX_KEY + 1
              WHERE  SEQ_NAME = @SEQ_NAME

                if @MAX_KEY = 0
                        select 1/0
                else
                        select @MAX_KEY

              RETURN @MAX_KEY
 

It is possible to define a sequence-name field-descriptor attribute in the repository file. If such an attribute was not found, the implementation build an extent aware sequence name by its own.
Keep in mind when define a sequence name, that you are responsible to be aware of extents, that is: if you ask for an uid for an interface with several implementor classes, or a baseclass with several subclasses the returned uid have to be unique accross all tables representing objects of the extent in question. Thus you have to use the same sequence-name for all extents.

Implementation configuration properties:

Property Key Property Values
autoNaming Default was 'true'. If set 'true' OJB try to build a sequence name automatic if none found in field-descriptor and set this generated name as sequence-name in field-descriptor. If set 'false' OJB throws an exception if none sequence name was found in field-descriptor.

Limitations:



Version:
$Id: SequenceManagerStoredProcedureImpl.java 365232 2005-12-21 22:36:07Z tomdz $
Author:
Ryan Vanderwerf, Edson Carlos Ericksson Richter, Rajeev Kaul, Thomas Mahler, Armin Waibel

Field Summary
 
Fields inherited from class org.apache.ojb.broker.util.sequence.AbstractSequenceManager
PROPERTY_AUTO_NAMING
 
Constructor Summary
SequenceManagerStoredProcedureImpl(PersistenceBroker broker)
          Constructor
 
Method Summary
 
Methods inherited from class org.apache.ojb.broker.util.sequence.AbstractSequenceManager
afterStore, calculateSequenceName, getBrokerForClass, getConfigurationProperties, getConfigurationProperty, getPlatform, getUniqueValue, setConfigurationProperties, setConfigurationProperty, setReferenceFKs, useAutoNaming
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SequenceManagerStoredProcedureImpl

public SequenceManagerStoredProcedureImpl(PersistenceBroker broker)
Constructor

Parameters:
broker -


(C) 2002 - 2006 Apache Software Foundation
All rights reserved. Published under the Apache License 2.0.
http://db.apache.org/ojb
Version: 1.0.4, 2005-12-30