db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject svn commit: r513566 - in /db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence: AbstractSequenceManager.java PerFieldManager.java SequenceManager.java SequenceManagerHighLowImpl.java SequenceManagerIdentityImpl.java
Date Fri, 02 Mar 2007 00:55:33 GMT
Author: arminw
Date: Thu Mar  1 16:55:32 2007
New Revision: 513566

URL: http://svn.apache.org/viewvc?view=rev&rev=513566
Log:
SequenceManager changes to support of Statement.getGeneratedKeys() in database identity column
based sequence manager

Modified:
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/AbstractSequenceManager.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/PerFieldManager.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManager.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerHighLowImpl.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerIdentityImpl.java

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/AbstractSequenceManager.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/AbstractSequenceManager.java?view=diff&rev=513566&r1=513565&r2=513566
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/AbstractSequenceManager.java
(original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/AbstractSequenceManager.java
Thu Mar  1 16:55:32 2007
@@ -16,6 +16,7 @@
  */
 
 import java.util.Properties;
+import java.sql.Statement;
 
 import org.apache.ojb.broker.PersistenceBroker;
 import org.apache.ojb.broker.accesslayer.JdbcAccess;
@@ -165,5 +166,14 @@
             throws SequenceManagerException
     {
         // do nothing
+    }
+
+    /**
+     * NOTE: For backward compatibility with older sequence manager implementations, this
method calls deprecated method
+     * {@link #afterStore(org.apache.ojb.broker.accesslayer.JdbcAccess, org.apache.ojb.broker.metadata.ClassDescriptor,
Object)}.
+     */
+    public void afterStore(JdbcAccess dbAccess, Statement stmt, ClassDescriptor cld, Object
obj) throws SequenceManagerException
+    {
+        afterStore(dbAccess, cld, obj);
     }
 }

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/PerFieldManager.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/PerFieldManager.java?view=diff&rev=513566&r1=513565&r2=513566
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/PerFieldManager.java
(original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/PerFieldManager.java
Thu Mar  1 16:55:32 2007
@@ -17,6 +17,7 @@
 
 import java.lang.Object;
 import java.util.Map;
+import java.sql.Statement;
 
 import org.apache.ojb.broker.util.logging.Logger;
 import org.apache.ojb.broker.util.logging.LoggerFactory;
@@ -76,7 +77,7 @@
         this.defaultManager = defaultManager;
     }
 
-    public Object getUniqueValue(FieldDescriptor field) throws SequenceManagerException
+    public Object getUniqueValue(final FieldDescriptor field) throws SequenceManagerException
     {
         SequenceManager sm = lookup(field);
         return sm.getUniqueValue(field);
@@ -84,6 +85,11 @@
 
     public void afterStore(JdbcAccess dbAccess, ClassDescriptor cld, Object obj) throws SequenceManagerException
     {
+        // noop
+    }
+
+    public void afterStore(final JdbcAccess dbAccess, final Statement stmt, final ClassDescriptor
cld, final Object obj) throws SequenceManagerException
+    {
         FieldDescriptor[] pks = cld.getAutoIncrementFields();
         FieldDescriptor result;
         for(int i = 0; i < pks.length; i++)
@@ -92,7 +98,7 @@
             SequenceManager sm = lookup(result);
             if(sm != null)
             {
-                sm.afterStore(dbAccess, cld, obj);
+                sm.afterStore(dbAccess, stmt, cld, obj);
             }
         }
     }

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManager.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManager.java?view=diff&rev=513566&r1=513565&r2=513566
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManager.java
(original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManager.java
Thu Mar  1 16:55:32 2007
@@ -15,6 +15,8 @@
  * limitations under the License.
  */
 
+import java.sql.Statement;
+
 import org.apache.ojb.broker.accesslayer.JdbcAccess;
 import org.apache.ojb.broker.metadata.ClassDescriptor;
 import org.apache.ojb.broker.metadata.FieldDescriptor;
@@ -62,6 +64,20 @@
      * @param dbAccess Current used {@link org.apache.ojb.broker.accesslayer.JdbcAccess}
instance
      * @param cld Descriptor for specified object
      * @param obj The object to associate with identity value
+     * @deprecated
      */
     public void afterStore(JdbcAccess dbAccess, ClassDescriptor cld, Object obj) throws SequenceManagerException;
+
+    /**
+     * This method is called <strong>after</strong> the object was written to
the persistent storage.
+     * <br/>
+     * This is to support native Identity columns (auto_increment columns) on the db side.
+     * Other implementations may ignore this method.
+     * @param dbAccess The current used {@link org.apache.ojb.broker.accesslayer.JdbcAccess}
instance.
+     * @param stmt The current executed {@link Statement}.
+     * @param cld Descriptor for specified object.
+     * @param obj The object to associate with identity value.
+     */
+    public void afterStore(JdbcAccess dbAccess, Statement stmt, ClassDescriptor cld, Object
obj)
+            throws SequenceManagerException;
 }

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerHighLowImpl.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerHighLowImpl.java?view=diff&rev=513566&r1=513565&r2=513566
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerHighLowImpl.java
(original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerHighLowImpl.java
Thu Mar  1 16:55:32 2007
@@ -19,6 +19,7 @@
 import java.util.Map;
 
 import org.apache.commons.lang.SystemUtils;
+import org.apache.commons.lang.BooleanUtils;
 import org.apache.ojb.broker.Identity;
 import org.apache.ojb.broker.OptimisticLockException;
 import org.apache.ojb.broker.PersistenceBroker;
@@ -51,7 +52,7 @@
  *    </td>
  * </tr>
  * <tr>
- *     <td>grabSize</td>
+ *     <td>seq.grabSize</td>
  *     <td>
  *         Integer entry determines the
  *         number of IDs allocated within the
@@ -60,6 +61,15 @@
  *    </td>
  * </tr>
  * <tr>
+ *     <td>grabSize</td>
+ *     <td>
+ *         DEPRECATED. Integer entry determines the
+ *         number of IDs allocated within the
+ *         H/L sequence manager implementation.
+ *         Default was '20'.
+ *    </td>
+ * </tr>
+ * <tr>
  *     <td>seq.autoNaming</td>
  *     <td>
  *          Default was 'true'. If set 'true' OJB try to build a
@@ -167,7 +177,7 @@
             grabSizeStr = getConfigurationProperty(PROPERTY_GRAB_SIZE_OLD, "20");
         }
         grabSize = Integer.parseInt(grabSizeStr);
