You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by mt...@apache.org on 2011/02/27 12:15:55 UTC

svn commit: r1075026 - in /openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc: kernel/NativeJDBCSeq.java sql/DB2Dictionary.java sql/DBDictionary.java sql/FirebirdDictionary.java sql/OracleDictionary.java sql/PostgresDictionary.java

Author: mtylenda
Date: Sun Feb 27 11:15:55 2011
New Revision: 1075026

URL: http://svn.apache.org/viewvc?rev=1075026&view=rev
Log:
OPENJPA-1376: Cache values of @SequenceGenerator as specified by allocationSize parameter instead of setting CACHE on database sequence. Idea submitted by Palmer Cox.

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/FirebirdDictionary.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/OracleDictionary.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java?rev=1075026&r1=1075025&r2=1075026&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java Sun Feb 27 11:15:55 2011
@@ -45,16 +45,16 @@ import org.apache.openjpa.lib.util.Optio
 import org.apache.openjpa.util.MetaDataException;
 import org.apache.openjpa.util.UserException;
 
-import serp.util.Strings;
-
 ///////////////////////////////////////////////////////////
 // NOTE: Do not change property names; see SequenceMetaData
 // and SequenceMapping for standard property names.
 ////////////////////////////////////////////////////////////
 
 /**
- * {@link JDBCSeq} implementation that uses a database sequences
+ * {@link JDBCSeq} implementation that uses a database sequence
  * to generate numbers.
+ * Supports allocation (caching). In order for allocation to work properly, the database sequence must be defined
+ * with INCREMENT BY value equal to allocate * increment.
  *
  * @see JDBCSeq
  * @see AbstractJDBCSeq
@@ -73,14 +73,11 @@ public class NativeJDBCSeq
     private DBIdentifier _seqName = DBIdentifier.newSequence("OPENJPA_SEQUENCE");
     private int _increment = 1;
     private int _initial = 1;
-    private int _allocate = 0;
+    private int _allocate = 50;
     private Sequence _seq = null;
     private String _select = null;
-
-    // for deprecated auto-configuration support
-    private String _format = null;
-    private DBIdentifier _tableName = DBIdentifier.newTable("DUAL");
-    private boolean _subTable = false;
+    private long _nextValue = 0;
+    private long _maxValue = -1;
 
     private DBIdentifier _schema = DBIdentifier.NULL;
         
@@ -100,15 +97,6 @@ public class NativeJDBCSeq
     }
 
     /**
-     * @deprecated Use {@link #setSequence}. Retained for
-     * backwards-compatibility for auto-configuration.
-     */
-    @Deprecated
-    public void setSequenceName(String seqName) {
-        setSequence(seqName);
-    }
-
-    /**
      * @see Sequence#getInitialValue
      */
     public int getInitialValue() {
@@ -150,23 +138,6 @@ public class NativeJDBCSeq
         _increment = increment;
     }
 
