use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class MultithreadedTest method testQueryAndDetach.
/**
* Test that populates the datastore, and then starts many threads querying and detaching the data
* and trying to access the detached data (checking for undetached fields that should have been detached).
*/
public void testQueryAndDetach() {
addClassesToSchema(new Class[] { Employee.class, Manager.class, Developer.class });
try {
// Persist some data
LOG.debug(">> Persisting data");
PersistenceManager pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_DETACH_ON_CLOSE, "true");
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Manager mgr = new Manager(1, "The", "Boss", "the.boss@datanucleus.com", 200000, "100000");
pm.makePersistent(mgr);
for (int i = 0; i < 100; i++) {
Employee emp = new Employee(i + 2, "FirstName" + i, "LastName" + i, "first.last." + i + "@datanucleus.com", 100000 + i, "12345" + i);
emp.setManager(mgr);
mgr.addSubordinate(emp);
pm.makePersistent(emp);
}
tx.commit();
} catch (Throwable thr) {
LOG.error("Exception persisting objects", thr);
fail("Exception persisting data : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
LOG.debug(">> Persisted data");
// Verify the persistence
pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_DETACH_ON_CLOSE, "true");
tx = pm.currentTransaction();
try {
tx.begin();
LOG.debug(">> Querying Employees");
Query<Employee> q = pm.newQuery(Employee.class);
List<Employee> emps = q.executeList();
for (Employee e : emps) {
LOG.debug(">> emp=" + e + " e.mgr=" + e.getManager());
}
LOG.debug(">> Queried Employees");
tx.commit();
} catch (Throwable thr) {
LOG.error("Exception checking objects", thr);
fail("Exception checking data : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Create the Threads
int THREAD_SIZE = 500;
final String[] threadErrors = new String[THREAD_SIZE];
Thread[] threads = new Thread[THREAD_SIZE];
for (int i = 0; i < THREAD_SIZE; i++) {
final int threadNo = i;
threads[i] = new Thread(new Runnable() {
public void run() {
String errorMsg = processQueryAndDetach(true);
threadErrors[threadNo] = errorMsg;
}
});
}
// Run the Threads
LOG.debug(">> Starting threads");
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());
}
}
LOG.debug(">> Completed threads");
// Process any errors and fail the test if any threads failed present
for (String error : threadErrors) {
if (error != null) {
fail(error);
}
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Manager 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 org.jpox.samples.models.company.Manager 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 org.jpox.samples.models.company.Manager 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 org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class JDOQLBasicTest method testCast.
public void testCast() {
try {
Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
bart.addSubordinate(boss);
bart.addSubordinate(boss2);
Department deptB = new Department("DeptB");
bart.addDepartment(deptB);
Qualification q1 = new Qualification("q1");
q1.setPerson(boss);
Qualification q2 = new Qualification("q2");
q2.setPerson(boss2);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(bart);
pm.makePersistent(homer);
pm.makePersistent(boss);
pm.makePersistent(boss2);
pm.makePersistent(q1);
pm.makePersistent(q2);
tx.commit();
tx.begin();
Query q = pm.newQuery(Qualification.class);
Collection c = q.filter("((Employee)person).serialNo == \"serial 3\"").imports("import org.jpox.samples.models.company.Employee").executeList();
assertEquals(1, c.size());
assertEquals("q1", ((Qualification) c.iterator().next()).getName());
q = pm.newQuery(Qualification.class);
c = q.filter("((Manager)person).serialNo == \"serial 4\"").imports("import org.jpox.samples.models.company.Employee").executeList();
assertEquals(1, c.size());
assertEquals("q2", ((Qualification) c.iterator().next()).getName());
tx.commit();
} finally {
if (tx.isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
} finally {
// Clean out our data
CompanyHelper.clearCompanyData(pmf);
}
}
Aggregations