Search in sources :

Example 1 with Sequence

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();
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Sequence(javax.jdo.datastore.Sequence) JDOUserException(javax.jdo.JDOUserException)

Example 2 with Sequence

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();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Sequence(javax.jdo.datastore.Sequence) JDOUserException(javax.jdo.JDOUserException)

Example 3 with Sequence

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;
}
Also used : Sequence(javax.jdo.datastore.Sequence) NucleusSequence(org.datanucleus.store.NucleusSequence) Method(java.lang.reflect.Method) NucleusSequence(org.datanucleus.store.NucleusSequence) JDOUserException(javax.jdo.JDOUserException) SequenceMetaData(org.datanucleus.metadata.SequenceMetaData) JDONullIdentityException(javax.jdo.JDONullIdentityException) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException) NucleusException(org.datanucleus.exceptions.NucleusException) TransactionActiveOnCloseException(org.datanucleus.exceptions.TransactionActiveOnCloseException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) TransactionNotWritableException(org.datanucleus.api.jdo.exceptions.TransactionNotWritableException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) NucleusOptimisticException(org.datanucleus.exceptions.NucleusOptimisticException)

Example 4 with Sequence

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();
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Sequence(javax.jdo.datastore.Sequence) JDOUserException(javax.jdo.JDOUserException)

Aggregations

JDOUserException (javax.jdo.JDOUserException)4 Sequence (javax.jdo.datastore.Sequence)4 PersistenceManager (javax.jdo.PersistenceManager)3 Method (java.lang.reflect.Method)1 JDOException (javax.jdo.JDOException)1 JDOFatalUserException (javax.jdo.JDOFatalUserException)1 JDONullIdentityException (javax.jdo.JDONullIdentityException)1 JDOOptimisticVerificationException (javax.jdo.JDOOptimisticVerificationException)1 JDOUnsupportedOptionException (javax.jdo.JDOUnsupportedOptionException)1 Transaction (javax.jdo.Transaction)1 TransactionNotActiveException (org.datanucleus.api.jdo.exceptions.TransactionNotActiveException)1 TransactionNotWritableException (org.datanucleus.api.jdo.exceptions.TransactionNotWritableException)1 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusOptimisticException (org.datanucleus.exceptions.NucleusOptimisticException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 TransactionActiveOnCloseException (org.datanucleus.exceptions.TransactionActiveOnCloseException)1 SequenceMetaData (org.datanucleus.metadata.SequenceMetaData)1 NucleusSequence (org.datanucleus.store.NucleusSequence)1