-        useGlobalSequenceIdentities = Boolean.getBoolean(getConfigurationProperty(PROPERTY_GLOBAL_SEQUENCE_ID,
"false"));
+        useGlobalSequenceIdentities = BooleanUtils.toBoolean(getConfigurationProperty(PROPERTY_GLOBAL_SEQUENCE_ID,
"false"));
         // support for deprecated properties
         long globalSequenceStart = Long.parseLong(getConfigurationProperty(PROPERTY_GLOBAL_SEQUENCE_START,
"1"));
         if(useGlobalSequenceIdentities && globalSequenceStart > sequenceStart)

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerIdentityImpl.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerIdentityImpl.java?view=diff&rev=513566&r1=513565&r2=513566
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerIdentityImpl.java
(original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerIdentityImpl.java
Thu Mar  1 16:55:32 2007
@@ -22,6 +22,7 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.commons.lang.BooleanUtils;
 import org.apache.ojb.broker.PersistenceBroker;
 import org.apache.ojb.broker.accesslayer.JdbcAccess;
 import org.apache.ojb.broker.metadata.SequenceDescriptor;
@@ -52,10 +53,12 @@
  * <td><strong>Property Values</strong></td>
  * </tr>
  * <tr>
- * <td>no properties to set</td>
- * <td>
- * <p/>
- * </td>
+ *     <td>seq.generatedKeys</td>
+ *     <td>
+ *          If set <em>true</em> OJB use JDBC 3.0 method <em>Statement.getGeneratedKeys()</em>
+ *          to obtain generated database identity keys. If set <em>false</em>
OJB use a separate
+ *          database specific <em>"last identity call"</em> query to request
the generated identity key.
+ *    </td>
  * </tr>
  * </table>
  * </p>
@@ -81,6 +84,9 @@
 {
     private Log log = LogFactory.getLog(SequenceManagerIdentityImpl.class);
 
+    public static final String PROPERTY_USE_GENERATED_KEYS = "seq.generatedKeys";
+    private boolean useGeneratedKeys;
+
     /*
      TODO:
      1. Find a better solution (if possible) for this problem
@@ -99,14 +105,21 @@
     public SequenceManagerIdentityImpl(PersistenceBroker broker)
     {
         super(broker);
+        init();
     }
 
     public SequenceManagerIdentityImpl(PersistenceBroker broker, SequenceDescriptor descriptor)
     {
         super(broker, descriptor);
+        init();
+    }
+
+    void init()
+    {
+        useGeneratedKeys = BooleanUtils.toBoolean(getConfigurationProperty(PROPERTY_USE_GENERATED_KEYS,
"true"));
     }
 
-    public void afterStore(JdbcAccess dbAccess, ClassDescriptor cld, Object obj) throws SequenceManagerException
+    public void afterStore(JdbcAccess dbAccess, Statement stmt, ClassDescriptor cld, Object
obj) throws SequenceManagerException
     {
         FieldDescriptor identityField = extractIdentityColumnField(cld);
         if(identityField != null)
@@ -115,8 +128,26 @@
             // thus we have to execute batch entries before
             getBrokerForClass().serviceConnectionManager().executeBatch();
             ifNotReadOnlyFail(identityField);
-            long newId = getLastInsert(cld, identityField);
-            setFieldValue(obj, identityField, new Long(newId));
+            Object val;
+            if(useGeneratedKeys && getPlatform().supportsGetGeneratedKeys())
+            {
+                try
+                {
+                    ResultSet result = getPlatform().getGeneratedKeys(stmt);
+                    result.next();
+                    val = identityField.getJdbcType().getObjectFromColumn(result, 1);
+                    val = identityField.getFieldConversion().sqlToJava(val);
+                }
+                catch(SQLException e)
+                {
+                    throw new SequenceManagerException("Error while extract generated keys
from statement", e);
+                }
+            }
+            else
+            {
+                val = new Long(getLastInsert(cld, identityField));
+            }
+            setFieldValue(obj, identityField, val);
         }
     }
 
@@ -205,9 +236,17 @@
                 getSupportedPlatform().getLastInsertIdentityQuery(tableName);
     }
 
-    private void setFieldValue(Object obj, FieldDescriptor field, Long identifier) throws
SequenceManagerException
+    private void setFieldValue(Object obj, FieldDescriptor field, Object identifier) throws
SequenceManagerException
     {
-        Object result = field.getJdbcType().sequenceKeyConversion(identifier);
+        Object result;
+        if(identifier instanceof Long)
+        {
+            result= field.getJdbcType().sequenceKeyConversion((Long)identifier);
+        }
+        else
+        {
+            result = identifier;
+        }
         result = field.getFieldConversion().sqlToJava(result);
         PersistentField pf = field.getPersistentField();
         pf.set(obj, result);



---------------------------------------------------------------------
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