use of javax.jdo.datastore.Sequence in project tests by datanucleus.
the class SequenceTest method testFactorySequence.
/**
* Basic test for a sequence generated using a factory that is nontransactional.
* @throws Exception
*/
public void testFactorySequence() throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
Sequence seq = pm.getSequence("org.datanucleus.samples.store.ProductSequenceFactory");
try {
long id1 = seq.nextValue();
long id2 = seq.nextValue();
assertTrue("The ids obtained from a factory sequence were not different!", id1 != id2);
} catch (Exception e) {
fail("Attempt to retrieve the ids of a factory sequence failed : " + e.getMessage());
}
pm.close();
}
use of javax.jdo.datastore.Sequence in project tests by datanucleus.
the class SequenceTest method testContiguousSequence.
/**
* Basic test for a transactional contiguous sequence.
* @throws Exception
*/
public void testContiguousSequence() throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
if (!storeMgr.supportsValueGenerationStrategy("sequence")) {
// Doesn't support SEQUENCE strategy so test doesn't apply
return;
}
try {
tx.begin();
Sequence seq = pm.getSequence("org.datanucleus.samples.store.ProductSequence");
seq.allocate(2);
try {
long id1 = seq.nextValue();
long id2 = seq.nextValue();
assertTrue("The ids obtained from a transactional contiguous sequence were not sequential!", id1 + 1 == id2);
} catch (Exception e) {
fail("Attempt to retrieve the ids of an allocated sequence failed : " + e.getMessage());
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown while performing sequence test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.datastore.Sequence in project datanucleus-api-jdo by datanucleus.
the class JDOPersistenceManager method getSequence.
// ------------------------------------- Sequence Management --------------------------------------
/**
* Method to retrieve a sequence by name. As per JDO2 spec section 12.14.
* If the named sequence is not known, throws a JDOUserException.
* @param sequenceName Fully qualified name of the sequence
* @return The sequence
*/
public Sequence getSequence(String sequenceName) {
assertIsOpen();
SequenceMetaData seqmd = ec.getMetaDataManager().getMetaDataForSequence(ec.getClassLoaderResolver(), sequenceName);
if (seqmd == null) {
throw new JDOUserException(Localiser.msg("017000", sequenceName));
}
Sequence seq = null;
if (seqmd.getFactoryClass() != null) {
// User has specified a factory class
seq = pmf.getSequenceForFactoryClass(seqmd.getFactoryClass());
if (seq == null) {
// Create a new instance of the factory class and obtain the Sequence
Class factory = ec.getClassLoaderResolver().classForName(seqmd.getFactoryClass());
if (factory == null) {
throw new JDOUserException(Localiser.msg("017001", sequenceName, seqmd.getFactoryClass()));
}
Class[] argTypes = null;
Object[] arguments = null;
if (seqmd.getStrategy() != null) {
argTypes = new Class[2];
argTypes[0] = String.class;
argTypes[1] = String.class;
arguments = new Object[2];
arguments[0] = seqmd.getName();
arguments[1] = seqmd.getStrategy().toString();
} else {
argTypes = new Class[1];
argTypes[0] = String.class;
arguments = new Object[1];
arguments[0] = seqmd.getName();
}
Method newInstanceMethod;
try {
// Obtain the sequence from the static "newInstance(...)" method
newInstanceMethod = factory.getMethod("newInstance", argTypes);
seq = (Sequence) newInstanceMethod.invoke(null, arguments);
} catch (Exception e) {
throw new JDOUserException(Localiser.msg("017002", seqmd.getFactoryClass(), e.getMessage()));
}
// Register the sequence with the PMF
pmf.addSequenceForFactoryClass(seqmd.getFactoryClass(), seq);
}
} else {
NucleusSequence nucSeq = ec.getStoreManager().getNucleusSequence(ec, seqmd);
seq = new JDOSequence(nucSeq);
}
return seq;
}
use of javax.jdo.datastore.Sequence in project tests by datanucleus.
the class SequenceTest method testNontransactionalSequence.
/**
* Basic test for a nontransactional sequence.
* @throws Exception
*/
public void testNontransactionalSequence() throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
if (!storeMgr.supportsValueGenerationStrategy("sequence")) {
// Doesn't support SEQUENCE strategy so test doesn't apply
return;
}
Sequence seq = pm.getSequence("org.datanucleus.samples.store.ProductSequenceNontrans");
seq.allocate(2);
try {
long id1 = seq.nextValue();
long id2 = seq.nextValue();
assertTrue("The ids obtained from a nontransactional sequence were not sequential!", id1 + 1 == id2);
} catch (Exception e) {
fail("Attempt to retrieve the ids of a nontransactional sequence failed : " + e.getMessage());
}
pm.close();
}
Aggregations