Search in sources :

Example 31 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.

the class MultithreadPMTest method testMultipleTransitionWrite.

/**
 * Test changing the state
 */
public void testMultipleTransitionWrite() {
    Properties multiProps = new Properties();
    multiProps.setProperty(PropertyNames.PROPERTY_MULTITHREADED, "true");
    PersistenceManagerFactory myPMF = getPMF(1, multiProps);
    try {
        int THREAD_SIZE = 1000;
        Thread[] threads = new Thread[THREAD_SIZE];
        PersistenceManager pm = myPMF.getPersistenceManager();
        pm.currentTransaction().begin();
        final Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        final Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        woody.setManager(bart);
        pm.makePersistent(woody);
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i] = new Thread(new Runnable() {

                    public void run() {
                        woody.setLastName("name");
                        woody.setManager(boss);
                    }
                });
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].start();
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                try {
                    threads[i].join();
                } catch (InterruptedException e) {
                    fail(e.getMessage());
                }
            }
        } finally {
            if (pm.currentTransaction().isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(myPMF);
        myPMF.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 32 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.

the class MultithreadPMTest method testMultipleNonTransitionWrite.

/**
 * Test changing the state
 */
public void testMultipleNonTransitionWrite() {
    Properties multiProps = new Properties();
    multiProps.setProperty(PropertyNames.PROPERTY_MULTITHREADED, "true");
    PersistenceManagerFactory myPMF = getPMF(1, multiProps);
    try {
        int THREAD_SIZE = 1000;
        Thread[] threads = new Thread[THREAD_SIZE];
        PersistenceManager pm = myPMF.getPersistenceManager();
        pm.currentTransaction().begin();
        final Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        final Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        woody.setManager(bart);
        pm.makePersistent(woody);
        pm.currentTransaction().commit();
        pm.currentTransaction().setNontransactionalWrite(true);
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i] = new Thread(new Runnable() {

                    public void run() {
                        woody.setLastName("name");
                        woody.setManager(boss);
                    }
                });
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].start();
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                try {
                    threads[i].join();
                } catch (InterruptedException e) {
                    fail(e.getMessage());
                }
            }
        } finally {
            if (pm.currentTransaction().isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(myPMF);
        myPMF.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 33 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.

the class MultithreadPMTest method testMultipleDetachCopy.

public void testMultipleDetachCopy() {
    Properties multiProps = new Properties();
    multiProps.setProperty(PropertyNames.PROPERTY_MULTITHREADED, "true");
    PersistenceManagerFactory myPMF = getPMF(1, multiProps);
    try {
        int THREAD_SIZE = 1000;
        Thread[] threads = new Thread[THREAD_SIZE];
        MultithreadDetachRunner[] runner = new MultithreadDetachRunner[THREAD_SIZE];
        PersistenceManager pm = myPMF.getPersistenceManager();
        pm.currentTransaction().begin();
        Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        woody.setManager(bart);
        pm.makePersistent(woody);
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                runner[i] = new MultithreadDetachRunner(pm, woody);
                threads[i] = new Thread(runner[i]);
                threads[i].start();
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].join();
                if (runner[i].getException() != null) {
                    LOG.error("Exception during test", runner[i].getException());
                    fail("Exception thrown during test : " + runner[i].getException());
                }
            }
        } catch (Exception e) {
            fail(e.getMessage());
        } finally {
            if (pm.currentTransaction().isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(myPMF);
        myPMF.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 34 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManagerFactory method clearPMProxyDelegate.

/**
 * Convenience method to clear the thread-local delegate PM that we refer to.
 * This is invoked when the proxy PM has close() invoked.
 */
void clearPMProxyDelegate() {
    // TODO Is it safe to assume 'this' is actually the right PMF already?
    PersistenceManagerFactory pmf = getPMProxyDelegate().getPersistenceManagerFactory();
    String txnType = pmf.getTransactionType();
    if (TransactionType.RESOURCE_LOCAL.toString().equalsIgnoreCase(txnType)) {
        // Close the PM and unset the thread-local
        getPMProxyDelegate().close();
        pmProxyThreadLocal.remove();
    } else if (TransactionType.JTA.toString().equalsIgnoreCase(txnType)) {
    // Do nothing
    }
}
Also used : PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory)

Example 35 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project datanucleus-api-jdo by datanucleus.

the class JDOTransaction method setIsolationLevel.

/**
 * Mutator for the isolation level.
 * @param level The level
 * @throws JDOUserException if the required level is not supported.
 */
public void setIsolationLevel(String level) {
    assertNotCommitting();
    if (tx.isActive() && !tx.getOptimistic()) {
        throw new JDOUnsupportedOptionException("Cannot change the transaction isolation level while a datastore transaction is active");
    }
    PersistenceManagerFactory pmf = pm.getPersistenceManagerFactory();
    if (!pmf.supportedOptions().contains("javax.jdo.option.TransactionIsolationLevel." + level)) {
        throw new JDOUnsupportedOptionException("Isolation level \"" + level + "\" not supported by this datastore");
    }
    int isolationLevel = TransactionUtils.getTransactionIsolationLevelForName(level);
    tx.setOption(org.datanucleus.Transaction.TRANSACTION_ISOLATION_OPTION, isolationLevel);
}
Also used : JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory)

Aggregations

PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)67 PersistenceManager (javax.jdo.PersistenceManager)52 Transaction (javax.jdo.Transaction)44 Properties (java.util.Properties)40 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)34 Employee (org.jpox.samples.models.company.Employee)18 Query (javax.jdo.Query)15 JDOUserException (javax.jdo.JDOUserException)13 Manager (org.jpox.samples.models.company.Manager)13 Iterator (java.util.Iterator)12 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)11 Extent (javax.jdo.Extent)10 JDODataStoreCache (org.datanucleus.api.jdo.JDODataStoreCache)10 JDOFatalUserException (javax.jdo.JDOFatalUserException)9 Collection (java.util.Collection)8 JDOException (javax.jdo.JDOException)8 DataStoreCache (javax.jdo.datastore.DataStoreCache)8 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)8 Level2Cache (org.datanucleus.cache.Level2Cache)8 SQLException (java.sql.SQLException)7