-    /**
-     * @deprecated Retained for backwards-compatibility for auto-configuration.
-     */
-    @Deprecated
-    public void setTableName(String table) {
-        _tableName = DBIdentifier.newTable(table);
-    }
-
-    /**
-     * @deprecated Retained for backwards-compatibility for auto-configuration.
-     */
-    @Deprecated
-    public void setFormat(String format) {
-        _format = format;
-        _subTable = true;
-    }
-
     @Override
     public void addSchema(ClassMapping mapping, SchemaGroup group) {
         // sequence already exists?
@@ -204,28 +175,43 @@ public class NativeJDBCSeq
         buildSequence();
 
         DBDictionary dict = _conf.getDBDictionaryInstance();
-        if (_format == null) {
-            _format = dict.nextSequenceQuery;
-            if (_format == null)
-                throw new MetaDataException(_loc.get("no-seq-sql", _seqName));
+        String format = dict.nextSequenceQuery;
+        if (format == null) {
+            throw new MetaDataException(_loc.get("no-seq-sql", _seqName));
         }
-        if (DBIdentifier.isNull(_tableName))
-            _tableName = DBIdentifier.newTable("DUAL");
 
         String name = dict.getFullName(_seq);
-        Object[] subs = (_subTable) ? new Object[]{ name, _tableName }
-            : new Object[]{ name };
-        _select = MessageFormat.format(_format, subs);
+        // Increment step is needed for Firebird which uses non-standard sequence fetch syntax.
+        // Use String.valueOf to get rid of possible locale-specific number formatting.
+        _select = MessageFormat.format(format, new Object[]{name, String.valueOf(_allocate * _increment)});
         
         type = dict.nativeSequenceType;
     }
     
     @Override
-    protected Object nextInternal(JDBCStore store, ClassMapping mapping)
+    protected synchronized Object nextInternal(JDBCStore store, ClassMapping mapping)
+        throws SQLException {
+        if (_nextValue < _maxValue) {
+            return _nextValue++;
+        }
+
+        allocateInternal(0, store, mapping);
+        return _nextValue++;
+    }
+
+    /**
+     * Allocate additional sequence values.
+     * @param additional ignored - the allocation size is fixed and determined by allocate and increment properties. 
+     * @param store used to obtain connection
+     * @param mapping ignored
+     */
+    @Override
+    protected synchronized void allocateInternal(int additional, JDBCStore store, ClassMapping mapping)
         throws SQLException {
         Connection conn = getConnection(store);
         try {
-            return getSequence(conn);
+            _nextValue = getSequence(conn);
+            _maxValue = _nextValue + _allocate * _increment;
         } finally {
             closeConnection(conn);
         }
@@ -300,9 +286,7 @@ public class NativeJDBCSeq
         try {
             stmnt = conn.prepareStatement(_select);
             dict.setTimeouts(stmnt, _conf, false);
-            synchronized(this) {
-                rs = stmnt.executeQuery();
-            }
+            rs = stmnt.executeQuery();
             if (rs.next())
                 return rs.getLong(1);
 
@@ -327,13 +311,12 @@ public class NativeJDBCSeq
      *  Where the following options are recognized.
      * <ul>
      * <li><i>-properties/-p &lt;properties file or resource&gt;</i>: The
-     * path or resource name of a OpenJPA properties file containing
-     * information such as the license key	and connection data as
+     * path or resource name of an OpenJPA properties file containing
+     * information such as connection data as
      * outlined in {@link JDBCConfiguration}. Optional.</li>
      * <li><i>-&lt;property name&gt; &lt;property value&gt;</i>: All bean
      * properties of the OpenJPA {@link JDBCConfiguration} can be set by
-     * using their	names and supplying a value. For example:
-     * <code>-licenseKey adslfja83r3lkadf</code></li>
+     * using their names and supplying a value.</li>
      * </ul>
      *  The various actions are as follows.
      * <ul>
@@ -373,7 +356,7 @@ public class NativeJDBCSeq
     }
 
     /**
-     * Run the tool. Return false if an invalid option was given.
+     * Run the tool. Returns false if an invalid option was given.
      */
     public static boolean run(JDBCConfiguration conf, String[] args,
         String action)

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java?rev=1075026&r1=1075025&r2=1075026&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java Sun Feb 27 11:15:55 2011
@@ -44,7 +44,6 @@ import org.apache.openjpa.jdbc.kernel.ex
 import org.apache.openjpa.jdbc.schema.Column;
 import org.apache.openjpa.jdbc.schema.Index;
 import org.apache.openjpa.jdbc.schema.Schema;
-import org.apache.openjpa.jdbc.schema.Sequence;
 import org.apache.openjpa.jdbc.schema.Table;
 import org.apache.openjpa.kernel.Filters;
 import org.apache.openjpa.kernel.MixedLockLevels;
@@ -225,13 +224,6 @@ public class DB2Dictionary
         }
     }
 
-    public String[] getCreateSequenceSQL(Sequence seq) {
-        String[] sql = super.getCreateSequenceSQL(seq);
-        if (seq.getAllocate() > 1)
-            sql[0] += " CACHE " + seq.getAllocate();
-        return sql;
-    }
-
     @Override
     protected String getSequencesSQL(String schemaName, String sequenceName) {
         return getSequencesSQL(DBIdentifier.newSchema(schemaName), 

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?rev=1075026&r1=1075025&r2=1075026&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java Sun Feb 27 11:15:55 2011
@@ -3400,8 +3400,8 @@ public class DBDictionary
         buf.append(seqName);
         if (seq.getInitialValue() != 0)
             buf.append(" START WITH ").append(seq.getInitialValue());
-        if (seq.getIncrement() > 1)
-            buf.append(" INCREMENT BY ").append(seq.getIncrement());
+        if ((seq.getIncrement() > 1) || (seq.getAllocate() > 1))
+            buf.append(" INCREMENT BY ").append(seq.getIncrement() * seq.getAllocate());
         return new String[]{ buf.toString() };
     }
 
@@ -4359,7 +4359,7 @@ public class DBDictionary
         }
     }
     
