db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/util/sequence SequenceManagerMSSQLGuidImpl.java
Date Thu, 31 Jul 2003 19:13:20 GMT
arminw      2003/07/31 12:13:20

  Modified:    xdocs    sequencemanager.xml
  Added:       src/java/org/apache/ojb/broker/util/sequence
                        SequenceManagerMSSQLGuidImpl.java
  Log:
  add new sequence manager implementation
  for MSSQL DB posted by Andrew Clute
  
  Revision  Changes    Path
  1.12      +63 -2     db-ojb/xdocs/sequencemanager.xml
  
  Index: sequencemanager.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/xdocs/sequencemanager.xml,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- sequencemanager.xml	7 May 2003 21:32:49 -0000	1.11
  +++ sequencemanager.xml	31 Jul 2003 19:13:20 -0000	1.12
  @@ -15,13 +15,15 @@
       <li><a href="#automatical">Automatical assignment of unique values</a></li>
       <li><a href="#force">Force computation of unique values</a></li>
       <li><a href="#howToChange">How to change the sequence manager?</a></li>
  -    <li><a href="#implementations">SequenceManager implementations</a>
  +    <li><a href="#SequenceManager implementations">SequenceManager implementations</a>
           <ul>
               <li><a href="#High/Low sequence manager">High/Low sequence manager</a></li>
               <li><a href="#In-Memory sequence manager">In-Memory sequence manager</a></li>
               <li><a href="#Database sequences based implementation">Database
sequences based implementation</a></li>
               <li><a href="#Database sequences based high/low implementation">Database
sequences based high/low implementation</a></li>
               <li><a href="#Oracle-style sequencing">Oracle-style sequencing
using stored procedure</a></li>
  +            <li><a href="#Microsoft SQL Server 'uniqueidentifier' type (GUID)
