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();
}
}
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();
}
}
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();
}
}
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
}
}
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);
}
Aggregations