-    /*
+    /**
      * Combines partial foreign keys into singular key
      */
     protected ForeignKey combineForeignKey(Map<FKMapKey, ForeignKey> fkMap,
@@ -5252,8 +5252,8 @@ public class DBDictionary
     }
     
     /**
-     * Validate that the given name is no longer than given maximum length.
-     * If the given name is indeed longer then raises an UserException with the 
+     * Validate that the given name is not longer than given maximum length.
+     * If the given name is indeed longer then raises a UserException with the 
      * given message key otherwise returns the same name.
      */
     final String checkNameLength(String name, int length, String msgKey) {

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/FirebirdDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/FirebirdDictionary.java?rev=1075026&r1=1075025&r2=1075026&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/FirebirdDictionary.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/FirebirdDictionary.java Sun Feb 27 11:15:55 2011
@@ -59,11 +59,6 @@ public class FirebirdDictionary
     protected String createSequenceSQLFB15 = "CREATE GENERATOR {0}";
     protected String createSequenceSQLFB20 = "CREATE SEQUENCE {0}";
     protected String dropSequenceSQLFB15 = "DROP GENERATOR ";
-    protected String nextSequenceQueryFB15 =
-        "SELECT GEN_ID({0}, 1) FROM RDB$DATABASE";
-    protected String nextSequenceQueryFB20 =
-        "SELECT NEXT VALUE FOR {0} FROM RDB$DATABASE";
-
     protected String alterSequenceSQL = alterSequenceSQLFB20;
     protected String createSequenceSQL = createSequenceSQLFB20;
 
@@ -98,7 +93,9 @@ public class FirebirdDictionary
 
         supportsMultipleNontransactionalResultSets = false;
 
-        nextSequenceQuery = nextSequenceQueryFB20;
+        // On Firebird 2 the recommended syntax is "SELECT NEXT VALUE FOR {0} FROM RDB$DATABASE".
+        // However, that syntax allows incrementing the sequence value by 1 only.
+        nextSequenceQuery = "SELECT GEN_ID({0}, {1}) FROM RDB$DATABASE";
         sequenceSQL =
             "SELECT NULL AS SEQUENCE_SCHEMA, RDB$GENERATOR_NAME "
                 + "AS SEQUENCE_NAME FROM RDB$GENERATORS "
@@ -176,7 +173,6 @@ public class FirebirdDictionary
             trimBothFunction = "LTRIM(RTRIM({0}))";
             alterSequenceSQL = alterSequenceSQLFB15;
             createSequenceSQL = createSequenceSQLFB15;
-            nextSequenceQuery = nextSequenceQueryFB15;
         }
     }
 

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/OracleDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/OracleDictionary.java?rev=1075026&r1=1075025&r2=1075026&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/OracleDictionary.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/OracleDictionary.java Sun Feb 27 11:15:55 2011
@@ -50,7 +50,6 @@ import org.apache.openjpa.jdbc.schema.Co
 import org.apache.openjpa.jdbc.schema.ForeignKey;
 import org.apache.openjpa.jdbc.schema.Index;
 import org.apache.openjpa.jdbc.schema.PrimaryKey;
-import org.apache.openjpa.jdbc.schema.Sequence;
 import org.apache.openjpa.jdbc.schema.Table;
 import org.apache.openjpa.jdbc.schema.ForeignKey.FKMapKey;
 import org.apache.openjpa.lib.jdbc.DelegatingDatabaseMetaData;
@@ -996,14 +995,6 @@ public class OracleDictionary
         return sql;
     }
 
-    @Override
-    public String[] getCreateSequenceSQL(Sequence seq) {
-        String[] sql = super.getCreateSequenceSQL(seq);
-        if (seq.getAllocate() > 1)
-            sql[0] += " CACHE " + seq.getAllocate();
-        return sql;
-    }
-    
     /**
      * Return the preferred {@link Types} constant for the given
      * {@link JavaTypes} or {@link JavaSQLTypes} constant.

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java?rev=1075026&r1=1075025&r2=1075026&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java Sun Feb 27 11:15:55 2011
@@ -45,7 +45,6 @@ import org.apache.openjpa.jdbc.kernel.JD
 import org.apache.openjpa.jdbc.kernel.JDBCStore;
 import org.apache.openjpa.jdbc.kernel.exps.FilterValue;
 import org.apache.openjpa.jdbc.schema.Column;
-import org.apache.openjpa.jdbc.schema.Sequence;
 import org.apache.openjpa.jdbc.schema.Table;
 import org.apache.openjpa.kernel.Filters;
 import org.apache.openjpa.lib.jdbc.DelegatingConnection;
@@ -355,14 +354,6 @@ public class PostgresDictionary
     }
 
     @Override
-    public String[] getCreateSequenceSQL(Sequence seq) {
-        String[] sql = super.getCreateSequenceSQL(seq);
-        if (seq.getAllocate() > 1)
-            sql[0] += " CACHE " + seq.getAllocate();
-        return sql;
-    }
-
-    @Override
     protected boolean supportsDeferredUniqueConstraints() {
         // Postgres only supports deferred foreign key constraints.
         return false;