db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: jakarta-ojb/src/java/org/apache/ojb/broker/util/sequence SequenceManagerNextValImpl.java SequenceManagerHighLowImpl.java SequenceManagerOracleImpl.java
Date Sun, 22 Dec 2002 17:12:23 GMT
arminw      2002/12/22 09:12:23

  Modified:    src/test/org/apache/ojb repository_junit.xml repository.dtd
               src/test/org/apache/ojb/broker/sequence
                        SequenceManagerTest.java
               src/java/org/apache/ojb/broker/util/sequence
                        SequenceManagerOracleImpl.java
  Added:       src/test/org/apache/ojb/broker/sequence
                        SMDatabaseSequence.java
               src/java/org/apache/ojb/broker/util/sequence
                        SequenceManagerNextValImpl.java
                        SequenceManagerHighLowImpl.java
  Log:
  improve sequence key generation:
  simplfiy H/L implementation,
  add common database sequence based SMImpl
  
  Revision  Changes    Path
  1.31      +30 -6     jakarta-ojb/src/test/org/apache/ojb/repository_junit.xml
  
  Index: repository_junit.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/test/org/apache/ojb/repository_junit.xml,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- repository_junit.xml	16 Dec 2002 18:17:41 -0000	1.30
  +++ repository_junit.xml	22 Dec 2002 17:12:22 -0000	1.31
  @@ -121,7 +121,7 @@
            auto-delete="false"
         >
            <orderby name="articleId" sort="DESC"/>
  -         
  +
            <inverse-foreignkey field-id-ref="4"/>
         </collection-descriptor>
      </class-descriptor>
  @@ -2839,10 +2839,6 @@
           />
       </class-descriptor>
   
  -    <!-- ************************************************* -->
  -    <!--      END  -  Sequence manager test classes        -->
  -    <!-- ************************************************* -->
  -
   
       <!-- Definitions for org.apache.ojb.broker.sequence.SMMaxBB -->
       <class-descriptor
  @@ -2869,7 +2865,7 @@
            jdbc-type="VARCHAR"
            primarykey="true"
            autoincrement="true"
  -         sequence-name="sequence-name-test"
  +         sequence-name="TEST_SEQUENCE"
           />
   
           <field-descriptor id="4"
  @@ -2888,5 +2884,33 @@
            autoincrement="true"
           />
       </class-descriptor>
  +
  +
  +<!-- Definitions for org.apache.ojb.broker.sequence.SMDatabaseSequence -->
  +    <class-descriptor
  +      class="org.apache.ojb.broker.sequence.SMDatabaseSequence"
  +      table="SM_TAB_DATABASE_SEQUENCE"
  +    >
  +        <field-descriptor id="1"
  +         name="seqId"
  +         column="SEQ_ID"
  +         jdbc-type="INTEGER"
  +         primarykey="true"
  +         autoincrement="true"
  +         sequence-name="TEST_SEQUENCE"
  +        />
  +
  +        <field-descriptor id="2"
  +         name="name"
  +         column="NAME"
  +         jdbc-type="VARCHAR"
  +        />
  +    </class-descriptor>
  +
  +
  +
  +    <!-- ************************************************* -->
  +    <!--      END  -  Sequence manager test classes        -->
  +    <!-- ************************************************* -->
   
   <!-- Mapping of classes used in junit tests and tutorials ends here -->
  
  
  
  1.23      +40 -33    jakarta-ojb/src/test/org/apache/ojb/repository.dtd
  
  Index: repository.dtd
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/test/org/apache/ojb/repository.dtd,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- repository.dtd	22 Dec 2002 15:59:44 -0000	1.22
  +++ repository.dtd	22 Dec 2002 17:12:22 -0000	1.23
  @@ -5,7 +5,8 @@
     -->
   
   <!--
  -	The descriptor-repository is the root element of a repository.xml file.
  +	The descriptor-repository is the root element of a
  +    repository.xml file.
   	It consists of one jdbc-connection-descriptor and at least one
   	class-descriptor element.
   
  @@ -20,13 +21,15 @@
       The isolation attribute defines the default isolation level for
       class-descriptor that do not define a specific isolation level.
   
  -	The version attribute is used to bind a repository.xml file to a given
  -	version of this dtd. This will help to avoid versions conflicts.
  +	The version attribute is used to bind a repository.xml
  +    file to a given version of this dtd. This will help to
  +    avoid versions conflicts.
     -->
   <!ATTLIST descriptor-repository
   	version (0.9.8) #REQUIRED
  -	isolation-level (read-uncommitted | read-committed | repeatable-read |
  -	                 serializable | optimistic) "read-uncommitted"
  +	isolation-level (read-uncommitted | read-committed |
  +                    repeatable-read | serializable |
  +                    optimistic) "read-uncommitted"
   >
   
   <!--
  @@ -46,22 +49,24 @@
     -->
   <!ELEMENT jdbc-connection-descriptor (documentation?, connection-pool?)>
   <!--
  -	The platform attribute is used to define the specific RDBMS Platform.
  -	This attribute corresponds to a
  +	The platform attribute is used to define the specific
  +    RDBMS Platform. This attribute corresponds to a
   	org.apache.ojb.broker.platforms.PlatformXXXImpl class.
   
   	The jdbc-level attribute is used to specify the Jdbc compliance
   	level of the used Jdbc driver.
   
  -	If a jndi-datasource-name for JNDI based lookup of Jdbc Connections is
  -    specified, the four attributes driver, protocol, subprotocol, dbalias
  -    used for Jdbc DriverManager based construction of Jdbc Connections
  -    must not be declared.
  -
  -    The username and password attributes are used as credentials for obtaining
  -    a jdbc connections.
  -    If users don't want to keep this information the repository.xml file,
  -    they'll have to perform an explicit call to PersistenceBroker.open(...).
  +	If a jndi-datasource-name for JNDI based lookup of
  +    Jdbc Connections is specified, the four attributes
  +    driver, protocol, subprotocol, dbalias
  +    used for Jdbc DriverManager based construction of
  +    Jdbc Connections must not be declared.
  +
  +    The username and password attributes are used as credentials
  +    for obtaining a jdbc connections.
  +    If users don't want to keep this information the
  +    repository.xml file, they could pass user/password
  +    using PBKey.java to obtain a PersistenceBroker
     -->
   <!ATTLIST jdbc-connection-descriptor
   	platform (Db2 | Hsqldb | Informix | MsAccess | MsSQLServer |
  @@ -82,8 +87,8 @@
   >
   
   <!--
  -	The connection-pool element specifies the connection pooling
  -	parameter.
  +	The connection-pool element specifies the connection
  +    pooling parameter.
   -->
   <!ELEMENT connection-pool (documentation?)>
   <!--
  @@ -92,11 +97,13 @@
   	pool at one time. When non-positive, there is no limit.
   
   	maxIdle
  -	controls the maximum number of connections that can sit idle in the
  -	pool at any time. When non-positive, there is no limit
  +	controls the maximum number of connections that can sit
  +    idle in the pool at any time. When non-positive,
  +    there is no limit
   
   	maxWait
  -	max time block to get connection instance from pool, after that exception is thrown.
  +	max time block to get connection instance from pool,
  +    after that exception is thrown.
   	When non-positive, block till last judgement
   
   	whenExhaustedAction
  @@ -106,18 +113,21 @@
   
   
   	testOnBorrow
  -	The pool will attempt to validate each object before it is returned from the pool
  +	The pool will attempt to validate each object before
  +    it is returned from the pool
   
   	testOnReturn
  -	The pool will attempt to validate each object before it is returned to the pool
  +	The pool will attempt to validate each object
  +    before it is returned to the pool
   
   	testWhileIdle
   	Indicates whether or not idle objects should be validated.
   	Objects that fail to validate will be dropped from the pool
   
   	timeBetweenEvictionRunsMillis
  -	indicates how long the eviction thread should sleep before "runs" of examining
  -	idle objects. When non-positive, no eviction thread will be launched.
  +	indicates how long the eviction thread should sleep before "runs"
  +    of examining idle objects. When non-positive, no eviction
  +    thread will be launched.
   
   	minEvictableIdleTimeMillis
   	specifies the minimum amount of time that a connection may sit idle
  @@ -187,10 +197,6 @@
   	This class will be used as the RowReader implementation used to
   	materialize instances of the persistent class.
   
  -	The sequence-manager attribute may contain a full qualified class name.
  -	This class will be used as the SequenceManager implementation for
  -	the persistent class.
  -
   	The accept-locks attribute specifies whether implicit locking should
   	propagate to this class.  Currently relevant for the ODMG layer only.
     -->
  @@ -202,7 +208,6 @@
   	schema CDATA #IMPLIED
   	table CDATA #IMPLIED
   	row-reader CDATA #IMPLIED
  -	sequence-manager CDATA #IMPLIED
       extends IDREF #IMPLIED
   	accept-locks (true | false) "true"
   >
  @@ -258,8 +263,10 @@
   	attribute are automatically generated by OJB.
   
       The sequence-name attribute can be used to state explicitly a sequence
  -    name used by the sequence manager. If not specified the OJB standard
  -    sequence manager implementations build a sequence name automatic.
  +    name used by the sequence manager. Mandatory when using one of the
  +    proprietary database sequence based sequence manager implementations.
  +    If not specified, the OJB standard sequence manager implementations
  +    build a sequence name automatic.
   
   	The locking attribute is set to true if the persistent attribute is
   	used for optimistic locking. can only be set for TIMESTAMP and INTEGER
  @@ -481,7 +488,7 @@
   	name CDATA #REQUIRED
   	sort (ASC | DESC) "ASC"
   >
  -  
  +
   <!--
       A inverse-foreignkey element contains information on a foreign-key
       persistent attribute that implement the association on the database level.
  
  
  
  1.11      +33 -5     jakarta-ojb/src/test/org/apache/ojb/broker/sequence/SequenceManagerTest.java
  
  Index: SequenceManagerTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/test/org/apache/ojb/broker/sequence/SequenceManagerTest.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- SequenceManagerTest.java	29 Nov 2002 13:54:36 -0000	1.10
  +++ SequenceManagerTest.java	22 Dec 2002 17:12:22 -0000	1.11
  @@ -12,6 +12,7 @@
   import org.apache.ojb.broker.util.sequence.SequenceManagerException;
   import org.apache.ojb.broker.util.sequence.SequenceManagerFactory;
   import org.apache.ojb.broker.util.sequence.SequenceManagerHelper;
  +import org.apache.ojb.broker.util.sequence.SequenceManagerNextValImpl;
   
   import java.util.ArrayList;
   import java.util.Iterator;
  @@ -29,6 +30,7 @@
    */
   public class SequenceManagerTest extends TestCase
   {
  +    private static final String TEST_SEQUENCE_NAME = "TEST_SEQUENCE";
       /**
        * Max PK value for {@link SMMax} test class prepared
        * in database.
  @@ -155,16 +157,42 @@
        * Test the use of the 'sequence-name' field descriptor
        * attribute.
        */
  -    public void testSequenceNameGeneration()
  +    public void testSequenceNameAttribute()
       {
           // sequence name used in the repository
  -        String test = "sequence-name-test";
           String fieldName = "stringKey";
           PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
           FieldDescriptor field = broker.getClassDescriptor(SMKey.class).getFieldDescriptorByName(fieldName);
           String result = SequenceManagerHelper.buildSequenceName(broker, field);
   
  -        assertEquals(test, result);
  +        assertEquals(TEST_SEQUENCE_NAME, result);
  +        broker.close();
  +    }
  +
  +    /**
  +     * This test only works, when using
  +     * {@link org.apache.ojb.broker.util.sequence.SequenceManagerNextValImpl}
  +     * for sequence generation.
  +     */
  +    public void testDatabaseSequenceGeneration() throws Exception
  +    {
  +        PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  +        SequenceManager sm = SequenceManagerFactory.getSequenceManager(broker);
  +        if(!(sm instanceof SequenceManagerNextValImpl))
  +        {
  +            System.out.println("This test only works for SeqMan implementations using "
  +                    +SequenceManagerNextValImpl.class+" Skip test case.");
  +            broker.close();
  +            return;
  +        }
  +        int count = 0;
  +        for (int i = 0; i < 10; i++)
  +        {
  +            count += sm.getUniqueId(broker.getClassDescriptor(
  +                    SMDatabaseSequence.class).getAutoIncrementField());
  +            System.err.println("count "+count);
  +        }
  +        assertFalse("No keys generated", count == 0);
           broker.close();
       }
   
  @@ -395,7 +423,7 @@
        * test case was written by a user - thanks.
        * this test was *commented out* by default.
        */
  -    public void XXXtest_getUniqueIdWithTwoBrokers()
  +    public void test_getUniqueIdWithTwoBrokers()
       {
           PersistenceBroker pb = PersistenceBrokerFactory.defaultPersistenceBroker();
           PersistenceBroker pb2 = PersistenceBrokerFactory.defaultPersistenceBroker();
  
  
  
  1.1                  jakarta-ojb/src/test/org/apache/ojb/broker/sequence/SMDatabaseSequence.java
  
  Index: SMDatabaseSequence.java
  ===================================================================
  package org.apache.ojb.broker.sequence;
  
  /**
   *
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: SMDatabaseSequence.java,v 1.1 2002/12/22 17:12:22 arminw Exp $
   */
  public class SMDatabaseSequence
  {
      private Integer seqId;
      private String name;
  
      public SMDatabaseSequence(Integer seqId, String name)
      {
          this.seqId = seqId;
          this.name = name;
      }
  
      public SMDatabaseSequence()
      {
      }
  
      public Integer getSeqId()
      {
          return seqId;
      }
  
      public void setSeqId(Integer seqId)
      {
          this.seqId = seqId;
      }
  
      public String getName()
      {
          return name;
      }
  
      public void setName(String name)
      {
          this.name = name;
      }
  }
  
  
  
  1.5       +2 -1      jakarta-ojb/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerOracleImpl.java
  
  Index: SequenceManagerOracleImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerOracleImpl.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SequenceManagerOracleImpl.java	22 Nov 2002 09:27:26 -0000	1.4
  +++ SequenceManagerOracleImpl.java	22 Dec 2002 17:12:23 -0000	1.5
  @@ -129,7 +129,8 @@
               if (sequenceName == null || sequenceName.length() == 0)
               {
                   throw new SequenceManagerException(
  -                        "sequence-name attribute is not defined in the class descriptor
- "
  +                        "sequence-name attribute is not defined for field " +
  +                        field.getAttributeName() + " in the class descriptor "
                           + cld.getClassNameOfObject());
               }
               StringBuffer sql =
  
  
  
  1.1                  jakarta-ojb/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerNextValImpl.java
  
  Index: SequenceManagerNextValImpl.java
  ===================================================================
  package org.apache.ojb.broker.util.sequence;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache ObjectRelationalBridge" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.broker.metadata.ClassDescriptor;
  import org.apache.ojb.broker.metadata.FieldDescriptor;
  import org.apache.ojb.broker.query.Query;
  import org.apache.ojb.broker.util.logging.LoggerFactory;
  
  /**
   * This SequenceManager implementation uses database
   * sequence key generation (which was supported by
   * e.g. Oracle, SAP DB, PostgreSQL).
   * This class is responsible for creating new unique ID's
   * for primary key columns containing integer values.
   * <br/>
   * For using this sequence manager with your database it is mandatory
   * to define a <code>sequence-name</code> field-descriptor attribute
   * in the repository file.
   * <br/>
   * Keep in mind 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 <code>sequence-name</code>
   * for all extents.
   * <br/>
   * If your database needs a specific sql statement to obtain the next
   * sequence, extend this class and override the
   * {@link #buildSqlStatement(String sequenceName)} method to build
   * your own sql statement.
   *
   *
   * @author Edson Carlos Ericksson Richter
   * @author Rajeev Kaul
   * @author Thomas Mahler
   * @author Armin Waibel
   * @version $Id: SequenceManagerNextValImpl.java,v 1.1 2002/12/22 17:12:23 arminw Exp $
   */
  public class SequenceManagerNextValImpl extends AbstractSequenceManager
  {
      public final static String SQL_SELECT = "select ";
      public final static String SQL_NEXTVAL = ".nextval from dual";
  
      /**
       * Public constructor
       */
      public SequenceManagerNextValImpl(PersistenceBroker broker)
      {
          super(broker);
      }
  
      public int getUniqueId(FieldDescriptor field) throws SequenceManagerException
      {
          return getNextId(field);
      }
  
      /**
       * Override this method to build your own sequence specific
       * sql statement - The standard sql statement was
       * "select SEQUENCE_NAME.nextval from dual".
       */
      public String buildSqlStatement(String sequenceName)
      {
          return new StringBuffer().
              append(SQL_SELECT).
              append(sequenceName).
              append(SQL_NEXTVAL).
              toString();
      }
  
      /**
       * Returns next Id get from "sequence sequence.NextVal from Dual".
       * Mount sequence name as:
       * Schema.SQ_tableName_fieldName. If you have a table named MY_TABLE
       * and the sequenced field is MY_FIELD on schema MY_SCHEMA, the command
       * to create the sequence is:
       * CREATE SEQUENCE MY_SCHEMA.SQ_MY_TABLE_MY_FIELD
       */
      private synchronized int getNextId(FieldDescriptor field)
              throws SequenceManagerException
      {
          int result = 0;
          ResultSet rs = null;
          Statement stmt = null;
          try
          {
              ClassDescriptor cld = field.getClassDescriptor();
              // lookup sequence name as custom attribute on class descriptor level.
              String sequenceName = field.getSequenceName();
              if (sequenceName == null || sequenceName.length() == 0)
              {
                  throw new SequenceManagerException(
                          "sequence-name attribute is not defined for field '" +
                          field.getAttributeName() + "' in the class descriptor for object
"
                          + cld.getClassNameOfObject());
              }
              stmt = getBrokerForClass().getStatementManager().getGenericStatement(cld, Query.NOT_SCROLLABLE);
              rs = stmt.executeQuery(buildSqlStatement(sequenceName));
              rs.next();
              result = rs.getInt(1);
          }
          catch (Throwable t)
          {
              LoggerFactory.getDefaultLogger().error(t);
              throw new SequenceManagerException("Could not grab next id",t);
          }
          finally
          {
              try
              {
                  if (rs != null)
                      rs.close();
                  if (stmt != null)
                      stmt.close();
              }
              catch (SQLException e)
              {
                  LoggerFactory.getDefaultLogger().error(e);
                  throw new SequenceManagerException(e);
              }
              return result;
          }
      }
  }
  
  
  
  1.11      +62 -313   jakarta-ojb/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerHighLowImpl.java
  
  
  
  

Mime
View raw message