mirror of https://github.com/apache/openjpa.git
OPENJPA-2069 Update db sequence's INCREMENT BY value to match the allocationSize specified in @SequenceGenerator
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1199921 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d64a3e5fdb
commit
5fafa802aa
|
@ -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
|
||||
/////////
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -79,11 +79,12 @@ public class TestNativeSeqGenerator extends SQLListenerTestCase {
|
|||
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);
|
||||
|
|
Loading…
Reference in New Issue