sequencing">
  +                Microsoft SQL Server 'uniqueidentifier' type (GUID) sequencing</a></li>
           </ul>
       </li>
       <li><a href="#howToWrite">How to write my own sequence manager?</a></li>
  @@ -160,7 +162,7 @@
   
   
   
  -<subsection name="SequenceManager implementations" anchor="implementations">
  +<subsection name="SequenceManager implementations" anchor="SequenceManager implementations">
   <p>
   Source code of all <code>SequenceManager</code> implementations could be
   found in <code>org.apache.ojb.broker.util.sequence</code> package.
  @@ -435,6 +437,65 @@
   <br/>- currently none known
   <br/>(- do not use if database will be updated by other
   applications)
  +</p>
  +
  +
  +<br/>
  +<p>
  +<a name="Microsoft SQL Server 'uniqueidentifier' type (GUID) sequencing"/>
  +<b>Microsoft SQL Server 'uniqueidentifier' type (GUID) sequencing</b><br/>
  +<font size="-1">(Thanks Andrew Clute)</font>
  +<br/>
  +
  +For those users you are using SQL Server 7.0 and up, the uniqueidentifier
  +was introduced, and allows for your rows Primary Keys to be GUID's that are
  +guaranteed to be unique in time and space.
  +</p>
  +
  +<p>
  +However, this type is different than the Identity field type, whereas there
  +is no way to programmatically retrieve the inserted value. Most
  +implementations when using the u.i. field type set a default value of
  +"newid()". The SequenceManagerMSSQLGuidImpl class manages this process for
  +you as if it was any normal generated sequence/identity field.
  +</p>
  +
  +<p>
  +Assuming that your PK on your table is set to 'uniqueidentifier', your
  +field-description would be the same as using any other SequenceManager:
  +
  +<source><![CDATA[
  +<field-descriptor
  +         name="guid"
  +         column="document_file_guid"
  +         jdbc-type="VARCHAR"
  +         primarykey="true"
  +         autoincrement="true"
  +      />
  +]]></source>
  +Note that the jdbc-type is a VARCHAR,  and thus the attribute (in this case
  +'guid') on your class should be a String (SQL Server does the conversion
  +from the String representation to the binary representation when
  +retrieved/set).
  +</p>
  +
  +<p>
  +You also need to turn on the SequenceManager in your
  +jdbc-connection-descriptor like this:
  +<source><![CDATA[
  +<sequence-manager
  +className="org.apache.ojb.broker.util.sequence.SequenceManagerMSSQLGuidImpl"
  +/>
  +]]></source>
  +</p>
  +
  +<p>
  +Limitations:
  +<br/>-This will only work with SQL Server 7.0 and higher as the
  +uniqueidentifier type was not introduced until then.
  +<br/>This works well in situations where other applications might be updated the
  +database as well, because it guarantees (well, as much as Microsoft can
  +guarantee) that there will be no collisions between the Guids generated.
   </p>
   
   </subsection>
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerMSSQLGuidImpl.java
  
  Index: SequenceManagerMSSQLGuidImpl.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 org.apache.commons.lang.SystemUtils;
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.broker.PersistenceBrokerException;
  import org.apache.ojb.broker.accesslayer.ResultSetAndStatement;
  import org.apache.ojb.broker.metadata.FieldDescriptor;
  import org.apache.ojb.broker.query.Query;
  
  import java.sql.SQLException;
  
  
  /**
   * An Implementation Class that will retrieve a valid new value
   * for a PK field that is of type 'uniqueidentifier'. Since values
   * for these types are generated through a 'newid()' call to
   * MSSQL Server, this class is only valid for MSSQL Server 7.0 and up.
   * <br/>
   * This SequenceManager can be used for any classes that have their PK
   * defined as a 'uniqueidetifier'
   *
   * @author <a href="mailto:aclute825@hotmail.com">Andrew Clute</a>
   * @version $Id: SequenceManagerMSSQLGuidImpl.java,v 1.1 2003/07/31 19:13:20 arminw Exp
$
   */
  public class SequenceManagerMSSQLGuidImpl extends AbstractSequenceManager
  {
      /**
       * Constructor used by
       * {@link org.apache.ojb.broker.util.sequence.SequenceManagerFactory}
       *
       * @param broker  PB instance to perform the
       * id generation.
       */
      public SequenceManagerMSSQLGuidImpl(PersistenceBroker broker)
      {
          super(broker);
      }
  
      /**
       * returns a unique String for given field.
       * the returned uid is unique accross all tables.
       */
      protected String getUniqueString(FieldDescriptor field) throws SequenceManagerException
      {
          ResultSetAndStatement rsStmt = null;
          try
          {
              rsStmt = getBrokerForClass().serviceJdbcAccess().executeSQL("select newid()",
field.getClassDescriptor(), Query.NOT_SCROLLABLE);
              String returnValue = null;
              if (rsStmt.m_rs.next())
              {
                  returnValue = rsStmt.m_rs.getString(1);
              }
              rsStmt.m_rs.close();
              return returnValue;
  
          }
          catch (PersistenceBrokerException e)
          {
              throw new SequenceManagerException(e);
          }
          catch (SQLException e)
          {
              throw new SequenceManagerException(e);
          }
  
          finally
          {
              if (rsStmt != null)
                  rsStmt.close();
          }
      }
  
      /**
       * Returns a new unique int for the given Class and fieldname.
       */
      protected int getUniqueId(FieldDescriptor field) throws SequenceManagerException
      {
          throw new SequenceManagerException(
                  SystemUtils.LINE_SEPARATOR +
                  "Failure attempting to retrieve a Guid for a field that is an int -- field
should be returned as a VARCHAR");
      }
  
      /**
       * Returns a new unique int for the given Class and fieldname.
       */
      protected long getUniqueLong(FieldDescriptor field) throws SequenceManagerException
      {
          throw new SequenceManagerException(
                  SystemUtils.LINE_SEPARATOR +
                  "Failure attempting to retrieve a Guid for a field that is a long -- field
should be returned as a VARCHAR");
      }
  
      /**
       * returns a unique Object for class clazz and field fieldName.
       * the returned Object is unique accross all tables in the extent of clazz.
       */
      protected Object getUniqueObject(FieldDescriptor field) throws SequenceManagerException
      {
          return getUniqueString(field);
      }
  }
  
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message