use of org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition in project eclipselink by eclipse-ee4j.
the class OracleNativeSeqInitTest method setup.
@Override
public void setup() {
if (!getSession().getPlatform().supportsSequenceObjects()) {
throw new TestWarningException("This test requires a platform that supports sequence objects");
}
ClassDescriptor descriptor = getSession().getDescriptor(Employee.class);
if (!descriptor.usesSequenceNumbers()) {
throw new TestWarningException("Employee doesn't use sequencing");
}
originalSequence = getSession().getPlatform().getSequence(descriptor.getSequenceNumberName());
usesNativeSequencingOriginal = (originalSequence instanceof NativeSequence || (originalSequence instanceof DefaultSequence && getSession().getPlatform().getDefaultSequence() instanceof NativeSequence)) && !originalSequence.shouldAcquireValueAfterInsert();
if (!usesNativeSequencingOriginal) {
NativeSequence newSequence = new NativeSequence(originalSequence.getName(), originalSequence.getPreallocationSize());
newSequence.onConnect(originalSequence.getDatasourcePlatform());
getAbstractSession().getPlatform().addSequence(newSequence);
sequence = newSequence;
} else {
sequence = originalSequence;
}
seqPreallocationSizeOriginal = originalSequence.getPreallocationSize();
lastSeqNumberOriginal = getSession().getNextSequenceNumberValue(Employee.class).intValue() - 1;
usesBatchWritingOriginal = getSession().getPlatform().usesBatchWriting();
shouldCacheAllStatementsOriginal = getSession().getPlatform().shouldCacheAllStatements();
getDatabaseSession().getSequencingControl().initializePreallocated();
sequenceDefinition = new SequenceObjectDefinition(sequence);
sequenceDefinition.setQualifier(getSession().getLogin().getTableQualifier());
if (shouldUseSchemaManager) {
schemaManager = new SchemaManager(getDatabaseSession());
// make sure that upcoming DROP and CREATE haven't been cached
// and therefore for sure will go through
getSession().getPlatform().setShouldCacheAllStatements(false);
// This is the worst case scenario settings - SchemaManager should handle it.
getSession().getPlatform().setUsesBatchWriting(true);
getSession().getPlatform().setShouldCacheAllStatements(true);
} else {
getSession().getPlatform().setUsesBatchWriting(false);
getSession().getPlatform().setShouldCacheAllStatements(false);
}
// all three modes start with dropping an existing sequence (if any)
try {
drop();
} catch (DatabaseException exception) {
// Ignore already deleted
}
if (mode == DROP_CREATE) {
// sequence doesn't exist.
// create sequence with seqPreallocationSize.
// note that both increment and starting value are set to
// sequenceDefinition.getIncrement()
sequence.setInitialValue(1);
sequence.setPreallocationSize(seqPreallocationSize);
create();
// next available sequence number.
idExpected = 1;
} else if (mode == CREATE_CREATE) {
// sequence doesn't exist,
// create sequence with seqPreallocationSizeOld
// note that both increment and starting value are set to
// sequenceDefinition.getIncrement()
sequence.setInitialValue(1);
sequence.setPreallocationSize(seqPreallocationSizeOld);
create();
// now sequence exists,
// create sequence with seqPreallocationSize
// Note that createOnDatabase will call alterOnDatabase
sequence.setInitialValue(1);
sequence.setPreallocationSize(seqPreallocationSize);
create();
// next available sequence number.
// note that the second createOnDatabase selects NEXTVAL during existance check,
// because it is the first call to NEXTVAL, the starting sequence value is returned,
// and this value was set to seqPreallocationSizeOld by the first createOnDatabase
idExpected = 1 + seqPreallocationSizeOld;
} else if (mode == NEXTVAL_ALTER) {
// sequence doesn't exist,
// create sequence with seqPreallocationSizeOld
// note that both increment and starting value are set to
// sequenceDefinition.getIncrement()
sequence.setInitialValue(1);
sequence.setPreallocationSize(seqPreallocationSizeOld);
create();
// now sequence exists,
// select NEXTVAL
// because it is the first call to NEXTVAL, the starting sequence value is returned,
// and this value was set to seqPreallocationSizeOld by the first createOnDatabase
sequenceDefinition.checkIfExist((AbstractSession) getSession());
// alter increment of sequence with seqPreallocationSize.
sequence.setInitialValue(1);
sequence.setPreallocationSize(seqPreallocationSize);
alter();
// next available sequence number.
// because there was just one call to NEXTVAL, the starting sequence value is returned,
// and this value was set to seqPreallocationSizeOld by createOnDatabase
idExpected = 1 + seqPreallocationSizeOld;
} else if (mode == CREATE_ALTER) {
// sequence doesn't exist,
// create sequence with seqPreallocationSizeOld
// note that both increment and starting value are set to
// sequenceDefinition.getIncrement()
sequence.setInitialValue(1);
sequence.setPreallocationSize(seqPreallocationSizeOld);
create();
// alter increment of sequence with seqPreallocationSize.
sequence.setInitialValue(1);
sequence.setPreallocationSize(seqPreallocationSize);
alter();
// next available sequence number.
idExpected = 1;
}
getSession().getPlatform().getSequence(descriptor.getSequenceNumberName()).setPreallocationSize(seqPreallocationSize);
}
use of org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method internalTestSequenceObjectDefinition.
protected void internalTestSequenceObjectDefinition(int preallocationSize, int startValue, String seqName, EntityManager em, AbstractSession ss) {
NativeSequence sequence = new NativeSequence(seqName, preallocationSize, startValue, false);
sequence.onConnect(ss.getPlatform());
SequenceObjectDefinition def = new SequenceObjectDefinition(sequence);
try {
// create sequence
String createStr = def.buildCreationWriter(ss, new StringWriter()).toString();
beginTransaction(em);
em.createNativeQuery(createStr).executeUpdate();
commitTransaction(em);
// sequence value preallocated
Vector seqValues = sequence.getGeneratedVector(null, ss);
int firstSequenceValue = ((Number) seqValues.elementAt(0)).intValue();
if (firstSequenceValue != startValue) {
fail(seqName + " sequence with preallocationSize = " + preallocationSize + " and startValue = " + startValue + " produced wrong firstSequenceValue =" + firstSequenceValue);
}
} finally {
sequence.onDisconnect(ss.getPlatform());
// CREATE and DML on the sequence are still open.
if (getDatabaseSession().getPlatform().isSymfoware())
return;
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
// drop sequence
String dropStr = def.buildDeletionWriter(ss, new StringWriter()).toString();
beginTransaction(em);
em.createNativeQuery(dropStr).executeUpdate();
commitTransaction(em);
}
}
use of org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition in project eclipselink by eclipse-ee4j.
the class SubstituteSequencingWithReturningPolicyAdapter method createSequences.
public void createSequences(Session session) {
if (!session.getPlatform().supportsSequenceObjects()) {
throw new TestWarningException("Requires database platform that supports sequence objects (like Oracle) - they will be used by triggers");
}
SchemaManager schemaManager = new SchemaManager((DatabaseSession) session);
Hashtable sequenceNameToDefinition = new Hashtable();
Enumeration tableNames = tableToField.keys();
while (tableNames.hasMoreElements()) {
String tableName = (String) tableNames.nextElement();
String sequenceName = getSequenceNameFromTableName(tableName);
if (!sequenceNameToDefinition.containsKey(sequenceName)) {
SequenceObjectDefinition definition = new SequenceObjectDefinition(new NativeSequence(sequenceName, 1, false));
sequenceNameToDefinition.put(sequenceName, definition);
schemaManager.createObject(definition);
}
}
}
use of org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method internalTestSequenceObjectDefinition.
protected void internalTestSequenceObjectDefinition(int preallocationSize, int startValue, String seqName, EntityManager em, ServerSession ss) {
NativeSequence sequence = new NativeSequence(seqName, preallocationSize, startValue, false);
sequence.onConnect(ss.getPlatform());
SequenceObjectDefinition def = new SequenceObjectDefinition(sequence);
boolean hasCreatedSequence = false;
try {
// create sequence
String createStr = def.buildCreationWriter(ss, new StringWriter()).toString();
beginTransaction(em);
em.createNativeQuery(createStr).setHint(QueryHints.COMPOSITE_UNIT_MEMBER, ss.getName()).executeUpdate();
commitTransaction(em);
hasCreatedSequence = true;
// sequence value preallocated
Vector seqValues = sequence.getGeneratedVector(null, ss);
int firstSequenceValue = ((Number) seqValues.elementAt(0)).intValue();
if (firstSequenceValue != startValue) {
fail(seqName + " sequence with preallocationSize = " + preallocationSize + " and startValue = " + startValue + " produced wrong firstSequenceValue =" + firstSequenceValue);
}
} finally {
sequence.onDisconnect(ss.getPlatform());
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
// CREATE and DML on the sequence are still open.
if (hasCreatedSequence && !ss.getPlatform().isSymfoware()) {
// drop sequence
String dropStr = def.buildDeletionWriter(ss, new StringWriter()).toString();
beginTransaction(em);
em.createNativeQuery(dropStr).setHint(QueryHints.COMPOSITE_UNIT_MEMBER, ss.getName()).executeUpdate();
commitTransaction(em);
}
}
}
use of org.eclipse.persistence.tools.schemaframework.SequenceObjectDefinition in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method internalTestSequenceObjectDefinition.
protected void internalTestSequenceObjectDefinition(int preallocationSize, int startValue, String seqName, EntityManager em, ServerSession ss) {
NativeSequence sequence = new NativeSequence(seqName, preallocationSize, startValue, false);
sequence.onConnect(ss.getPlatform());
SequenceObjectDefinition def = new SequenceObjectDefinition(sequence);
try {
// create sequence
String createStr = def.buildCreationWriter(ss, new StringWriter()).toString();
beginTransaction(em);
Query query = em.createNativeQuery(createStr);
query.setHint(QueryHints.BATCH_WRITING, false);
query.executeUpdate();
commitTransaction(em);
// sequence value preallocated
Vector seqValues = sequence.getGeneratedVector(null, ss);
int firstSequenceValue = ((Number) seqValues.elementAt(0)).intValue();
if (firstSequenceValue != startValue) {
fail(seqName + " sequence with preallocationSize = " + preallocationSize + " and startValue = " + startValue + " produced wrong firstSequenceValue =" + firstSequenceValue);
}
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
sequence.onDisconnect(ss.getPlatform());
// CREATE and DML on the sequence are still open.
if (JUnitTestCase.getServerSession().getPlatform().isSymfoware())
return;
// drop sequence
String dropStr = def.buildDeletionWriter(ss, new StringWriter()).toString();
beginTransaction(em);
Query query = em.createNativeQuery(dropStr);
query.setHint(QueryHints.BATCH_WRITING, false);
query.executeUpdate();
commitTransaction(em);
}
}
Aggregations