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

java.lang.Object
  extended byorg.apache.ojb.broker.util.sequence.AbstractSequenceManager
      extended byorg.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,v 1.6 2003/08/13 18:16:10 arminw Exp $
Author:
Ryan Vanderwerf, Edson Carlos Ericksson Richter, Rajeev Kaul, Thomas Mahler, Armin Waibel

Field Summary
protected static java.lang.String PROCEDURE_NAME
           
protected static java.lang.String SEQ_ID_STRING
           
protected static java.lang.String SEQ_NAME_STRING
           
protected static java.lang.String SEQ_TABLE_NAME
           
 
Fields inherited from class org.apache.ojb.broker.util.sequence.AbstractSequenceManager
PROPERTY_AUTO_NAMING
 
Constructor Summary
SequenceManagerStoredProcedureImpl(PersistenceBroker broker)
          Constructor
 
Method Summary
protected  long buildNextSequence(PersistenceBroker broker, ClassDescriptor cld, java.lang.String sequenceName)
          Calls the stored procedure stored procedure throws an error if it doesn't exist.
protected  void createSequence(PersistenceBroker broker, FieldDescriptor field, java.lang.String sequenceName, long maxKey)
          Creates new row in table
protected  long getUniqueLong(FieldDescriptor field)
          Gets the actual key Will create a new row with the max key of table if it does not exist.
protected  java.lang.String sp_createSequenceQuery(java.lang.String sequenceName, long maxKey)
          Insert syntax for our special table
 
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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROCEDURE_NAME

protected static final java.lang.String PROCEDURE_NAME
See Also:
Constant Field Values

SEQ_NAME_STRING

protected static final java.lang.String SEQ_NAME_STRING
See Also:
Constant Field Values

SEQ_ID_STRING

protected static final java.lang.String SEQ_ID_STRING
See Also:
Constant Field Values

SEQ_TABLE_NAME

protected static final java.lang.String SEQ_TABLE_NAME
See Also:
Constant Field Values
Constructor Detail

SequenceManagerStoredProcedureImpl

public SequenceManagerStoredProcedureImpl(PersistenceBroker broker)
Constructor

Parameters:
broker -
Method Detail

sp_createSequenceQuery

protected java.lang.String sp_createSequenceQuery(java.lang.String sequenceName,
                                                  long maxKey)
Insert syntax for our special table

Parameters:
sequenceName -
maxKey -
Returns:

getUniqueLong

protected long getUniqueLong(FieldDescriptor field)
                      throws SequenceManagerException
Gets the actual key Will create a new row with the max key of table if it does not exist.

Specified by:
getUniqueLong in class AbstractSequenceManager
Parameters:
field -
Returns:
Throws:
SequenceManagerException

buildNextSequence

protected long buildNextSequence(PersistenceBroker broker,
                                 ClassDescriptor cld,
                                 java.lang.String sequenceName)
                          throws LookupException,
                                 java.sql.SQLException,
                                 PlatformException
Calls the stored procedure stored procedure throws an error if it doesn't exist.

Parameters:
broker -
cld -
sequenceName -
Returns:
Throws:
LookupException
java.sql.SQLException
PlatformException

createSequence

protected void createSequence(PersistenceBroker broker,
                              FieldDescriptor field,
                              java.lang.String sequenceName,
                              long maxKey)
                       throws java.lang.Exception
Creates new row in table

Parameters:
broker -
field -
sequenceName -
maxKey -
Throws:
java.lang.Exception


Authors: Thomas Mahler and others. (C) 2000 - 2003 Apache Software Foundation
All rights reserved. Published under the Apache License.
http://db.apache.org/ojb
Version: 1.0.rc5, 2003-12-14