use of org.jpox.samples.models.company.Account in project tests by datanucleus.
the class GeneralTest method testCloseOnTxnCompletion.
public void testCloseOnTxnCompletion() throws Exception {
try {
PersistenceManager pm = null;
pm = pmf.getPersistenceManager();
new PersistenceManagerDisposer(pm);
pm.currentTransaction().begin();
Account accnt = new Account();
accnt.setUsername("jpox");
pm.makePersistent(accnt);
Object oid = pm.getObjectId(accnt);
pm.currentTransaction().commit();
assertTrue("The PersistenceManager is still open", pm.isClosed());
pm = pmf.getPersistenceManager();
new PersistenceManagerDisposer(pm);
pm.currentTransaction().begin();
accnt = (Account) pm.getObjectById(oid);
pm.deletePersistent(accnt);
pm.currentTransaction().commit();
assertTrue("The PersistenceManager is still open", pm.isClosed());
pm = pmf.getPersistenceManager();
new PersistenceManagerDisposer(pm);
pm.currentTransaction().begin();
accnt = new Account();
accnt.setUsername("jpox");
pm.makePersistent(accnt);
pm.currentTransaction().rollback();
assertTrue("The PersistenceManager is still open", pm.isClosed());
UserTransaction ut = getUserTransaction();
ut.begin();
pm = pmf.getPersistenceManager();
new PersistenceManagerDisposer(pm);
accnt = new Account();
accnt.setUsername("jpox");
pm.makePersistent(accnt);
oid = pm.getObjectId(accnt);
ut.commit();
assertTrue("The PersistenceManager is still open", pm.isClosed());
ut.begin();
pm = pmf.getPersistenceManager();
new PersistenceManagerDisposer(pm);
accnt = (Account) pm.getObjectById(oid);
pm.deletePersistent(accnt);
ut.commit();
assertTrue("The PersistenceManager is still open", pm.isClosed());
ut.begin();
pm = pmf.getPersistenceManager();
new PersistenceManagerDisposer(pm);
accnt = new Account();
accnt.setUsername("jpox");
pm.makePersistent(accnt);
oid = pm.getObjectId(accnt);
ut.rollback();
assertTrue("The PersistenceManager is still open", pm.isClosed());
} finally {
clean(Account.class);
}
}
use of org.jpox.samples.models.company.Account in project tests by datanucleus.
the class GeneralTest method testBasicJTA.
public void testBasicJTA() throws Exception {
try {
UserTransaction ut = getUserTransaction();
ut.setTransactionTimeout(300);
int totals = 0;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.setOptimistic(true);
try {
ut.begin();
assertTrue("Transaction is not active after starting UserTransaction", tx.isActive());
Query q = pm.newQuery(Account.class);
Collection c = (Collection) q.execute();
totals = c.size();
q.closeAll();
ut.commit();
} finally {
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.setOptimistic(true);
try {
ut.begin();
Account accnt = new Account();
accnt.setUsername("jpox");
pm.makePersistent(accnt);
ut.commit();
} finally {
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.setOptimistic(true);
try {
ut.begin();
Query q = pm.newQuery(Account.class);
Collection c = (Collection) q.execute();
try {
Assert.assertTrue(c.size() == (totals + 1));
} finally {
q.closeAll();
ut.commit();
}
} finally {
pm.close();
}
} finally {
clean(Account.class);
}
}
use of org.jpox.samples.models.company.Account in project tests by datanucleus.
the class GeneralTest method testAddDeleteJTA.
public void testAddDeleteJTA() throws Exception {
try {
boolean opt = false;
UserTransaction ut = getUserTransaction();
ut.setTransactionTimeout(300);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.setOptimistic(opt);
ut.begin();
Query q = pm.newQuery(Account.class);
Collection c = (Collection) q.execute();
c.size();
q.closeAll();
ut.commit();
pm.close();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.setOptimistic(opt);
ut.begin();
Account accnt = new Account();
accnt.setUsername("jpox");
pm.makePersistent(accnt);
Object oid = pm.getObjectId(accnt);
ut.commit();
pm.close();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.setOptimistic(opt);
ut.begin();
accnt = (Account) pm.getObjectById(oid);
pm.deletePersistent(accnt);
ut.commit();
pm.close();
pm = pmf.getPersistenceManager();
pm.currentTransaction().setOptimistic(opt);
ut.begin();
try {
accnt = (Account) pm.getObjectById(oid);
System.err.println(accnt);
Assert.assertTrue("accnt still in db:" + pm.getObjectId(accnt), false);
} catch (javax.jdo.JDOObjectNotFoundException ex) {
} finally {
ut.commit();
pm.close();
}
} finally {
clean(Account.class);
}
}
use of org.jpox.samples.models.company.Account in project tests by datanucleus.
the class GeneralTest method testConnectionAccessDuringAfterCompletion.
/**
* Verify accessibility of a previously acquired DB connection during Synchronization.afterCompletion()
* (e.g. for application code that performs invalidation of custom caches in afterCompletion(),
* such as cached results of computations based on persistent values)
*/
public void testConnectionAccessDuringAfterCompletion() throws NamingException, NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
try {
final PersistenceManager pm = pmf.getPersistenceManager();
// prepare a persistent object to be found later on
pm.currentTransaction().begin();
Account accnt = new Account();
accnt.setUsername("jpox");
pm.makePersistent(accnt);
pm.currentTransaction().commit();
final boolean[] success = new boolean[1];
// register a Synchronization object that accesses the DB during afterCompletion()
pm.currentTransaction().setSynchronization(new Synchronization() {
public void beforeCompletion() {
}
public void afterCompletion(int arg0) {
// access a DB connection acquired previously during the transaction
dbConnectionAccess(pm);
success[0] = true;
}
});
UserTransaction ut = getUserTransaction();
ut.begin();
// acquire a db connection for the ongoing transaction
dbConnectionAccess(pm);
ut.commit();
assertTrue("accessing the DB connection during afterCompletion() wasn't successful", success[0]);
} finally {
clean(Account.class);
}
}
use of org.jpox.samples.models.company.Account in project tests by datanucleus.
the class GeneralTest method testAddDeleteJTANoBatch.
public void testAddDeleteJTANoBatch() throws Exception {
PersistenceManagerFactory myPMF = null;
try {
boolean opt = false;
UserTransaction ut = getUserTransaction();
ut.setTransactionTimeout(300);
// Create PMF with no batching
Properties props = new Properties();
props.put("datanucleus.rdbms.statementBatchLimit", "0");
myPMF = getPMF(1, props);
PersistenceManager pm = myPMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.setOptimistic(opt);
ut.begin();
Query q = pm.newQuery(Account.class);
Collection c = (Collection) q.execute();
c.size();
q.closeAll();
ut.commit();
pm.close();
pm = myPMF.getPersistenceManager();
tx = pm.currentTransaction();
tx.setOptimistic(opt);
ut.begin();
Account accnt = new Account();
accnt.setUsername("jpox");
pm.makePersistent(accnt);
Object oid = pm.getObjectId(accnt);
ut.commit();
pm.close();
pm = myPMF.getPersistenceManager();
tx = pm.currentTransaction();
tx.setOptimistic(opt);
ut.begin();
accnt = (Account) pm.getObjectById(oid);
pm.deletePersistent(accnt);
ut.commit();
pm.close();
pm = myPMF.getPersistenceManager();
pm.currentTransaction().setOptimistic(opt);
ut.begin();
try {
accnt = (Account) pm.getObjectById(oid);
System.err.println(accnt);
Assert.assertTrue("accnt still in db:" + pm.getObjectId(accnt), false);
} catch (javax.jdo.JDOObjectNotFoundException ex) {
} finally {
ut.commit();
pm.close();
}
} finally {
clean(myPMF, Account.class);
myPMF.close();
}
}
Aggregations