Author: allee8285
Date: Wed Nov 9 19:12:34 2011
New Revision: 1199921
URL: http://svn.apache.org/viewvc?rev=1199921&view=rev
Log:
OPENJPA-2069 Update db sequence's INCREMENT BY value to match the
allocationSize specified in @SequenceGenerator
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/DBDictionary.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.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=1199921&r1=1199920&r2=1199921&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
Wed Nov 9 19:12:34 2011
@@ -80,7 +80,9 @@ public class NativeJDBCSeq
private long _maxValue = -1;
private DBIdentifier _schema = DBIdentifier.NULL;
-
+
+ private boolean alterIncrementBy = false;
+
/**
* The sequence name. Defaults to <code>OPENJPA_SEQUENCE</code>.
*/
@@ -191,13 +193,13 @@ public class NativeJDBCSeq
@Override
protected synchronized Object nextInternal(JDBCStore store, ClassMapping
mapping)
throws SQLException {
- if (_nextValue < _maxValue) {
- long result = _nextValue;
- _nextValue += _increment;
- return result;
+ if (!alterIncrementBy) {
+ allocateInternal(0, store, mapping);
+ alterIncrementBy = true;
+ }
+ if (_nextValue >= _maxValue) {
+ allocateInternal(0, store, mapping);
}
-
- allocateInternal(0, store, mapping);
long result = _nextValue;
_nextValue += _increment;
return result;
@@ -214,6 +216,10 @@ public class NativeJDBCSeq
throws SQLException {
Connection conn = getConnection(store);
try {
+ if (!alterIncrementBy) {
+ DBDictionary dict = _conf.getDBDictionaryInstance();
+ udpateSql(conn, dict.getAlterSequenceSQL(_seq));
+ }
_nextValue = getSequence(conn);
_maxValue = _nextValue + _allocate * _increment;
} finally {
@@ -305,6 +311,26 @@ public class NativeJDBCSeq
}
}
+ private int udpateSql(Connection conn, String sql) throws SQLException {
+ DBDictionary dict = _conf.getDBDictionaryInstance();
+ PreparedStatement stmnt = null;
+ int rc = -1;
+ try {
+ stmnt = conn.prepareStatement(sql);
+ dict.setTimeouts(stmnt, _conf, false);
+ rc = stmnt.executeUpdate();
+ } finally {
+ // clean up our resources
+ if (stmnt != null) {
+ try {
+ stmnt.close();
+ } catch (SQLException se) {
+ }
+ }
+ }
+ return rc;
+ }
+
/////////
// Main
/////////
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=1199921&r1=1199920&r2=1199921&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
Wed Nov 9 19:12:34 2011
@@ -3406,28 +3406,36 @@ public class DBDictionary
* [ INCREMENT BY <increment>]</code> by default.
*/
public String[] getCreateSequenceSQL(Sequence seq) {
+ return commonCreateAlterSequenceSQL(seq, true);
+ }
+
+ public String getAlterSequenceSQL(Sequence seq) {
+ return commonCreateAlterSequenceSQL(seq, false)[0];
+ }
+
+ private String[] commonCreateAlterSequenceSQL(Sequence seq, boolean
create) {
if (nextSequenceQuery == null)
return null;
- //We need a place to detect if the user is setting the
'useNativeSequenceCache' property.
- //While in previous releases this property had meaning, it is no longer
useful
- //given the code added via OPENJPA-1327. As such, we need to warn
user's the
- //property no longer has meaning. While it would be nice to have a
better way
- //to detect if the useNativeSequenceCache property has been set, the
best we can do
- //is detect the variable in this code path as this is the path a user's
code
- //would go down if they are still executing code which actually made
use of
- //the support provided via setting useNativeSequenceCache.
- if (!useNativeSequenceCache && logNativeSequenceCacheWarning){
- log.warn(_loc.get("sequence-cache-warning"));
- logNativeSequenceCacheWarning=false;
- }
+ //We need a place to detect if the user is setting the
'useNativeSequenceCache' property.
+ //While in previous releases this property had meaning, it is no
longer useful
+ //given the code added via OPENJPA-1327. As such, we need to warn
user's the
+ //property no longer has meaning. While it would be nice to have a
better way
+ //to detect if the useNativeSequenceCache property has been set, the
best we can do
+ //is detect the variable in this code path as this is the path a
user's code
+ //would go down if they are still executing code which actually made
use of
+ //the support provided via setting useNativeSequenceCache.
+ if (!useNativeSequenceCache && logNativeSequenceCacheWarning){
+ log.warn(_loc.get("sequence-cache-warning"));
+ logNativeSequenceCacheWarning=false;
+ }
StringBuilder buf = new StringBuilder();
- buf.append("CREATE SEQUENCE ");
+ buf.append(create ? "CREATE" : "ALTER").append(" SEQUENCE ");
String seqName = checkNameLength(getFullName(seq), maxTableNameLength,
"long-seq-name");
buf.append(seqName);
- if (seq.getInitialValue() != 0)
+ if (create && seq.getInitialValue() != 0)
buf.append(" START WITH ").append(seq.getInitialValue());
if ((seq.getIncrement() > 1) || (seq.getAllocate() > 1))
buf.append(" INCREMENT BY ").append(seq.getIncrement() *
seq.getAllocate());
Modified:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.java?rev=1199921&r1=1199920&r2=1199921&view=diff
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.java
(original)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.java
Wed Nov 9 19:12:34 2011
@@ -79,11 +79,12 @@ public class TestNativeSeqGenerator exte
em.getTransaction().commit();
// Since allocationSize has a default of 50, we expect 2 sequence
fetches and 51 INSERTs.
- assertEquals("53 statements should be executed.", 53, getSQLCount());
- String[] statements = new String[53];
- statements[0] = ".*";
+ assertEquals("54 statements should be executed.", 54, getSQLCount());
+ String[] statements = new String[54];
+ statements[0] = "ALTER .*";
statements[1] = ".*";
- for (int i = 2; i < 53; i++) {
+ statements[2] = ".*";
+ for (int i = 3; i < 54; i++) {
statements[i] = "INSERT .*";
}
assertAllExactSQLInOrder(statements);